Skip to content

Commit dc965c3

Browse files
committed
Replace MatrixClient.isRoomEncrypted by MatrixClient.CryptoApi.isEncryptionEnabledInRoom in EventTile.tsx
1 parent b907ec3 commit dc965c3

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/components/views/rooms/EventTile.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React, { createRef, forwardRef, JSX, MouseEvent, ReactNode } from "react"
1111
import classNames from "classnames";
1212
import {
1313
EventStatus,
14+
EventTimeline,
1415
EventType,
1516
MatrixEvent,
1617
MatrixEventEvent,
@@ -21,6 +22,7 @@ import {
2122
Room,
2223
RoomEvent,
2324
RoomMember,
25+
RoomStateEvent,
2426
Thread,
2527
ThreadEvent,
2628
} from "matrix-js-sdk/src/matrix";
@@ -262,6 +264,10 @@ interface IState {
262264

263265
thread: Thread | null;
264266
threadNotification?: NotificationCountType;
267+
/**
268+
* Whether the event is encrypted.
269+
*/
270+
isRoomEncrypted: boolean;
265271
}
266272

267273
/**
@@ -318,6 +324,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
318324
hover: false,
319325

320326
thread,
327+
isRoomEncrypted: false,
321328
};
322329

323330
// don't do RR animations until we are mounted
@@ -386,7 +393,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
386393
return true;
387394
}
388395

389-
public componentDidMount(): void {
396+
public async componentDidMount(): Promise<void> {
390397
this.unmounted = false;
391398
this.suppressReadReceiptAnimation = false;
392399
const client = MatrixClientPeg.safeGet();
@@ -413,6 +420,12 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
413420
room?.on(ThreadEvent.New, this.onNewThread);
414421

415422
this.verifyEvent();
423+
424+
room?.getLiveTimeline().getState(EventTimeline.FORWARDS)?.on(RoomStateEvent.Events, this.onRoomStateEvents);
425+
426+
const crypto = client.getCrypto();
427+
if (!room || !crypto) return;
428+
this.setState({ isRoomEncrypted: await crypto.isEncryptionEnabledInRoom(room.roomId) });
416429
}
417430

418431
private updateThread = (thread: Thread): void => {
@@ -434,6 +447,10 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
434447
client.removeListener(RoomEvent.Receipt, this.onRoomReceipt);
435448
const room = client.getRoom(this.props.mxEvent.getRoomId());
436449
room?.off(ThreadEvent.New, this.onNewThread);
450+
room
451+
?.getLiveTimeline()
452+
.getState(EventTimeline.FORWARDS)
453+
?.off(RoomStateEvent.Events, this.onRoomStateEvents);
437454
}
438455
this.isListeningForReceipts = false;
439456
this.props.mxEvent.removeListener(MatrixEventEvent.Decrypted, this.onDecrypted);
@@ -470,6 +487,17 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
470487
}
471488
};
472489

490+
private onRoomStateEvents = async (evt: MatrixEvent): Promise<void> => {
491+
const client = MatrixClientPeg.safeGet();
492+
const crypto = client.getCrypto();
493+
if (!crypto) return;
494+
495+
const room = client.getRoom(evt.getRoomId());
496+
if (room && evt.getType() === EventType.RoomEncryption) {
497+
this.setState({ isRoomEncrypted: await crypto.isEncryptionEnabledInRoom(room.roomId) });
498+
}
499+
};
500+
473501
private get thread(): Thread | null {
474502
let thread: Thread | undefined = this.props.mxEvent.getThread();
475503
/**
@@ -767,7 +795,8 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
767795
}
768796
}
769797

770-
if (MatrixClientPeg.safeGet().isRoomEncrypted(ev.getRoomId()!)) {
798+
console.log("this.state.isRoomEncrypted", this.state.isRoomEncrypted);
799+
if (this.state.isRoomEncrypted) {
771800
// else if room is encrypted
772801
// and event is being encrypted or is not_sent (Unknown Devices/Network Error)
773802
if (ev.status === EventStatus.ENCRYPTING) {
@@ -783,6 +812,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
783812
return null; // we expect this to be unencrypted
784813
}
785814
if (!ev.isEncrypted()) {
815+
console.log("pad lock unencrypted");
786816
// if the event is not encrypted, but it's an e2e room, show a warning
787817
return <E2ePadlockUnencrypted />;
788818
}

test/test-utils/test-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export function createTestClient(): MatrixClient {
135135
restoreKeyBackupWithPassphrase: jest.fn(),
136136
loadSessionBackupPrivateKeyFromSecretStorage: jest.fn(),
137137
storeSessionBackupPrivateKey: jest.fn(),
138+
getEncryptionInfoForEvent: jest.fn().mockResolvedValue(null),
138139
}),
139140

140141
getPushActionsForEvent: jest.fn(),

test/unit-tests/components/views/dialogs/ForwardDialog-test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
mkEvent,
3333
mkMessage,
3434
mkStubRoom,
35+
mockClientMethodsCrypto,
3536
mockPlatformPeg,
3637
} from "../../../../test-utils";
3738
import { TILE_SERVER_WK_KEY } from "../../../../../src/utils/WellKnownUtils";
@@ -67,7 +68,6 @@ describe("ForwardDialog", () => {
6768
getAccountData: jest.fn().mockReturnValue(accountDataEvent),
6869
getPushActionsForEvent: jest.fn(),
6970
mxcUrlToHttp: jest.fn().mockReturnValue(""),
70-
isRoomEncrypted: jest.fn().mockReturnValue(false),
7171
getProfileInfo: jest.fn().mockResolvedValue({
7272
displayname: "Alice",
7373
}),
@@ -76,6 +76,7 @@ describe("ForwardDialog", () => {
7676
getClientWellKnown: jest.fn().mockReturnValue({
7777
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
7878
}),
79+
...mockClientMethodsCrypto(),
7980
});
8081
const defaultRooms = ["a", "A", "b"].map((name) => mkStubRoom(name, name, mockClient));
8182

test/unit-tests/components/views/rooms/EventTile-test.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import * as React from "react";
1010
import { act, fireEvent, render, screen, waitFor } from "jest-matrix-react";
1111
import { mocked } from "jest-mock";
1212
import {
13+
EventTimeline,
1314
EventType,
1415
IEventDecryptionResult,
1516
MatrixClient,
1617
MatrixEvent,
1718
NotificationCountType,
1819
PendingEventOrdering,
1920
Room,
21+
RoomStateEvent,
2022
TweakName,
2123
} from "matrix-js-sdk/src/matrix";
2224
import {
@@ -243,6 +245,7 @@ describe("EventTile", () => {
243245
const mockCrypto = {
244246
// a mocked version of getEncryptionInfoForEvent which will pick its result from `eventToEncryptionInfoMap`
245247
getEncryptionInfoForEvent: async (event: MatrixEvent) => eventToEncryptionInfoMap.get(event.getId()!)!,
248+
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
246249
} as unknown as CryptoApi;
247250
client.getCrypto = () => mockCrypto;
248251
});
@@ -434,7 +437,7 @@ describe("EventTile", () => {
434437
});
435438

436439
it("should update the warning when the event is replaced with an unencrypted one", async () => {
437-
jest.spyOn(client, "isRoomEncrypted").mockReturnValue(true);
440+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
438441

439442
// we start out with an event from the trusted device
440443
mxEvent = await mkEncryptedMatrixEvent({
@@ -578,4 +581,27 @@ describe("EventTile", () => {
578581
});
579582
});
580583
});
584+
585+
it("should display the not encrypted status for an unencrypted event when the room becomes encrypted", async () => {
586+
jest.spyOn(client.getCrypto()!, "getEncryptionInfoForEvent").mockResolvedValue({
587+
shieldColour: EventShieldColour.NONE,
588+
shieldReason: null,
589+
});
590+
591+
getComponent();
592+
await flushPromises();
593+
// The room and the event are unencrypted, the tile should not show the not encrypted status
594+
expect(screen.queryByText("Not encrypted")).toBeNull();
595+
596+
// The room is now encrypted
597+
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
598+
// Emit a state event to trigger a re-render
599+
const roomState = room!.getLiveTimeline().getState(EventTimeline.FORWARDS)!;
600+
act(() => {
601+
roomState.emit(RoomStateEvent.Events, new MatrixEvent({ type: EventType.RoomEncryption }), roomState, null);
602+
});
603+
604+
// The event tile should now show the not encrypted status
605+
await waitFor(() => expect(screen.getByText("Not encrypted")).toBeInTheDocument());
606+
});
581607
});

0 commit comments

Comments
 (0)