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

Commit

Permalink
Support sending encrypted to-device messages
Browse files Browse the repository at this point in the history
  • Loading branch information
robintown committed Jul 28, 2022
1 parent 3b02a45 commit 4c2a0d0
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,37 @@ export class StopGapWidgetDriver extends WidgetDriver {

public async sendToDevice(
eventType: string,
encrypt: boolean,
encrypted: boolean,
contentMap: { [userId: string]: { [deviceId: string]: unknown } },
): Promise<void> {
if (encrypt) {
throw new Error("Encrypted to-device events not supported yet");
const client = MatrixClientPeg.get();

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]) => {
if (deviceId === "*") {
// Send the message to all devices we have keys for
await client.encryptAndSendToDevices(
Object.values(deviceInfoMap[userId]).map(deviceInfo => ({
userId, deviceInfo,
})),
content,
);
} else {
// Send the message to a specific device
await client.encryptAndSendToDevices(
[{ userId, deviceInfo: deviceInfoMap[userId][deviceId] }],
content,
);
}
}),
),
);
} else {
await MatrixClientPeg.get().sendToDevice(eventType, contentMap);
await client.sendToDevice(eventType, contentMap);
}
}

Expand Down

0 comments on commit 4c2a0d0

Please sign in to comment.