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 { 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) => {

View File

@@ -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) => {

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");
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<CreateRoomResult> {
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,
};
}
/**