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

Commit

Permalink
Use new crypto api to send to-device messages from widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Sep 11, 2024
1 parent b67a230 commit 1233d6a
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,33 +430,28 @@ export class StopGapWidgetDriver extends WidgetDriver {
const client = MatrixClientPeg.safeGet();

if (encrypted) {
const deviceInfoMap = await client.crypto!.deviceList.downloadKeys(Object.keys(contentMap), false);

await Promise.all(
Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
Object.entries(userContentMap).map(async ([deviceId, content]): Promise<void> => {
const devices = deviceInfoMap.get(userId);
if (!devices) return;

if (deviceId === "*") {
// Send the message to all devices we have keys for
await client.encryptAndSendToDevices(
Array.from(devices.values()).map((deviceInfo) => ({
userId,
deviceInfo,
})),
content,
);
} else if (devices.has(deviceId)) {
// Send the message to a specific device
await client.encryptAndSendToDevices(
[{ userId, deviceInfo: devices.get(deviceId)! }],
content,
);
}
}),
),
);
const crypto = client.getCrypto();
if (!crypto) throw new Error("E2EE not enabled");

// attempt to re-batch these up into a single request
const invertedContentMap: { [content: string]: { userId: string; deviceId: string }[] } = {};

Object.entries(contentMap).forEach(([userId, userContentMap]) =>
Object.entries(userContentMap).forEach(([deviceId, content]) => {
const stringifiedContent = JSON.stringify(content);
invertedContentMap[stringifiedContent] = invertedContentMap[stringifiedContent] || [];

invertedContentMap[stringifiedContent].push({ userId, deviceId });
}),
),

await Promise.all(Object.entries(invertedContentMap).map(
async ([stringifiedContent, recipients]) => {
const batch = await crypto.encryptToDeviceMessages(eventType, recipients, JSON.parse(stringifiedContent));

Check failure on line 450 in src/stores/widgets/StopGapWidgetDriver.ts

View workflow job for this annotation

GitHub Actions / Jest (2)

StopGapWidgetDriver › sendToDevice › sends encrypted messages

TypeError: crypto.encryptToDeviceMessages is not a function at encryptToDeviceMessages (src/stores/widgets/StopGapWidgetDriver.ts:450:48) at Array.map (<anonymous>) at StopGapWidgetDriver.map [as sendToDevice] (src/stores/widgets/StopGapWidgetDriver.ts:448:66) at Object.sendToDevice (test/stores/widgets/StopGapWidgetDriver-test.ts:221:26)

await client.queueToDevice(batch);
},
));
} else {
await client.queueToDevice({
eventType,
Expand Down

0 comments on commit 1233d6a

Please sign in to comment.