Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in useIsEncrypted (#28282)

* Replace `MatrixClient.isRoomEncrypted` by `MatrixClient.CryptoApi.isEncryptionEnabledInRoom` in `useIsEncrypted`

* Catch error

* Return `null` when computed

* Use `useRoomState` & `useAsyncMemo`
This commit is contained in:
Florian Duros 2024-11-13 11:32:35 +01:00 committed by GitHub
parent c67e67af4e
commit 8a756b592c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 29 deletions

View file

@ -6,24 +6,25 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details. Please see LICENSE files in the repository root for full details.
*/ */
import { useCallback, useState } from "react"; import { MatrixClient, MatrixEvent, Room, EventType } from "matrix-js-sdk/src/matrix";
import { MatrixClient, MatrixEvent, Room, RoomStateEvent, EventType } from "matrix-js-sdk/src/matrix";
import { useTypedEventEmitter } from "./useEventEmitter"; import { useRoomState } from "./useRoomState.ts";
import { useAsyncMemo } from "./useAsyncMemo.ts";
// Hook to simplify watching whether a Matrix room is encrypted, returns undefined if room is undefined // Hook to simplify watching whether a Matrix room is encrypted, returns null if room is undefined or the state is loading
export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | undefined { export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | null {
const [isEncrypted, setIsEncrypted] = useState(room ? cli.isRoomEncrypted(room.roomId) : undefined); const encryptionStateEvent: MatrixEvent | undefined = useRoomState(
room,
const update = useCallback( (roomState) => roomState.getStateEvents(EventType.RoomEncryption)?.[0],
(event: MatrixEvent) => {
if (room && event.getType() === EventType.RoomEncryption) {
setIsEncrypted(cli.isRoomEncrypted(room.roomId));
}
},
[cli, room],
); );
useTypedEventEmitter(room?.currentState, RoomStateEvent.Events, update); return useAsyncMemo(
async () => {
const crypto = cli.getCrypto();
if (!room || !crypto) return null;
return isEncrypted; return crypto.isEncryptionEnabledInRoom(room.roomId);
},
[room, encryptionStateEvent],
null,
);
} }

View file

@ -23,7 +23,7 @@ import * as settingsHooks from "../../../../../src/hooks/useSettings";
import Modal from "../../../../../src/Modal"; import Modal from "../../../../../src/Modal";
import RightPanelStore from "../../../../../src/stores/right-panel/RightPanelStore"; import RightPanelStore from "../../../../../src/stores/right-panel/RightPanelStore";
import { RightPanelPhases } from "../../../../../src/stores/right-panel/RightPanelStorePhases"; import { RightPanelPhases } from "../../../../../src/stores/right-panel/RightPanelStorePhases";
import { flushPromises, getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../../test-utils"; import { flushPromises, stubClient } from "../../../../test-utils";
import { PollHistoryDialog } from "../../../../../src/components/views/dialogs/PollHistoryDialog"; import { PollHistoryDialog } from "../../../../../src/components/views/dialogs/PollHistoryDialog";
import { RoomPermalinkCreator } from "../../../../../src/utils/permalinks/Permalinks"; import { RoomPermalinkCreator } from "../../../../../src/utils/permalinks/Permalinks";
import { _t } from "../../../../../src/languageHandler"; import { _t } from "../../../../../src/languageHandler";
@ -56,16 +56,7 @@ describe("<RoomSummaryCard />", () => {
}; };
beforeEach(() => { beforeEach(() => {
mockClient = getMockClientWithEventEmitter({ mockClient = mocked(stubClient());
...mockClientMethodsUser(userId),
getAccountData: jest.fn(),
isRoomEncrypted: jest.fn(),
getOrCreateFilter: jest.fn().mockResolvedValue({ filterId: 1 }),
getRoom: jest.fn(),
isGuest: jest.fn().mockReturnValue(false),
deleteRoomTag: jest.fn().mockResolvedValue({}),
setRoomTag: jest.fn().mockResolvedValue({}),
});
room = new Room(roomId, mockClient, userId); room = new Room(roomId, mockClient, userId);
const roomCreateEvent = new MatrixEvent({ const roomCreateEvent = new MatrixEvent({
type: "m.room.create", type: "m.room.create",

View file

@ -134,6 +134,7 @@ beforeEach(() => {
getUserDeviceInfo: jest.fn(), getUserDeviceInfo: jest.fn(),
userHasCrossSigningKeys: jest.fn().mockResolvedValue(false), userHasCrossSigningKeys: jest.fn().mockResolvedValue(false),
getUserVerificationStatus: jest.fn(), getUserVerificationStatus: jest.fn(),
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
} as unknown as CryptoApi); } as unknown as CryptoApi);
mockClient = mocked({ mockClient = mocked({
@ -148,7 +149,6 @@ beforeEach(() => {
on: jest.fn(), on: jest.fn(),
off: jest.fn(), off: jest.fn(),
isSynapseAdministrator: jest.fn().mockResolvedValue(false), isSynapseAdministrator: jest.fn().mockResolvedValue(false),
isRoomEncrypted: jest.fn().mockReturnValue(false),
doesServerSupportUnstableFeature: jest.fn().mockReturnValue(false), doesServerSupportUnstableFeature: jest.fn().mockReturnValue(false),
doesServerSupportExtendedProfiles: jest.fn().mockResolvedValue(false), doesServerSupportExtendedProfiles: jest.fn().mockResolvedValue(false),
getExtendedProfileProperty: jest.fn().mockRejectedValue(new Error("Not supported")), getExtendedProfileProperty: jest.fn().mockRejectedValue(new Error("Not supported")),
@ -660,7 +660,7 @@ describe("<UserInfo />", () => {
describe("with an encrypted room", () => { describe("with an encrypted room", () => {
beforeEach(() => { beforeEach(() => {
mockClient.isRoomEncrypted.mockReturnValue(true); jest.spyOn(mockClient.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
}); });
it("renders unverified user info", async () => { it("renders unverified user info", async () => {