From b03dc6ac43a5a9bc478603cf710ac7e7657aea3f Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Mon, 11 Dec 2023 10:30:27 +0000 Subject: [PATCH] Move roomList out of MatrixClient, into legacy Crypto (#3944) * Comment explaining the purpose of RoomList * Fix incorrect return type declaration on RoomList.getRoomEncryption * Move RoomList out of MatrixClient, into legacy Crypto * Initialise RoomList inside Crypto.init to allow us to await it --- spec/unit/crypto.spec.ts | 24 ++++-------------------- src/client.ts | 25 ++++--------------------- src/crypto/RoomList.ts | 9 ++++++++- src/crypto/index.ts | 27 +++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/spec/unit/crypto.spec.ts b/spec/unit/crypto.spec.ts index bb21efd34..ac1eec9d2 100644 --- a/spec/unit/crypto.spec.ts +++ b/spec/unit/crypto.spec.ts @@ -356,7 +356,6 @@ describe("Crypto", function () { let crypto: Crypto; let mockBaseApis: MatrixClient; - let mockRoomList: RoomList; let fakeEmitter: EventEmitter; @@ -390,19 +389,10 @@ describe("Crypto", function () { isGuest: jest.fn(), emit: jest.fn(), } as unknown as MatrixClient; - mockRoomList = {} as unknown as RoomList; fakeEmitter = new EventEmitter(); - crypto = new Crypto( - mockBaseApis, - "@alice:home.server", - "FLIBBLE", - clientStore, - cryptoStore, - mockRoomList, - [], - ); + crypto = new Crypto(mockBaseApis, "@alice:home.server", "FLIBBLE", clientStore, cryptoStore, []); crypto.registerEventHandlers(fakeEmitter as any); await crypto.init(); }); @@ -1341,15 +1331,9 @@ describe("Crypto", function () { setRoomEncryption: jest.fn().mockResolvedValue(undefined), } as unknown as RoomList; - crypto = new Crypto( - mockClient, - "@alice:home.server", - "FLIBBLE", - clientStore, - cryptoStore, - mockRoomList, - [], - ); + crypto = new Crypto(mockClient, "@alice:home.server", "FLIBBLE", clientStore, cryptoStore, []); + // @ts-ignore we are injecting a mock into a private property + crypto.roomList = mockRoomList; }); it("should set the algorithm if called for a known room", async () => { diff --git a/src/client.ts b/src/client.ts index 7225af05a..9ef4550d4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -51,7 +51,7 @@ import { decodeBase64, encodeBase64 } from "./base64"; import { IExportedDevice as IExportedOlmDevice } from "./crypto/OlmDevice"; import { IOlmDevice } from "./crypto/algorithms/megolm"; import { TypedReEmitter } from "./ReEmitter"; -import { IRoomEncryption, RoomList } from "./crypto/RoomList"; +import { IRoomEncryption } from "./crypto/RoomList"; import { logger, Logger } from "./logger"; import { SERVICE_TYPES } from "./service-types"; import { @@ -1272,7 +1272,6 @@ export class MatrixClient extends TypedEventEmitter room e2e info object (body of the m.room.encryption event) private roomEncryption: Record = {}; @@ -43,7 +50,7 @@ export class RoomList { }); } - public getRoomEncryption(roomId: string): IRoomEncryption { + public getRoomEncryption(roomId: string): IRoomEncryption | null { return this.roomEncryption[roomId] || null; } diff --git a/src/crypto/index.ts b/src/crypto/index.ts index 2a3c371ac..1b3cc228c 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -64,7 +64,7 @@ import { IUploadKeySignaturesResponse, MatrixClient, } from "../client"; -import type { IRoomEncryption, RoomList } from "./RoomList"; +import { IRoomEncryption, RoomList } from "./RoomList"; import { IKeyBackupInfo } from "./keybackup"; import { ISyncStateData } from "../sync"; import { CryptoStore } from "./store/base"; @@ -385,6 +385,7 @@ export class Crypto extends TypedEventEmitter; private readonly verificationMethods: Map; public readonly supportedAlgorithms: string[]; @@ -473,10 +474,13 @@ export class Crypto extends TypedEventEmitter, ) { super(); + + logger.debug("Crypto: initialising roomlist..."); + this.roomList = new RoomList(cryptoStore); + this.reEmitter = new TypedReEmitter(this); if (verificationMethods) { @@ -626,6 +630,9 @@ export class Crypto extends TypedEventEmitter