Merge pull request #1704 from vector-im/dbkr/refactor_room_create

Refactor room creation code a little
This commit is contained in:
David Baker
2023-10-06 16:18:18 +01:00
committed by GitHub
3 changed files with 50 additions and 34 deletions

View File

@@ -17,7 +17,6 @@ limitations under the License.
import { useState, useCallback, FormEvent, FormEventHandler } from "react"; import { useState, useCallback, FormEvent, FormEventHandler } from "react";
import { useHistory } from "react-router-dom"; import { useHistory } from "react-router-dom";
import { MatrixClient } from "matrix-js-sdk/src/client"; import { MatrixClient } from "matrix-js-sdk/src/client";
import { randomString } from "matrix-js-sdk/src/randomstring";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Heading } from "@vector-im/compound-web"; import { Heading } from "@vector-im/compound-web";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
@@ -42,8 +41,6 @@ import { Form } from "../form/Form";
import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting"; import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting";
import { AnalyticsNotice } from "../analytics/AnalyticsNotice"; import { AnalyticsNotice } from "../analytics/AnalyticsNotice";
import { E2EEBanner } from "../E2EEBanner"; import { E2EEBanner } from "../E2EEBanner";
import { setLocalStorageItem } from "../useLocalStorage";
import { getRoomSharedKeyLocalStorageKey } from "../e2ee/sharedKeyManagement";
interface Props { interface Props {
client: MatrixClient; client: MatrixClient;
@@ -77,20 +74,19 @@ export function RegisteredView({ client }: Props) {
setError(undefined); setError(undefined);
setLoading(true); setLoading(true);
const roomId = ( const createRoomResult = await createRoom(
await createRoom(client, roomName, e2eeEnabled ?? false) client,
)[1]; roomName,
e2eeEnabled ?? false
);
const roomPassword = randomString(32); history.push(
getRelativeRoomUrl(
if (e2eeEnabled) { createRoomResult.roomId,
setLocalStorageItem( roomName,
getRoomSharedKeyLocalStorageKey(roomId), createRoomResult.password
roomPassword )
); );
}
history.push(getRelativeRoomUrl(roomId, roomName, roomPassword));
} }
submit().catch((error) => { submit().catch((error) => {

View File

@@ -44,8 +44,6 @@ import { AnalyticsNotice } from "../analytics/AnalyticsNotice";
import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting"; import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting";
import { Config } from "../config/Config"; import { Config } from "../config/Config";
import { E2EEBanner } from "../E2EEBanner"; import { E2EEBanner } from "../E2EEBanner";
import { getRoomSharedKeyLocalStorageKey } from "../e2ee/sharedKeyManagement";
import { setLocalStorageItem } from "../useLocalStorage";
export const UnauthenticatedView: FC = () => { export const UnauthenticatedView: FC = () => {
const { setClient } = useClient(); const { setClient } = useClient();
@@ -87,19 +85,13 @@ export const UnauthenticatedView: FC = () => {
true true
); );
let roomId: string; let createRoomResult;
const roomPassword = randomString(32);
try { try {
roomId = ( createRoomResult = await createRoom(
await createRoom(client, roomName, e2eeEnabled ?? false) client,
)[1]; roomName,
e2eeEnabled ?? false
if (e2eeEnabled) { );
setLocalStorageItem(
getRoomSharedKeyLocalStorageKey(roomId),
roomPassword
);
}
} catch (error) { } catch (error) {
if (!setClient) { if (!setClient) {
throw error; throw error;
@@ -128,7 +120,13 @@ export const UnauthenticatedView: FC = () => {
} }
setClient({ client, session }); setClient({ client, session });
history.push(getRelativeRoomUrl(roomId, roomName, roomPassword)); history.push(
getRelativeRoomUrl(
createRoomResult.roomId,
roomName,
createRoomResult.password
)
);
} }
submit().catch((error) => { submit().catch((error) => {

View File

@@ -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"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import {
GroupCallIntent, GroupCallIntent,
GroupCallType, GroupCallType,
} from "matrix-js-sdk/src/webrtc/groupCall"; } 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 { 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";
@@ -35,6 +36,8 @@ import IndexedDBWorker from "./IndexedDBWorker?worker";
import { getUrlParams, PASSWORD_STRING } from "./UrlParams"; import { getUrlParams, PASSWORD_STRING } from "./UrlParams";
import { loadOlm } from "./olm"; import { loadOlm } from "./olm";
import { Config } from "./config/Config"; import { Config } from "./config/Config";
import { setLocalStorageItem } from "./useLocalStorage";
import { getRoomSharedKeyLocalStorageKey } from "./e2ee/sharedKeyManagement";
export const fallbackICEServerAllowed = export const fallbackICEServerAllowed =
import.meta.env.VITE_FALLBACK_STUN_ALLOWED === "true"; 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(); return parts[1] === client.getDomain();
} }
interface CreateRoomResult {
roomId: string;
alias?: string;
password?: string;
}
export async function createRoom( export async function createRoom(
client: MatrixClient, client: MatrixClient,
name: string, name: string,
e2ee: boolean e2ee: boolean
): Promise<[string, string]> { ): Promise<CreateRoomResult> {
logger.log(`Creating room for group call`); logger.log(`Creating room for group call`);
const createPromise = client.createRoom({ const createPromise = client.createRoom({
visibility: Visibility.Private, visibility: Visibility.Private,
@@ -336,7 +345,20 @@ export async function createRoom(
true 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,
};
} }
/** /**