diff --git a/src/matrix-utils.ts b/src/matrix-utils.ts index 3e576e7..81da90c 100644 --- a/src/matrix-utils.ts +++ b/src/matrix-utils.ts @@ -8,6 +8,7 @@ import { MemoryCryptoStore } from "matrix-js-sdk/src/crypto/store/memory-crypto- import { createClient, MatrixClient } from "matrix-js-sdk/src/matrix"; import { ICreateClientOpts } from "matrix-js-sdk/src/matrix"; import { ClientEvent } from "matrix-js-sdk/src/client"; +import { Room } from "matrix-js-sdk/src/models/room"; import { Visibility, Preset } from "matrix-js-sdk/src/@types/partials"; import { GroupCallIntent, @@ -226,6 +227,23 @@ export async function createRoom( name: string, isPtt = false ): Promise { + let setExpectedRoomId: (roomId: string) => void; + const expectedRoomId = new Promise( + (resolve) => (setExpectedRoomId = resolve) + ); + + // There's no telling whether the new room will come down sync in the middle + // of the createRoom request, or after it, so start watching for it beforehand + const roomReceived = new Promise((resolve) => { + const onRoom = async (room: Room) => { + if (room.roomId === (await expectedRoomId)) { + resolve(); + client.off(ClientEvent.Room, onRoom); + } + }; + client.on(ClientEvent.Room, onRoom); + }); + const createRoomResult = await client.createRoom({ visibility: Visibility.Private, preset: Preset.PublicChat, @@ -256,6 +274,10 @@ export async function createRoom( }, }); + // Wait for the room to come down sync before doing anything with it + setExpectedRoomId(createRoomResult.room_id); + await roomReceived; + console.log(`Creating ${isPtt ? "PTT" : "video"} group call room`); await client.createGroupCall(