Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
DeviceListener: replace calls to deprecated APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed May 12, 2023
1 parent d944422 commit 21daeed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/DeviceListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,16 @@ export default class DeviceListener {
// cross-signing support was added to Matrix in MSC1756, which landed in spec v1.1
if (!(await cli.isVersionSupported("v1.1"))) return;

if (!cli.isCryptoEnabled()) return;
const crypto = cli.getCrypto();
if (!crypto) return;

// don't recheck until the initial sync is complete: lots of account data events will fire
// while the initial sync is processing and we don't need to recheck on each one of them
// (we add a listener on sync to do once check after the initial sync is done)
if (!cli.isInitialSyncComplete()) return;

const crossSigningReady = await cli.isCrossSigningReady();
const secretStorageReady = await cli.isSecretStorageReady();
const crossSigningReady = await crypto.isCrossSigningReady();
const secretStorageReady = await crypto.isSecretStorageReady();
const allSystemsReady = crossSigningReady && secretStorageReady;

if (this.dismissedThisDeviceToast || allSystemsReady) {
Expand All @@ -283,7 +285,8 @@ export default class DeviceListener {
this.checkKeyBackupStatus();
} else if (this.shouldShowSetupEncryptionToast()) {
// make sure our keys are finished downloading
await cli.downloadKeys([cli.getUserId()!]);
await crypto.getUserDeviceInfo([cli.getUserId()!]);

// cross signing isn't enabled - nag to enable it
// There are 3 different toasts for:
if (!cli.getCrossSigningId() && cli.getStoredCrossSigningForUser(cli.getUserId()!)) {
Expand All @@ -310,7 +313,7 @@ export default class DeviceListener {
}
}

// This needs to be done after awaiting on downloadKeys() above, so
// This needs to be done after awaiting on getUserDeviceInfo() above, so
// we make sure we get the devices after the fetch is done.
await this.ensureDeviceIdsAtStartPopulated();

Expand All @@ -324,10 +327,7 @@ export default class DeviceListener {

const isCurrentDeviceTrusted =
crossSigningReady &&
Boolean(
(await cli.getCrypto()?.getDeviceVerificationStatus(cli.getUserId()!, cli.deviceId!))
?.crossSigningVerified,
);
Boolean((await crypto.getDeviceVerificationStatus(cli.getUserId()!, cli.deviceId!))?.crossSigningVerified);

// as long as cross-signing isn't ready,
// you can't see or dismiss any device toasts
Expand All @@ -336,7 +336,7 @@ export default class DeviceListener {
for (const deviceId of devices) {
if (deviceId === cli.deviceId) continue;

const deviceTrust = await cli.getCrypto()!.getDeviceVerificationStatus(cli.getUserId()!, deviceId);
const deviceTrust = await crypto.getDeviceVerificationStatus(cli.getUserId()!, deviceId);
if (!deviceTrust?.crossSigningVerified && !this.dismissed.has(deviceId)) {
if (this.ourDeviceIdsAtStart?.has(deviceId)) {
oldUnverifiedDeviceIds.add(deviceId);
Expand Down
12 changes: 5 additions & 7 deletions test/DeviceListener-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ describe("DeviceListener", () => {
crossSigningVerified: false,
}),
getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()),
isCrossSigningReady: jest.fn().mockResolvedValue(true),
isSecretStorageReady: jest.fn().mockResolvedValue(true),
} as unknown as Mocked<CryptoApi>;
mockClient = getMockClientWithEventEmitter({
isGuest: jest.fn(),
Expand All @@ -89,15 +91,11 @@ describe("DeviceListener", () => {
getKeyBackupVersion: jest.fn().mockResolvedValue(undefined),
getRooms: jest.fn().mockReturnValue([]),
isVersionSupported: jest.fn().mockResolvedValue(true),
isCrossSigningReady: jest.fn().mockResolvedValue(true),
isSecretStorageReady: jest.fn().mockResolvedValue(true),
isCryptoEnabled: jest.fn().mockReturnValue(true),
isInitialSyncComplete: jest.fn().mockReturnValue(true),
getKeyBackupEnabled: jest.fn(),
getCrossSigningId: jest.fn(),
getStoredCrossSigningForUser: jest.fn(),
waitForClientWellKnown: jest.fn(),
downloadKeys: jest.fn(),
isRoomEncrypted: jest.fn(),
getClientWellKnown: jest.fn(),
getDeviceId: jest.fn().mockReturnValue(deviceId),
Expand Down Expand Up @@ -288,15 +286,15 @@ describe("DeviceListener", () => {
mocked(isSecretStorageBeingAccessed).mockReturnValue(true);
await createAndStart();

expect(mockClient!.downloadKeys).not.toHaveBeenCalled();
expect(mockCrypto!.getUserDeviceInfo).not.toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled();
});

it("does not do any checks or show any toasts when no rooms are encrypted", async () => {
mockClient!.isRoomEncrypted.mockReturnValue(false);
await createAndStart();

expect(mockClient!.downloadKeys).not.toHaveBeenCalled();
expect(mockCrypto!.getUserDeviceInfo).not.toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled();
});

Expand All @@ -309,7 +307,7 @@ describe("DeviceListener", () => {
mockClient!.getStoredCrossSigningForUser.mockReturnValue(new CrossSigningInfo(userId));
await createAndStart();

expect(mockClient!.downloadKeys).toHaveBeenCalled();
expect(mockCrypto!.getUserDeviceInfo).toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).toHaveBeenCalledWith(
SetupEncryptionToast.Kind.VERIFY_THIS_SESSION,
);
Expand Down

0 comments on commit 21daeed

Please sign in to comment.