Skip to content

Commit 22e6e77

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

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/components/views/rooms/EventTile.tsx

+26-2
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 => {
@@ -470,6 +483,17 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
470483
}
471484
};
472485

486+
private onRoomStateEvents = async (evt: MatrixEvent): Promise<void> => {
487+
const client = MatrixClientPeg.safeGet();
488+
const crypto = client.getCrypto();
489+
if (!crypto) return;
490+
491+
const room = client.getRoom(evt.getRoomId());
492+
if (room && evt.getType() === EventType.RoomEncryption) {
493+
this.setState({ isRoomEncrypted: await crypto.isEncryptionEnabledInRoom(room.roomId) });
494+
}
495+
};
496+
473497
private get thread(): Thread | null {
474498
let thread: Thread | undefined = this.props.mxEvent.getThread();
475499
/**
@@ -767,7 +791,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
767791
}
768792
}
769793

770-
if (MatrixClientPeg.safeGet().isRoomEncrypted(ev.getRoomId()!)) {
794+
if (this.state.isRoomEncrypted) {
771795
// else if room is encrypted
772796
// and event is being encrypted or is not_sent (Unknown Devices/Network Error)
773797
if (ev.status === EventStatus.ENCRYPTING) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ describe("EventTile", () => {
243243
const mockCrypto = {
244244
// a mocked version of getEncryptionInfoForEvent which will pick its result from `eventToEncryptionInfoMap`
245245
getEncryptionInfoForEvent: async (event: MatrixEvent) => eventToEncryptionInfoMap.get(event.getId()!)!,
246+
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
246247
} as unknown as CryptoApi;
247248
client.getCrypto = () => mockCrypto;
248249
});
@@ -434,7 +435,7 @@ describe("EventTile", () => {
434435
});
435436

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

439440
// we start out with an event from the trusted device
440441
mxEvent = await mkEncryptedMatrixEvent({

0 commit comments

Comments
 (0)