Replace MatrixClient.isRoomEncrypted
by MatrixClient.CryptoApi.isEncryptionEnabledInRoom
in EventTile.tsx
This commit is contained in:
parent
b907ec380f
commit
dc53178ea6
4 changed files with 60 additions and 4 deletions
|
@ -11,6 +11,7 @@ import React, { createRef, forwardRef, JSX, MouseEvent, ReactNode } from "react"
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import {
|
import {
|
||||||
EventStatus,
|
EventStatus,
|
||||||
|
EventTimeline,
|
||||||
EventType,
|
EventType,
|
||||||
MatrixEvent,
|
MatrixEvent,
|
||||||
MatrixEventEvent,
|
MatrixEventEvent,
|
||||||
|
@ -21,6 +22,7 @@ import {
|
||||||
Room,
|
Room,
|
||||||
RoomEvent,
|
RoomEvent,
|
||||||
RoomMember,
|
RoomMember,
|
||||||
|
RoomStateEvent,
|
||||||
Thread,
|
Thread,
|
||||||
ThreadEvent,
|
ThreadEvent,
|
||||||
} from "matrix-js-sdk/src/matrix";
|
} from "matrix-js-sdk/src/matrix";
|
||||||
|
@ -262,6 +264,10 @@ interface IState {
|
||||||
|
|
||||||
thread: Thread | null;
|
thread: Thread | null;
|
||||||
threadNotification?: NotificationCountType;
|
threadNotification?: NotificationCountType;
|
||||||
|
/**
|
||||||
|
* Whether the event is encrypted.
|
||||||
|
*/
|
||||||
|
isRoomEncrypted: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -318,6 +324,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||||
hover: false,
|
hover: false,
|
||||||
|
|
||||||
thread,
|
thread,
|
||||||
|
isRoomEncrypted: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
// don't do RR animations until we are mounted
|
// don't do RR animations until we are mounted
|
||||||
|
@ -386,7 +393,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public componentDidMount(): void {
|
public async componentDidMount(): Promise<void> {
|
||||||
this.unmounted = false;
|
this.unmounted = false;
|
||||||
this.suppressReadReceiptAnimation = false;
|
this.suppressReadReceiptAnimation = false;
|
||||||
const client = MatrixClientPeg.safeGet();
|
const client = MatrixClientPeg.safeGet();
|
||||||
|
@ -413,6 +420,12 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||||
room?.on(ThreadEvent.New, this.onNewThread);
|
room?.on(ThreadEvent.New, this.onNewThread);
|
||||||
|
|
||||||
this.verifyEvent();
|
this.verifyEvent();
|
||||||
|
|
||||||
|
room?.getLiveTimeline().getState(EventTimeline.FORWARDS)?.on(RoomStateEvent.Events, this.onRoomStateEvents);
|
||||||
|
|
||||||
|
const crypto = client.getCrypto();
|
||||||
|
if (!room || !crypto) return;
|
||||||
|
this.setState({ isRoomEncrypted: await crypto.isEncryptionEnabledInRoom(room.roomId) });
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateThread = (thread: Thread): void => {
|
private updateThread = (thread: Thread): void => {
|
||||||
|
@ -434,6 +447,10 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||||
client.removeListener(RoomEvent.Receipt, this.onRoomReceipt);
|
client.removeListener(RoomEvent.Receipt, this.onRoomReceipt);
|
||||||
const room = client.getRoom(this.props.mxEvent.getRoomId());
|
const room = client.getRoom(this.props.mxEvent.getRoomId());
|
||||||
room?.off(ThreadEvent.New, this.onNewThread);
|
room?.off(ThreadEvent.New, this.onNewThread);
|
||||||
|
room
|
||||||
|
?.getLiveTimeline()
|
||||||
|
.getState(EventTimeline.FORWARDS)
|
||||||
|
?.off(RoomStateEvent.Events, this.onRoomStateEvents);
|
||||||
}
|
}
|
||||||
this.isListeningForReceipts = false;
|
this.isListeningForReceipts = false;
|
||||||
this.props.mxEvent.removeListener(MatrixEventEvent.Decrypted, this.onDecrypted);
|
this.props.mxEvent.removeListener(MatrixEventEvent.Decrypted, this.onDecrypted);
|
||||||
|
@ -470,6 +487,17 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private onRoomStateEvents = async (evt: MatrixEvent): Promise<void> => {
|
||||||
|
const client = MatrixClientPeg.safeGet();
|
||||||
|
const crypto = client.getCrypto();
|
||||||
|
if (!crypto) return;
|
||||||
|
|
||||||
|
const room = client.getRoom(evt.getRoomId());
|
||||||
|
if (room && evt.getType() === EventType.RoomEncryption) {
|
||||||
|
this.setState({ isRoomEncrypted: await crypto.isEncryptionEnabledInRoom(room.roomId) });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private get thread(): Thread | null {
|
private get thread(): Thread | null {
|
||||||
let thread: Thread | undefined = this.props.mxEvent.getThread();
|
let thread: Thread | undefined = this.props.mxEvent.getThread();
|
||||||
/**
|
/**
|
||||||
|
@ -767,7 +795,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MatrixClientPeg.safeGet().isRoomEncrypted(ev.getRoomId()!)) {
|
if (this.state.isRoomEncrypted) {
|
||||||
// else if room is encrypted
|
// else if room is encrypted
|
||||||
// and event is being encrypted or is not_sent (Unknown Devices/Network Error)
|
// and event is being encrypted or is not_sent (Unknown Devices/Network Error)
|
||||||
if (ev.status === EventStatus.ENCRYPTING) {
|
if (ev.status === EventStatus.ENCRYPTING) {
|
||||||
|
|
|
@ -135,6 +135,7 @@ export function createTestClient(): MatrixClient {
|
||||||
restoreKeyBackupWithPassphrase: jest.fn(),
|
restoreKeyBackupWithPassphrase: jest.fn(),
|
||||||
loadSessionBackupPrivateKeyFromSecretStorage: jest.fn(),
|
loadSessionBackupPrivateKeyFromSecretStorage: jest.fn(),
|
||||||
storeSessionBackupPrivateKey: jest.fn(),
|
storeSessionBackupPrivateKey: jest.fn(),
|
||||||
|
getEncryptionInfoForEvent: jest.fn().mockResolvedValue(null),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getPushActionsForEvent: jest.fn(),
|
getPushActionsForEvent: jest.fn(),
|
||||||
|
|
|
@ -32,6 +32,7 @@ import {
|
||||||
mkEvent,
|
mkEvent,
|
||||||
mkMessage,
|
mkMessage,
|
||||||
mkStubRoom,
|
mkStubRoom,
|
||||||
|
mockClientMethodsCrypto,
|
||||||
mockPlatformPeg,
|
mockPlatformPeg,
|
||||||
} from "../../../../test-utils";
|
} from "../../../../test-utils";
|
||||||
import { TILE_SERVER_WK_KEY } from "../../../../../src/utils/WellKnownUtils";
|
import { TILE_SERVER_WK_KEY } from "../../../../../src/utils/WellKnownUtils";
|
||||||
|
@ -67,7 +68,6 @@ describe("ForwardDialog", () => {
|
||||||
getAccountData: jest.fn().mockReturnValue(accountDataEvent),
|
getAccountData: jest.fn().mockReturnValue(accountDataEvent),
|
||||||
getPushActionsForEvent: jest.fn(),
|
getPushActionsForEvent: jest.fn(),
|
||||||
mxcUrlToHttp: jest.fn().mockReturnValue(""),
|
mxcUrlToHttp: jest.fn().mockReturnValue(""),
|
||||||
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
|
||||||
getProfileInfo: jest.fn().mockResolvedValue({
|
getProfileInfo: jest.fn().mockResolvedValue({
|
||||||
displayname: "Alice",
|
displayname: "Alice",
|
||||||
}),
|
}),
|
||||||
|
@ -76,6 +76,7 @@ describe("ForwardDialog", () => {
|
||||||
getClientWellKnown: jest.fn().mockReturnValue({
|
getClientWellKnown: jest.fn().mockReturnValue({
|
||||||
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
||||||
}),
|
}),
|
||||||
|
...mockClientMethodsCrypto(),
|
||||||
});
|
});
|
||||||
const defaultRooms = ["a", "A", "b"].map((name) => mkStubRoom(name, name, mockClient));
|
const defaultRooms = ["a", "A", "b"].map((name) => mkStubRoom(name, name, mockClient));
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import * as React from "react";
|
||||||
import { act, fireEvent, render, screen, waitFor } from "jest-matrix-react";
|
import { act, fireEvent, render, screen, waitFor } from "jest-matrix-react";
|
||||||
import { mocked } from "jest-mock";
|
import { mocked } from "jest-mock";
|
||||||
import {
|
import {
|
||||||
|
EventTimeline,
|
||||||
EventType,
|
EventType,
|
||||||
IEventDecryptionResult,
|
IEventDecryptionResult,
|
||||||
MatrixClient,
|
MatrixClient,
|
||||||
|
@ -17,6 +18,7 @@ import {
|
||||||
NotificationCountType,
|
NotificationCountType,
|
||||||
PendingEventOrdering,
|
PendingEventOrdering,
|
||||||
Room,
|
Room,
|
||||||
|
RoomStateEvent,
|
||||||
TweakName,
|
TweakName,
|
||||||
} from "matrix-js-sdk/src/matrix";
|
} from "matrix-js-sdk/src/matrix";
|
||||||
import {
|
import {
|
||||||
|
@ -243,6 +245,7 @@ describe("EventTile", () => {
|
||||||
const mockCrypto = {
|
const mockCrypto = {
|
||||||
// a mocked version of getEncryptionInfoForEvent which will pick its result from `eventToEncryptionInfoMap`
|
// a mocked version of getEncryptionInfoForEvent which will pick its result from `eventToEncryptionInfoMap`
|
||||||
getEncryptionInfoForEvent: async (event: MatrixEvent) => eventToEncryptionInfoMap.get(event.getId()!)!,
|
getEncryptionInfoForEvent: async (event: MatrixEvent) => eventToEncryptionInfoMap.get(event.getId()!)!,
|
||||||
|
isEncryptionEnabledInRoom: jest.fn().mockResolvedValue(false),
|
||||||
} as unknown as CryptoApi;
|
} as unknown as CryptoApi;
|
||||||
client.getCrypto = () => mockCrypto;
|
client.getCrypto = () => mockCrypto;
|
||||||
});
|
});
|
||||||
|
@ -434,7 +437,7 @@ describe("EventTile", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should update the warning when the event is replaced with an unencrypted one", async () => {
|
it("should update the warning when the event is replaced with an unencrypted one", async () => {
|
||||||
jest.spyOn(client, "isRoomEncrypted").mockReturnValue(true);
|
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
|
||||||
|
|
||||||
// we start out with an event from the trusted device
|
// we start out with an event from the trusted device
|
||||||
mxEvent = await mkEncryptedMatrixEvent({
|
mxEvent = await mkEncryptedMatrixEvent({
|
||||||
|
@ -578,4 +581,27 @@ describe("EventTile", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should display the not encrypted status for an unencrypted event when the room becomes encrypted", async () => {
|
||||||
|
jest.spyOn(client.getCrypto()!, "getEncryptionInfoForEvent").mockResolvedValue({
|
||||||
|
shieldColour: EventShieldColour.NONE,
|
||||||
|
shieldReason: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
getComponent();
|
||||||
|
await flushPromises();
|
||||||
|
// The room and the event are unencrypted, the tile should not show the not encrypted status
|
||||||
|
expect(screen.queryByText("Not encrypted")).toBeNull();
|
||||||
|
|
||||||
|
// The room is now encrypted
|
||||||
|
jest.spyOn(client.getCrypto()!, "isEncryptionEnabledInRoom").mockResolvedValue(true);
|
||||||
|
// Emit a state event to trigger a re-render
|
||||||
|
const roomState = room!.getLiveTimeline().getState(EventTimeline.FORWARDS)!;
|
||||||
|
act(() => {
|
||||||
|
roomState.emit(RoomStateEvent.Events, new MatrixEvent({ type: EventType.RoomEncryption }), roomState, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
// The event tile should now show the not encrypted status
|
||||||
|
await waitFor(() => expect(screen.getByText("Not encrypted")).toBeInTheDocument());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue