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

Element-R: Starting a DMs with a user #10673

Merged
merged 6 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/createRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,10 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
*/
export async function canEncryptToAllUsers(client: MatrixClient, userIds: string[]): Promise<boolean> {
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) {
Expand Down
19 changes: 10 additions & 9 deletions test/createRoom-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<MatrixClient>;
let cryptoApi: Mocked<CryptoApi>;

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()],
Expand All @@ -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],
Expand All @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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);
Expand Down