Skip to content

Commit

Permalink
Use new CryptoApi.encryptToDeviceMessages() to send encrypted to-devi…
Browse files Browse the repository at this point in the history
…ce messages from widgets
  • Loading branch information
hughns committed Oct 28, 2024
1 parent 79c9563 commit 571b714
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,26 +416,49 @@ 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 }[] } = {};

Object.entries(contentMap).forEach(([userId, userContentMap]) =>

Check failure on line 434 in src/stores/widgets/StopGapWidgetDriver.ts

View workflow job for this annotation

GitHub Actions / ESLint

Expected an assignment or function call and instead saw an expression
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));

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[] {
Expand Down

0 comments on commit 571b714

Please # to comment.