Skip to content

Commit 11ddcc1

Browse files
committed
Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in RoomView
1 parent 83777a6 commit 11ddcc1

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

Diff for: src/components/structures/RoomView.tsx

+20-5
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ export interface IRoomState {
233233
liveTimeline?: EventTimeline;
234234
narrow: boolean;
235235
msc3946ProcessDynamicPredecessor: boolean;
236+
/**
237+
* Wether the room is encrypted or not.
238+
*/
239+
isRoomEncrypted: boolean;
236240

237241
canAskToJoin: boolean;
238242
promptAskToJoin: boolean;
@@ -417,6 +421,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
417421
canAskToJoin: this.askToJoinEnabled,
418422
promptAskToJoin: false,
419423
viewRoomOpts: { buttons: [] },
424+
isRoomEncrypted: false,
420425
};
421426

422427
this.dispatcherRef = dis.register(this.onAction);
@@ -903,13 +908,17 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
903908
return isManuallyShown && widgets.length > 0;
904909
}
905910

906-
public componentDidMount(): void {
911+
public async componentDidMount(): Promise<void> {
907912
this.onRoomViewStoreUpdate(true);
908913

914+
const isRoomEncrypted = Boolean(
915+
this.state.roomId && (await this.context.client?.getCrypto()?.isEncryptionEnabledInRoom(this.state.roomId)),
916+
);
909917
const call = this.getCallForRoom();
910918
const callState = call?.state;
911919
this.setState({
912920
callState,
921+
isRoomEncrypted,
913922
});
914923

915924
this.context.legacyCallHandler.on(LegacyCallHandlerEvent.CallState, this.onCallState);
@@ -1405,10 +1414,12 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
14051414
});
14061415
}
14071416

1408-
private updatePreviewUrlVisibility({ roomId }: Room): void {
1417+
private async updatePreviewUrlVisibility({ roomId }: Room): Promise<void> {
1418+
const isRoomEncrypted = Boolean(await this.context.client?.getCrypto()?.isEncryptionEnabledInRoom(roomId));
14091419
// URL Previews in E2EE rooms can be a privacy leak so use a different setting which is per-room explicit
1410-
const key = this.context.client?.isRoomEncrypted(roomId) ? "urlPreviewsEnabled_e2ee" : "urlPreviewsEnabled";
1420+
const key = isRoomEncrypted ? "urlPreviewsEnabled_e2ee" : "urlPreviewsEnabled";
14111421
this.setState({
1422+
isRoomEncrypted,
14121423
showUrlPreview: SettingsStore.getValue(key, roomId),
14131424
});
14141425
}
@@ -1452,7 +1463,11 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
14521463
};
14531464

14541465
private async updateE2EStatus(room: Room): Promise<void> {
1455-
if (!this.context.client?.isRoomEncrypted(room.roomId)) return;
1466+
if (!this.context.client) return;
1467+
1468+
const isRoomEncrypted = Boolean(await this.context.client.getCrypto()?.isEncryptionEnabledInRoom(room.roomId));
1469+
this.setState({ isRoomEncrypted });
1470+
if (!isRoomEncrypted) return;
14561471

14571472
// If crypto is not currently enabled, we aren't tracking devices at all,
14581473
// so we don't know what the answer is. Let's error on the safe side and show
@@ -2243,7 +2258,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
22432258
searchInfo={this.state.search}
22442259
onCancelClick={this.onCancelSearchClick}
22452260
onSearchScopeChange={this.onSearchScopeChange}
2246-
isRoomEncrypted={this.context.client.isRoomEncrypted(this.state.room.roomId)}
2261+
isRoomEncrypted={this.state.isRoomEncrypted}
22472262
/>
22482263
);
22492264
} else if (showRoomUpgradeBar) {

Diff for: test/test-utils/test-utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function createTestClient(): MatrixClient {
117117

118118
getCrypto: jest.fn().mockReturnValue({
119119
getOwnDeviceKeys: jest.fn(),
120-
getUserDeviceInfo: jest.fn(),
120+
getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()),
121121
getUserVerificationStatus: jest.fn(),
122122
getDeviceVerificationStatus: jest.fn(),
123123
resetKeyBackup: jest.fn(),

Diff for: test/unit-tests/components/structures/RoomView-test.tsx

+15-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
SearchResult,
2222
IEvent,
2323
} from "matrix-js-sdk/src/matrix";
24+
import { CryptoApi, UserVerificationStatus } from "matrix-js-sdk/src/crypto-api";
2425
import { KnownMembership } from "matrix-js-sdk/src/types";
2526
import { fireEvent, render, screen, RenderResult, waitForElementToBeRemoved, waitFor } from "jest-matrix-react";
2627
import userEvent from "@testing-library/user-event";
@@ -72,14 +73,14 @@ describe("RoomView", () => {
7273
let rooms: Map<string, Room>;
7374
let roomCount = 0;
7475
let stores: SdkContextClass;
76+
let cryptoApi: CryptoApi;
7577

7678
// mute some noise
7779
filterConsole("RVS update", "does not have an m.room.create event", "Current version: 1", "Version capability");
7880

7981
beforeEach(() => {
8082
mockPlatformPeg({ reload: () => {} });
81-
stubClient();
82-
cli = mocked(MatrixClientPeg.safeGet());
83+
cli = mocked(stubClient());
8384

8485
room = new Room(`!${roomCount++}:example.org`, cli, "@alice:example.org");
8586
jest.spyOn(room, "findPredecessor");
@@ -97,6 +98,7 @@ describe("RoomView", () => {
9798
stores.rightPanelStore.useUnitTestClient(cli);
9899

99100
jest.spyOn(VoipUserMapper.sharedInstance(), "getVirtualRoomForRoom").mockResolvedValue(undefined);
101+
cryptoApi = cli.getCrypto()!;
100102
jest.spyOn(cli, "getCrypto").mockReturnValue(undefined);
101103
});
102104

@@ -230,8 +232,9 @@ describe("RoomView", () => {
230232

231233
it("updates url preview visibility on encryption state change", async () => {
232234
room.getMyMembership = jest.fn().mockReturnValue(KnownMembership.Join);
235+
jest.spyOn(cli, "getCrypto").mockReturnValue(cryptoApi);
233236
// we should be starting unencrypted
234-
expect(cli.isRoomEncrypted(room.roomId)).toEqual(false);
237+
expect(await cli.getCrypto()?.isEncryptionEnabledInRoom(room.roomId)).toEqual(false);
235238

236239
const roomViewInstance = await getRoomViewInstance();
237240

@@ -246,10 +249,10 @@ describe("RoomView", () => {
246249
expect(roomViewInstance.state.showUrlPreview).toBe(true);
247250

248251
// now enable encryption
249-
cli.isRoomEncrypted.mockReturnValue(true);
252+
jest.spyOn(cli.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
250253

251254
// and fake an encryption event into the room to prompt it to re-check
252-
room.addLiveEvents([
255+
await room.addLiveEvents([
253256
new MatrixEvent({
254257
type: "m.room.encryption",
255258
sender: cli.getUserId()!,
@@ -341,6 +344,11 @@ describe("RoomView", () => {
341344

342345
describe("that is encrypted", () => {
343346
beforeEach(() => {
347+
jest.spyOn(cli, "getCrypto").mockReturnValue(cryptoApi);
348+
jest.spyOn(cli.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
349+
jest.spyOn(cli.getCrypto()!, "getUserVerificationStatus").mockResolvedValue(
350+
new UserVerificationStatus(false, true, false),
351+
);
344352
mocked(cli.isRoomEncrypted).mockReturnValue(true);
345353
localRoom.encrypted = true;
346354
localRoom.currentState.setStateEvents([
@@ -402,7 +410,8 @@ describe("RoomView", () => {
402410
]);
403411
jest.spyOn(DMRoomMap.shared(), "getUserIdForRoomId").mockReturnValue(cli.getSafeUserId());
404412
jest.spyOn(DMRoomMap.shared(), "getRoomIds").mockReturnValue(new Set([room.roomId]));
405-
mocked(cli).isRoomEncrypted.mockReturnValue(true);
413+
jest.spyOn(cli, "getCrypto").mockReturnValue(cryptoApi);
414+
jest.spyOn(cli.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
406415
await renderRoomView();
407416
});
408417

0 commit comments

Comments
 (0)