From d2b244f9725fb1f9489931afbdb32da7effbd286 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 6 Nov 2024 20:02:35 -0500 Subject: [PATCH] use Crypto API to determine if room is encrypted --- .../views/rooms/UserIdentityWarning.tsx | 17 +++++++++-------- .../views/rooms/UserIdentityWarning-test.tsx | 5 +++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/components/views/rooms/UserIdentityWarning.tsx b/src/components/views/rooms/UserIdentityWarning.tsx index 6e674d7ef8..17807339cc 100644 --- a/src/components/views/rooms/UserIdentityWarning.tsx +++ b/src/components/views/rooms/UserIdentityWarning.tsx @@ -125,6 +125,12 @@ export const UserIdentityWarning: React.FC = ({ room } if (!crypto || initialisedRef.current) { return; } + // If encryption is not enabled in the room, we don't need to do + // anything. If encryption gets enabled later, we will retry, via + // onRoomStateEvent. + if (!(await crypto.isEncryptionEnabledInRoom(room.roomId))) { + return; + } initialisedRef.current = true; const gotVerificationStatusUpdate = gotVerificationStatusUpdateRef.current; @@ -151,14 +157,9 @@ export const UserIdentityWarning: React.FC = ({ room } selectCurrentPrompt(); }, [crypto, room, initialisedRef, gotVerificationStatusUpdateRef, membersNeedingApprovalRef, selectCurrentPrompt]); - // If the room has encryption enabled, we load the room members right away. - // If not, we wait until encryption is enabled before loading the room - // members, since we don't need to display anything in unencrypted rooms. - if (crypto && room.hasEncryptionStateEvent()) { - loadMembers().catch((e) => { - logger.error("Error initialising UserIdentityWarning:", e); - }); - } + loadMembers().catch((e) => { + logger.error("Error initialising UserIdentityWarning:", e); + }); // When a user's verification status changes, we check if they need to be // added/removed from the set of members needing approval. diff --git a/test/unit-tests/components/views/rooms/UserIdentityWarning-test.tsx b/test/unit-tests/components/views/rooms/UserIdentityWarning-test.tsx index c02449a36d..52d082740e 100644 --- a/test/unit-tests/components/views/rooms/UserIdentityWarning-test.tsx +++ b/test/unit-tests/components/views/rooms/UserIdentityWarning-test.tsx @@ -32,7 +32,6 @@ function mockRoom(): Room { getMember: jest.fn((userId) => {}), roomId: ROOM_ID, shouldEncryptForInvitedMembers: jest.fn(() => true), - hasEncryptionStateEvent: jest.fn(() => true), } as unknown as Room; return room; @@ -80,6 +79,7 @@ describe("UserIdentityWarning", () => { beforeEach(async () => { client = stubClient(); room = mockRoom(); + jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true); }); afterEach(() => { @@ -117,8 +117,8 @@ describe("UserIdentityWarning", () => { mockRoomMember("@alice:example.org", "Alice"), ]); // Start the room off unencrypted. We shouldn't display anything. - jest.spyOn(room, "hasEncryptionStateEvent").mockReturnValue(false); const crypto = client.getCrypto()!; + jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockResolvedValue(false); jest.spyOn(crypto, "getUserVerificationStatus").mockResolvedValue( new UserVerificationStatus(false, false, false, true), ); @@ -129,6 +129,7 @@ describe("UserIdentityWarning", () => { // Encryption gets enabled in the room. We should now warn that Alice's // identity changed. + jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockResolvedValue(true); client.emit( RoomStateEvent.Events, new MatrixEvent({