Refactor useIsRoomE2EE

Make it take a room object rather than a room ID to avoid it depending
on a side effect, ie. if the room object input changes, the hook will be
re-run but if we can't get the room from the room ID for whatever reason,
we'd be stuck.

Also add logging on why we decided a room was e2ee.
This commit is contained in:
David Baker
2023-09-22 15:35:03 +01:00
parent e9e37736b0
commit 3cd0ca205b
4 changed files with 23 additions and 14 deletions

View File

@@ -15,10 +15,11 @@ limitations under the License.
*/
import { useEffect, useMemo } from "react";
import { Room } from "matrix-js-sdk";
import { logger } from "matrix-js-sdk/src/logger";
import { useEnableE2EE } from "../settings/useSetting";
import { useLocalStorage } from "../useLocalStorage";
import { useClient } from "../ClientContext";
import { PASSWORD_STRING, useUrlParams } from "../UrlParams";
import { widget } from "../widget";
@@ -83,14 +84,19 @@ export const useManageRoomSharedKey = (roomId: string): string | null => {
return e2eeSharedKey ?? urlPassword;
};
export const useIsRoomE2EE = (roomId: string): boolean | null => {
const { client } = useClient();
const room = useMemo(() => client?.getRoom(roomId) ?? null, [roomId, client]);
export const useIsRoomE2EE = (room?: Room): boolean | null => {
// For now, rooms in widget mode are never considered encrypted.
// In the future, when widget mode gains encryption support, then perhaps we
// should inspect the e2eEnabled URL parameter here?
return useMemo(
() => widget === null && (room === null || !room.getCanonicalAlias()),
const canonAlias = useMemo(
() => (room ? room.getCanonicalAlias() : null),
[room]
);
const result = room ? !canonAlias && !widget : null;
logger.debug(
`Room ${room?.roomId} has canon alias ${canonAlias}. Is E2EE: ${result}`
);
return result;
};