diff --git a/src/home/RegisteredView.tsx b/src/home/RegisteredView.tsx index ea6aac2..71abb28 100644 --- a/src/home/RegisteredView.tsx +++ b/src/home/RegisteredView.tsx @@ -56,13 +56,13 @@ export function RegisteredView({ client, isPasswordlessUser }: Props) { const data = new FormData(e.target as HTMLFormElement); const roomNameData = data.get("callName"); const roomName = typeof roomNameData === "string" ? roomNameData : ""; - // const ptt = callType === CallType.Radio; + const ptt = callType === CallType.Radio; async function submit() { setError(undefined); setLoading(true); - const [roomIdOrAlias] = await createRoom(client, roomName); + const [roomIdOrAlias] = await createRoom(client, roomName, ptt); if (roomIdOrAlias) { history.push(`/room/${roomIdOrAlias}`); @@ -82,7 +82,7 @@ export function RegisteredView({ client, isPasswordlessUser }: Props) { } }); }, - [client, history, modalState] + [client, history, modalState, callType] ); const recentRooms = useGroupCallRooms(client); diff --git a/src/matrix-utils.ts b/src/matrix-utils.ts index ed3cde3..4c25d17 100644 --- a/src/matrix-utils.ts +++ b/src/matrix-utils.ts @@ -17,7 +17,12 @@ import { Visibility, Preset } from "matrix-js-sdk/src/@types/partials"; import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync"; import { WidgetApi } from "matrix-widget-api"; import { logger } from "matrix-js-sdk/src/logger"; +import { + GroupCallIntent, + GroupCallType, +} from "matrix-js-sdk/src/webrtc/groupCall"; +import type { Room } from "matrix-js-sdk/src/models/room"; import IndexedDBWorker from "./IndexedDBWorker?worker"; import { getRoomParams } from "./room/useRoomParams"; @@ -290,9 +295,10 @@ export function isLocalRoomId(roomId: string): boolean { export async function createRoom( client: MatrixClient, - name: string + name: string, + ptt: boolean ): Promise<[string, string]> { - const result = await client.createRoom({ + const createPromise = client.createRoom({ visibility: Visibility.Private, preset: Preset.PublicChat, name, @@ -322,6 +328,36 @@ export async function createRoom( }, }); + // Wait for the room to arrive + await new Promise((resolve, reject) => { + const onRoom = async (room: Room) => { + if (room.roomId === (await createPromise).room_id) { + resolve(); + cleanUp(); + } + }; + createPromise.catch((e) => { + reject(e); + cleanUp(); + }); + + const cleanUp = () => { + client.off(ClientEvent.Room, onRoom); + }; + client.on(ClientEvent.Room, onRoom); + }); + + const result = await createPromise; + + console.log(`Creating ${ptt ? "PTT" : "video"} group call room`); + + await client.createGroupCall( + result.room_id, + ptt ? GroupCallType.Voice : GroupCallType.Video, + ptt, + GroupCallIntent.Room + ); + return [fullAliasFromRoomName(name, client), result.room_id]; } diff --git a/src/room/useLoadGroupCall.ts b/src/room/useLoadGroupCall.ts index 301ba54..a741b90 100644 --- a/src/room/useLoadGroupCall.ts +++ b/src/room/useLoadGroupCall.ts @@ -87,7 +87,8 @@ export const useLoadGroupCall = ( // The room doesn't exist, but we can create it const [, roomId] = await createRoom( client, - roomNameFromRoomId(roomIdOrAlias) + roomNameFromRoomId(roomIdOrAlias), + createPtt ); // likewise, wait for the room return await waitForRoom(roomId);