From 35742a423e5d55cd5b1161fe3cdb4a7b6a4f66f3 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 20 Apr 2023 12:08:01 +0200 Subject: [PATCH 1/4] Use `cli.getUserDeviceInfo` instead of `cli.downloadKeys` to create a room --- src/createRoom.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/createRoom.ts b/src/createRoom.ts index 2b5831498be..19afb52772b 100644 --- a/src/createRoom.ts +++ b/src/createRoom.ts @@ -397,7 +397,7 @@ export default async function createRoom(opts: IOpts): Promise { */ export async function canEncryptToAllUsers(client: MatrixClient, userIds: string[]): Promise { try { - const usersDeviceMap = await client.downloadKeys(userIds); + const usersDeviceMap = await client.getUserDeviceInfo(userIds, true); for (const devices of usersDeviceMap.values()) { if (devices.size === 0) { From 681bb204f3e499329aa61dc29686f4d5a6ad0e3b Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Fri, 21 Apr 2023 16:35:27 +0200 Subject: [PATCH 2/4] Use `client.getCrypto().getUserDeviceInfo` instead of `client.getUserDeviceInfo` --- src/createRoom.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/createRoom.ts b/src/createRoom.ts index 19afb52772b..cf090b06ca4 100644 --- a/src/createRoom.ts +++ b/src/createRoom.ts @@ -397,7 +397,10 @@ export default async function createRoom(opts: IOpts): Promise { */ export async function canEncryptToAllUsers(client: MatrixClient, userIds: string[]): Promise { try { - const usersDeviceMap = await client.getUserDeviceInfo(userIds, true); + const usersDeviceMap = await client.getCrypto()?.getUserDeviceInfo(userIds, true); + if (!usersDeviceMap) { + return false; + } for (const devices of usersDeviceMap.values()) { if (devices.size === 0) { From e19d684c1bc9e572c5d8bb5ae860655a477e10f8 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Fri, 21 Apr 2023 17:22:52 +0200 Subject: [PATCH 3/4] Update `createRoom-test.ts` to use `getUserDeviceInfo` --- test/createRoom-test.ts | 19 ++++++++++--------- test/test-utils/test-utils.ts | 1 + 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/test/createRoom-test.ts b/test/createRoom-test.ts index 0c34041b071..a9387bfcdfb 100644 --- a/test/createRoom-test.ts +++ b/test/createRoom-test.ts @@ -15,8 +15,7 @@ limitations under the License. */ import { mocked, Mocked } from "jest-mock"; -import { MatrixClient } from "matrix-js-sdk/src/matrix"; -import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; +import { CryptoApi, MatrixClient, Device } from "matrix-js-sdk/src/matrix"; import { RoomType } from "matrix-js-sdk/src/@types/event"; import { stubClient, setupAsyncStoreWithClient, mockPlatformPeg } from "./test-utils"; @@ -151,30 +150,32 @@ describe("canEncryptToAllUsers", () => { const user2Id = "@user2:example.com"; const devices = new Map([ - ["DEV1", {} as unknown as DeviceInfo], - ["DEV2", {} as unknown as DeviceInfo], + ["DEV1", {} as unknown as Device], + ["DEV2", {} as unknown as Device], ]); let client: Mocked; + let cryptoApi: Mocked; beforeAll(() => { client = mocked(stubClient()); + cryptoApi = mocked(client.getCrypto()!); }); it("should return true if userIds is empty", async () => { - client.downloadKeys.mockResolvedValue(new Map()); + cryptoApi.getUserDeviceInfo.mockResolvedValue(new Map()); const result = await canEncryptToAllUsers(client, []); expect(result).toBe(true); }); it("should return true if download keys does not return any user", async () => { - client.downloadKeys.mockResolvedValue(new Map()); + cryptoApi.getUserDeviceInfo.mockResolvedValue(new Map()); const result = await canEncryptToAllUsers(client, [user1Id, user2Id]); expect(result).toBe(true); }); it("should return false if none of the users has a device", async () => { - client.downloadKeys.mockResolvedValue( + cryptoApi.getUserDeviceInfo.mockResolvedValue( new Map([ [user1Id, new Map()], [user2Id, new Map()], @@ -185,7 +186,7 @@ describe("canEncryptToAllUsers", () => { }); it("should return false if some of the users don't have a device", async () => { - client.downloadKeys.mockResolvedValue( + cryptoApi.getUserDeviceInfo.mockResolvedValue( new Map([ [user1Id, new Map()], [user2Id, devices], @@ -196,7 +197,7 @@ describe("canEncryptToAllUsers", () => { }); it("should return true if all users have a device", async () => { - client.downloadKeys.mockResolvedValue( + cryptoApi.getUserDeviceInfo.mockResolvedValue( new Map([ [user1Id, devices], [user2Id, devices], diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index ddd6b091257..49a2dfc982e 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -121,6 +121,7 @@ export function createTestClient(): MatrixClient { downloadKeys: jest.fn(), }, }, + getCrypto: jest.fn().mockReturnValue({ getUserDeviceInfo: jest.fn() }), getPushActionsForEvent: jest.fn(), getRoom: jest.fn().mockImplementation((roomId) => mkStubRoom(roomId, "My room", client)), From d752d08d5461f9e557cc1f80ed7e34769dbe0c13 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Mon, 24 Apr 2023 15:52:36 +0200 Subject: [PATCH 4/4] Remove duplicate field --- test/test-utils/test-utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 80f9e30d533..1c190b46164 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -234,7 +234,6 @@ export function createTestClient(): MatrixClient { }), searchUserDirectory: jest.fn().mockResolvedValue({ limited: false, results: [] }), - getCrypto: jest.fn(), } as unknown as MatrixClient; client.reEmitter = new ReEmitter(client);