diff --git a/src/home/RegisteredView.tsx b/src/home/RegisteredView.tsx index 779072bb..995f7a54 100644 --- a/src/home/RegisteredView.tsx +++ b/src/home/RegisteredView.tsx @@ -17,7 +17,6 @@ limitations under the License. import { useState, useCallback, FormEvent, FormEventHandler } from "react"; import { useHistory } from "react-router-dom"; import { MatrixClient } from "matrix-js-sdk/src/client"; -import { randomString } from "matrix-js-sdk/src/randomstring"; import { useTranslation } from "react-i18next"; import { Heading } from "@vector-im/compound-web"; import { logger } from "matrix-js-sdk/src/logger"; @@ -42,8 +41,6 @@ import { Form } from "../form/Form"; import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting"; import { AnalyticsNotice } from "../analytics/AnalyticsNotice"; import { E2EEBanner } from "../E2EEBanner"; -import { setLocalStorageItem } from "../useLocalStorage"; -import { getRoomSharedKeyLocalStorageKey } from "../e2ee/sharedKeyManagement"; interface Props { client: MatrixClient; @@ -77,20 +74,19 @@ export function RegisteredView({ client }: Props) { setError(undefined); setLoading(true); - const roomId = ( - await createRoom(client, roomName, e2eeEnabled ?? false) - )[1]; + const createRoomResult = await createRoom( + client, + roomName, + e2eeEnabled ?? false + ); - const roomPassword = randomString(32); - - if (e2eeEnabled) { - setLocalStorageItem( - getRoomSharedKeyLocalStorageKey(roomId), - roomPassword - ); - } - - history.push(getRelativeRoomUrl(roomId, roomName, roomPassword)); + history.push( + getRelativeRoomUrl( + createRoomResult.roomId, + roomName, + createRoomResult.password + ) + ); } submit().catch((error) => { diff --git a/src/home/UnauthenticatedView.tsx b/src/home/UnauthenticatedView.tsx index 376e4944..2f675436 100644 --- a/src/home/UnauthenticatedView.tsx +++ b/src/home/UnauthenticatedView.tsx @@ -44,8 +44,6 @@ import { AnalyticsNotice } from "../analytics/AnalyticsNotice"; import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting"; import { Config } from "../config/Config"; import { E2EEBanner } from "../E2EEBanner"; -import { getRoomSharedKeyLocalStorageKey } from "../e2ee/sharedKeyManagement"; -import { setLocalStorageItem } from "../useLocalStorage"; export const UnauthenticatedView: FC = () => { const { setClient } = useClient(); @@ -87,19 +85,13 @@ export const UnauthenticatedView: FC = () => { true ); - let roomId: string; - const roomPassword = randomString(32); + let createRoomResult; try { - roomId = ( - await createRoom(client, roomName, e2eeEnabled ?? false) - )[1]; - - if (e2eeEnabled) { - setLocalStorageItem( - getRoomSharedKeyLocalStorageKey(roomId), - roomPassword - ); - } + createRoomResult = await createRoom( + client, + roomName, + e2eeEnabled ?? false + ); } catch (error) { if (!setClient) { throw error; @@ -128,7 +120,13 @@ export const UnauthenticatedView: FC = () => { } setClient({ client, session }); - history.push(getRelativeRoomUrl(roomId, roomName, roomPassword)); + history.push( + getRelativeRoomUrl( + createRoomResult.roomId, + roomName, + createRoomResult.password + ) + ); } submit().catch((error) => { diff --git a/src/matrix-utils.ts b/src/matrix-utils.ts index e64d3d8d..471df590 100644 --- a/src/matrix-utils.ts +++ b/src/matrix-utils.ts @@ -1,5 +1,5 @@ /* -Copyright 2022 New Vector Ltd +Copyright 2022-2023 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import { GroupCallIntent, GroupCallType, } from "matrix-js-sdk/src/webrtc/groupCall"; +import { randomString } from "matrix-js-sdk/src/randomstring"; import type { MatrixClient } from "matrix-js-sdk/src/client"; import type { Room } from "matrix-js-sdk/src/models/room"; @@ -35,6 +36,8 @@ import IndexedDBWorker from "./IndexedDBWorker?worker"; import { getUrlParams, PASSWORD_STRING } from "./UrlParams"; import { loadOlm } from "./olm"; import { Config } from "./config/Config"; +import { setLocalStorageItem } from "./useLocalStorage"; +import { getRoomSharedKeyLocalStorageKey } from "./e2ee/sharedKeyManagement"; export const fallbackICEServerAllowed = import.meta.env.VITE_FALLBACK_STUN_ALLOWED === "true"; @@ -269,11 +272,17 @@ export function isLocalRoomId(roomId: string, client: MatrixClient): boolean { return parts[1] === client.getDomain(); } +interface CreateRoomResult { + roomId: string; + alias?: string; + password?: string; +} + export async function createRoom( client: MatrixClient, name: string, e2ee: boolean -): Promise<[string, string]> { +): Promise { logger.log(`Creating room for group call`); const createPromise = client.createRoom({ visibility: Visibility.Private, @@ -336,7 +345,20 @@ export async function createRoom( true ); - return [fullAliasFromRoomName(name, client), result.room_id]; + let password; + if (e2ee) { + password = randomString(32); + setLocalStorageItem( + getRoomSharedKeyLocalStorageKey(result.room_id), + password + ); + } + + return { + roomId: result.room_id, + alias: e2ee ? undefined : fullAliasFromRoomName(name, client), + password, + }; } /**