diff --git a/src/createRoom.ts b/src/createRoom.ts index 2b5831498b..cf090b06ca 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.downloadKeys(userIds); + const usersDeviceMap = await client.getCrypto()?.getUserDeviceInfo(userIds, true); + if (!usersDeviceMap) { + return false; + } for (const devices of usersDeviceMap.values()) { if (devices.size === 0) { diff --git a/test/createRoom-test.ts b/test/createRoom-test.ts index 0c34041b07..a9387bfcdf 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 58668fa470..1c190b4616 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -120,6 +120,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)), @@ -233,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);