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

Commit 2cf4fa6

Browse files
committed
Fix detection of encryption for all users in a room
1 parent 567248d commit 2cf4fa6

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

src/createRoom.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,21 @@ export default async function createRoom(opts: IOpts): Promise<string | null> {
398398
export async function canEncryptToAllUsers(client: MatrixClient, userIds: string[]): Promise<boolean> {
399399
try {
400400
const usersDeviceMap = await client.downloadKeys(userIds);
401-
// { "@user:host": { "DEVICE": {...}, ... }, ... }
402-
return Object.values(usersDeviceMap).every(
403-
(userDevices) =>
404-
// { "DEVICE": {...}, ... }
405-
Object.keys(userDevices).length > 0,
406-
);
401+
402+
// There are not keys at all.
403+
if (usersDeviceMap.size === 0) return false;
404+
405+
for (const devices of usersDeviceMap.values()) {
406+
if (devices.size === 0) {
407+
return false;
408+
}
409+
}
407410
} catch (e) {
408411
logger.error("Error determining if it's possible to encrypt to all users: ", e);
409412
return false; // assume not
410413
}
414+
415+
return true;
411416
}
412417

413418
// Similar to ensureDMExists but also adds creation content

test/createRoom-test.ts

+44-23
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,56 @@ describe("createRoom", () => {
147147
});
148148

149149
describe("canEncryptToAllUsers", () => {
150-
const trueUser = new Map([
151-
[
152-
"@goodUser:localhost",
153-
new Map([
154-
["DEV1", {} as unknown as DeviceInfo],
155-
["DEV2", {} as unknown as DeviceInfo],
156-
]),
157-
],
158-
]);
150+
const user1Id = "@user1:example.com";
151+
const user2Id = "@user2:example.com";
159152

160-
const falseUser = {
161-
"@badUser:localhost": {},
162-
};
153+
const devices = new Map([
154+
["DEV1", {} as unknown as DeviceInfo],
155+
["DEV2", {} as unknown as DeviceInfo],
156+
]);
163157

164158
let client: Mocked<MatrixClient>;
165-
beforeEach(() => {
166-
stubClient();
167-
client = mocked(MatrixClientPeg.get());
159+
160+
beforeAll(() => {
161+
client = mocked(stubClient());
168162
});
169163

170-
it("returns true if all devices have crypto", async () => {
171-
client.downloadKeys.mockResolvedValue(trueUser);
172-
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost"]);
173-
expect(response).toBe(true);
164+
it("should return false if download keys does not return any user", async () => {
165+
client.downloadKeys.mockResolvedValue(new Map());
166+
const result = await canEncryptToAllUsers(client, [user1Id, user2Id]);
167+
expect(result).toBe(false);
174168
});
175169

176-
it("returns false if not all users have crypto", async () => {
177-
client.downloadKeys.mockResolvedValue({ ...trueUser, ...falseUser });
178-
const response = await canEncryptToAllUsers(client, ["@goodUser:localhost", "@badUser:localhost"]);
179-
expect(response).toBe(false);
170+
it("should return false if none of the users has a device", async () => {
171+
client.downloadKeys.mockResolvedValue(
172+
new Map([
173+
[user1Id, new Map()],
174+
[user2Id, new Map()],
175+
]),
176+
);
177+
const result = await canEncryptToAllUsers(client, [user1Id, user2Id]);
178+
expect(result).toBe(false);
179+
});
180+
181+
it("should return false if some of the users don't have a device", async () => {
182+
client.downloadKeys.mockResolvedValue(
183+
new Map([
184+
[user1Id, new Map()],
185+
[user2Id, devices],
186+
]),
187+
);
188+
const result = await canEncryptToAllUsers(client, [user1Id, user2Id]);
189+
expect(result).toBe(false);
190+
});
191+
192+
it("should return true if all users have a device", async () => {
193+
client.downloadKeys.mockResolvedValue(
194+
new Map([
195+
[user1Id, devices],
196+
[user2Id, devices],
197+
]),
198+
);
199+
const result = await canEncryptToAllUsers(client, [user1Id, user2Id]);
200+
expect(result).toBe(true);
180201
});
181202
});

0 commit comments

Comments
 (0)