Skip to content
This repository has been archived by the owner on Oct 22, 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 Oct 7, 2024
1 parent 5fbc5af commit 8abd3a6
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 @@ -422,33 +422,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 442 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:442:48) at Array.map (<anonymous>) at StopGapWidgetDriver.map [as sendToDevice] (src/stores/widgets/StopGapWidgetDriver.ts:440:66) at Object.sendToDevice (test/stores/widgets/StopGapWidgetDriver-test.ts:217:26)

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

0 comments on commit 8abd3a6

Please sign in to comment.