Use new CryptoApi.encryptToDeviceMessages() to send encrypted to-device messages from widgets (#28315)

This commit is contained in:
Hugh Nimmo-Smith 2024-10-30 09:37:23 +00:00 committed by GitHub
parent 5c45ca5e3c
commit c23c9dfacb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 106 additions and 16 deletions

View file

@ -416,26 +416,54 @@ export class StopGapWidgetDriver extends WidgetDriver {
/**
* Implements {@link WidgetDriver#sendToDevice}
* Encrypted to-device events are not supported.
*/
public async sendToDevice(
eventType: string,
encrypted: boolean,
contentMap: { [userId: string]: { [deviceId: string]: object } },
): Promise<void> {
if (encrypted) throw new Error("Encrypted to-device events are not supported");
const client = MatrixClientPeg.safeGet();
await client.queueToDevice({
eventType,
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
Object.entries(userContentMap).map(([deviceId, content]) => ({
userId,
deviceId,
payload: content,
})),
),
});
if (encrypted) {
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 }[] } = {};
for (const userId of Object.keys(contentMap)) {
const userContentMap = contentMap[userId];
for (const deviceId of Object.keys(userContentMap)) {
const content = userContentMap[deviceId];
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),
);
await client.queueToDevice(batch);
}),
);
} else {
await client.queueToDevice({
eventType,
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
Object.entries(userContentMap).map(([deviceId, content]) => ({
userId,
deviceId,
payload: content,
})),
),
});
}
}
private pickRooms(roomIds?: (string | Symbols.AnyRoom)[]): Room[] {