From 4c2a0d0756d3402bbf3b66eecac870c97a1f42cf Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Thu, 28 Jul 2022 15:27:33 -0400 Subject: [PATCH] Support sending encrypted to-device messages --- src/stores/widgets/StopGapWidgetDriver.ts | 32 ++++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/stores/widgets/StopGapWidgetDriver.ts b/src/stores/widgets/StopGapWidgetDriver.ts index 29aa14f9a6f..77c3b833184 100644 --- a/src/stores/widgets/StopGapWidgetDriver.ts +++ b/src/stores/widgets/StopGapWidgetDriver.ts @@ -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 { - 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); } }