From 2a667b4a91ee4b6c6a9b084970d65ee7f33bfed6 Mon Sep 17 00:00:00 2001 From: Timo Date: Wed, 4 Jun 2025 15:34:56 +0200 Subject: [PATCH] review --- src/e2ee/sharedKeyManagement.ts | 22 +++++++++++++++++++--- src/home/useGroupCallRooms.ts | 3 +-- src/room/GroupCallView.tsx | 11 ++++++++++- src/useLocalStorage.ts | 9 ++++++--- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/e2ee/sharedKeyManagement.ts b/src/e2ee/sharedKeyManagement.ts index 6cb519fe..c8e39a41 100644 --- a/src/e2ee/sharedKeyManagement.ts +++ b/src/e2ee/sharedKeyManagement.ts @@ -7,16 +7,27 @@ Please see LICENSE in the repository root for full details. import { useEffect, useMemo } from "react"; -import { setLocalStorageItem, useLocalStorage } from "../useLocalStorage"; +import { + setLocalStorageItemReactive, + useLocalStorage, +} from "../useLocalStorage"; import { getUrlParams } from "../UrlParams"; import { E2eeType } from "./e2eeType"; import { useClient } from "../ClientContext"; +import { logger } from "matrix-js-sdk/lib/logger"; /** * This setter will update the state for all `useRoomSharedKey` hooks + * if the password is different from the one in local storage or if its not yet in the local storage. */ export function saveKeyForRoom(roomId: string, password: string): void { - setLocalStorageItem(getRoomSharedKeyLocalStorageKey(roomId), password); + if ( + localStorage.getItem(getRoomSharedKeyLocalStorageKey(roomId)) !== password + ) + setLocalStorageItemReactive( + getRoomSharedKeyLocalStorageKey(roomId), + password, + ); } const getRoomSharedKeyLocalStorageKey = (roomId: string): string => @@ -48,7 +59,12 @@ const useRoomSharedKey = ( export function getKeyForRoom(roomId: string): string | null { const { roomId: urlRoomId, password } = getUrlParams(); - if (roomId !== urlRoomId) return null; + if (roomId !== urlRoomId) + logger.warn( + "requested key for a roomId which is not the current call room id (from the URL)", + roomId, + urlRoomId, + ); return ( password ?? localStorage.getItem(getRoomSharedKeyLocalStorageKey(roomId)) ); diff --git a/src/home/useGroupCallRooms.ts b/src/home/useGroupCallRooms.ts index fe4cb104..e89c3f14 100644 --- a/src/home/useGroupCallRooms.ts +++ b/src/home/useGroupCallRooms.ts @@ -21,7 +21,7 @@ import { type MatrixRTCSession, } from "matrix-js-sdk/lib/matrixrtc"; -import { getKeyForRoom, saveKeyForRoom } from "../e2ee/sharedKeyManagement"; +import { getKeyForRoom } from "../e2ee/sharedKeyManagement"; export interface GroupCallRoom { roomAlias?: string; @@ -87,7 +87,6 @@ const roomIsJoinable = (room: Room): boolean => { // in case this key also does not exists we cannot join the room. return false; } - if (password) saveKeyForRoom(room.roomId, password); // otherwise we can always join rooms because we will automatically decide if we want to use perParticipant or password switch (room.getJoinRule()) { case JoinRule.Public: diff --git a/src/room/GroupCallView.tsx b/src/room/GroupCallView.tsx index 4097af6c..831cf84f 100644 --- a/src/room/GroupCallView.tsx +++ b/src/room/GroupCallView.tsx @@ -43,7 +43,10 @@ import { MUTE_PARTICIPANT_COUNT, type MuteStates } from "./MuteStates"; import { useMediaDevices } from "../livekit/MediaDevicesContext"; import { useMatrixRTCSessionMemberships } from "../useMatrixRTCSessionMemberships"; import { enterRTCSession, leaveRTCSession } from "../rtcSessionHelpers"; -import { useRoomEncryptionSystem } from "../e2ee/sharedKeyManagement"; +import { + saveKeyForRoom, + useRoomEncryptionSystem, +} from "../e2ee/sharedKeyManagement"; import { useRoomAvatar } from "./useRoomAvatar"; import { useRoomName } from "./useRoomName"; import { useJoinRule } from "./useJoinRule"; @@ -167,6 +170,12 @@ export const GroupCallView: FC = ({ useExperimentalToDeviceTransportSetting, ); + // Save the password once we start the groupCallView + const { password: passwordFromUrl } = useUrlParams(); + useEffect(() => { + if (passwordFromUrl) saveKeyForRoom(room.roomId, passwordFromUrl); + }, [passwordFromUrl, room.roomId]); + usePageTitle(roomName); const matrixInfo = useMemo((): MatrixInfo => { diff --git a/src/useLocalStorage.ts b/src/useLocalStorage.ts index 031eb2ac..43e828bf 100644 --- a/src/useLocalStorage.ts +++ b/src/useLocalStorage.ts @@ -15,8 +15,8 @@ export const localStorageBus = new EventEmitter(); /** * Like useState, but reads from and persists the value to localStorage - * this hook will not update when we write to localStorage.setItem(key, value) directly. - * For the hook to react either use the returned setter or `saveKeyForRoom`. + * This hook will not update when we write to localStorage.setItem(key, value) directly. + * For the hook to react either use the returned setter or `setLocalStorageItemReactive`. */ export const useLocalStorage = ( key: string, @@ -45,7 +45,10 @@ export const useLocalStorage = ( ]; }; -export const setLocalStorageItem = (key: string, value: string): void => { +export const setLocalStorageItemReactive = ( + key: string, + value: string, +): void => { localStorage.setItem(key, value); localStorageBus.emit(key, value); };