Merge pull request #479 from vector-im/dbkr/wait_for_room
Fix 'cannot find room' error
This commit is contained in:
commit
075049abc4
4 changed files with 42 additions and 8 deletions
|
@ -47,7 +47,7 @@ export function RegisteredView({ client }) {
|
||||||
setError(undefined);
|
setError(undefined);
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const roomIdOrAlias = await createRoom(client, roomName, ptt);
|
const [roomIdOrAlias] = await createRoom(client, roomName, ptt);
|
||||||
|
|
||||||
if (roomIdOrAlias) {
|
if (roomIdOrAlias) {
|
||||||
history.push(`/room/${roomIdOrAlias}`);
|
history.push(`/room/${roomIdOrAlias}`);
|
||||||
|
|
|
@ -70,7 +70,7 @@ export function UnauthenticatedView() {
|
||||||
|
|
||||||
let roomIdOrAlias;
|
let roomIdOrAlias;
|
||||||
try {
|
try {
|
||||||
roomIdOrAlias = await createRoom(client, roomName, ptt);
|
[roomIdOrAlias] = await createRoom(client, roomName, ptt);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.errcode === "M_ROOM_IN_USE") {
|
if (error.errcode === "M_ROOM_IN_USE") {
|
||||||
setOnFinished(() => () => {
|
setOnFinished(() => () => {
|
||||||
|
|
|
@ -220,8 +220,8 @@ export function isLocalRoomId(roomId: string): boolean {
|
||||||
export async function createRoom(
|
export async function createRoom(
|
||||||
client: MatrixClient,
|
client: MatrixClient,
|
||||||
name: string
|
name: string
|
||||||
): Promise<string> {
|
): Promise<[string, string]> {
|
||||||
await client.createRoom({
|
const result = await client.createRoom({
|
||||||
visibility: Visibility.Private,
|
visibility: Visibility.Private,
|
||||||
preset: Preset.PublicChat,
|
preset: Preset.PublicChat,
|
||||||
name,
|
name,
|
||||||
|
@ -251,7 +251,7 @@ export async function createRoom(
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return fullAliasFromRoomName(name, client);
|
return [fullAliasFromRoomName(name, client), result.room_id];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRoomUrl(roomId: string): string {
|
export function getRoomUrl(roomId: string): string {
|
||||||
|
|
|
@ -21,6 +21,7 @@ import {
|
||||||
GroupCallIntent,
|
GroupCallIntent,
|
||||||
} from "matrix-js-sdk/src/webrtc/groupCall";
|
} from "matrix-js-sdk/src/webrtc/groupCall";
|
||||||
import { GroupCallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/groupCallEventHandler";
|
import { GroupCallEventHandlerEvent } from "matrix-js-sdk/src/webrtc/groupCallEventHandler";
|
||||||
|
import { ClientEvent } from "matrix-js-sdk/src/client";
|
||||||
|
|
||||||
import type { MatrixClient } from "matrix-js-sdk/src/client";
|
import type { MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
import type { Room } from "matrix-js-sdk/src/models/room";
|
import type { Room } from "matrix-js-sdk/src/models/room";
|
||||||
|
@ -44,9 +45,38 @@ export const useLoadGroupCall = (
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setState({ loading: true });
|
setState({ loading: true });
|
||||||
|
|
||||||
|
const waitForRoom = async (roomId: string): Promise<Room> => {
|
||||||
|
const room = client.getRoom(roomId);
|
||||||
|
if (room) return room;
|
||||||
|
console.log(`Room ${roomId} hasn't arrived yet: waiting`);
|
||||||
|
|
||||||
|
const waitPromise = new Promise<Room>((resolve) => {
|
||||||
|
const onRoomEvent = async (room: Room) => {
|
||||||
|
if (room.roomId === roomId) {
|
||||||
|
client.removeListener(ClientEvent.Room, onRoomEvent);
|
||||||
|
resolve(room);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
client.on(ClientEvent.Room, onRoomEvent);
|
||||||
|
});
|
||||||
|
|
||||||
|
// race the promise with a timeout so we don't
|
||||||
|
// wait forever for the room
|
||||||
|
const timeoutPromise = new Promise<Room>((_, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error("Timed out trying to join room"));
|
||||||
|
}, 30000);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.race([waitPromise, timeoutPromise]);
|
||||||
|
};
|
||||||
|
|
||||||
const fetchOrCreateRoom = async (): Promise<Room> => {
|
const fetchOrCreateRoom = async (): Promise<Room> => {
|
||||||
try {
|
try {
|
||||||
return await client.joinRoom(roomIdOrAlias, { viaServers });
|
const room = await client.joinRoom(roomIdOrAlias, { viaServers });
|
||||||
|
// wait for the room to come down the sync stream, otherwise
|
||||||
|
// client.getRoom() won't return the room.
|
||||||
|
return waitForRoom(room.roomId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (
|
if (
|
||||||
isLocalRoomId(roomIdOrAlias) &&
|
isLocalRoomId(roomIdOrAlias) &&
|
||||||
|
@ -55,8 +85,12 @@ export const useLoadGroupCall = (
|
||||||
error.message.indexOf("Failed to fetch alias") !== -1))
|
error.message.indexOf("Failed to fetch alias") !== -1))
|
||||||
) {
|
) {
|
||||||
// The room doesn't exist, but we can create it
|
// The room doesn't exist, but we can create it
|
||||||
await createRoom(client, roomNameFromRoomId(roomIdOrAlias));
|
const [, roomId] = await createRoom(
|
||||||
return await client.joinRoom(roomIdOrAlias, { viaServers });
|
client,
|
||||||
|
roomNameFromRoomId(roomIdOrAlias)
|
||||||
|
);
|
||||||
|
// likewise, wait for the room
|
||||||
|
return await waitForRoom(roomId);
|
||||||
} else {
|
} else {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue