From 7e69f3037e48d28de65542922899a544f902ef56 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Thu, 28 Jul 2022 15:26:25 -0400 Subject: [PATCH] Yield a TURN server immediately --- 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 29aa14f9a6fe..77c3b833184b 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); } }