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

Support sending to-device messages from widgets with Rust Crypto #12972

Closed
wants to merge 1 commit into from
Closed
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
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 @@
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
Loading