use Crypto API to determine if room is encrypted

pull/28211/head
Hubert Chathi 2024-11-06 20:02:35 -05:00
parent 4e8a990887
commit d2b244f972
2 changed files with 12 additions and 10 deletions

View File

@ -125,6 +125,12 @@ export const UserIdentityWarning: React.FC<UserIdentityWarningProps> = ({ room }
if (!crypto || initialisedRef.current) { if (!crypto || initialisedRef.current) {
return; 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; initialisedRef.current = true;
const gotVerificationStatusUpdate = gotVerificationStatusUpdateRef.current; const gotVerificationStatusUpdate = gotVerificationStatusUpdateRef.current;
@ -151,14 +157,9 @@ export const UserIdentityWarning: React.FC<UserIdentityWarningProps> = ({ room }
selectCurrentPrompt(); selectCurrentPrompt();
}, [crypto, room, initialisedRef, gotVerificationStatusUpdateRef, membersNeedingApprovalRef, 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) => { loadMembers().catch((e) => {
logger.error("Error initialising UserIdentityWarning:", e); logger.error("Error initialising UserIdentityWarning:", e);
}); });
}
// When a user's verification status changes, we check if they need to be // When a user's verification status changes, we check if they need to be
// added/removed from the set of members needing approval. // added/removed from the set of members needing approval.

View File

@ -32,7 +32,6 @@ function mockRoom(): Room {
getMember: jest.fn((userId) => {}), getMember: jest.fn((userId) => {}),
roomId: ROOM_ID, roomId: ROOM_ID,
shouldEncryptForInvitedMembers: jest.fn(() => true), shouldEncryptForInvitedMembers: jest.fn(() => true),
hasEncryptionStateEvent: jest.fn(() => true),
} as unknown as Room; } as unknown as Room;
return room; return room;
@ -80,6 +79,7 @@ describe("UserIdentityWarning", () => {
beforeEach(async () => { beforeEach(async () => {
client = stubClient(); client = stubClient();
room = mockRoom(); room = mockRoom();
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
}); });
afterEach(() => { afterEach(() => {
@ -117,8 +117,8 @@ describe("UserIdentityWarning", () => {
mockRoomMember("@alice:example.org", "Alice"), mockRoomMember("@alice:example.org", "Alice"),
]); ]);
// Start the room off unencrypted. We shouldn't display anything. // Start the room off unencrypted. We shouldn't display anything.
jest.spyOn(room, "hasEncryptionStateEvent").mockReturnValue(false);
const crypto = client.getCrypto()!; const crypto = client.getCrypto()!;
jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockResolvedValue(false);
jest.spyOn(crypto, "getUserVerificationStatus").mockResolvedValue( jest.spyOn(crypto, "getUserVerificationStatus").mockResolvedValue(
new UserVerificationStatus(false, false, false, true), 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 // Encryption gets enabled in the room. We should now warn that Alice's
// identity changed. // identity changed.
jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockResolvedValue(true);
client.emit( client.emit(
RoomStateEvent.Events, RoomStateEvent.Events,
new MatrixEvent({ new MatrixEvent({