mirror of https://github.com/vector-im/riot-web
Use new CryptoApi.encryptToDeviceMessages() to send encrypted to-device messages from widgets (#28315)
parent
5c45ca5e3c
commit
c23c9dfacb
|
@ -416,16 +416,43 @@ export class StopGapWidgetDriver extends WidgetDriver {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements {@link WidgetDriver#sendToDevice}
|
* Implements {@link WidgetDriver#sendToDevice}
|
||||||
* Encrypted to-device events are not supported.
|
|
||||||
*/
|
*/
|
||||||
public async sendToDevice(
|
public async sendToDevice(
|
||||||
eventType: string,
|
eventType: string,
|
||||||
encrypted: boolean,
|
encrypted: boolean,
|
||||||
contentMap: { [userId: string]: { [deviceId: string]: object } },
|
contentMap: { [userId: string]: { [deviceId: string]: object } },
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (encrypted) throw new Error("Encrypted to-device events are not supported");
|
|
||||||
|
|
||||||
const client = MatrixClientPeg.safeGet();
|
const client = MatrixClientPeg.safeGet();
|
||||||
|
|
||||||
|
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({
|
await client.queueToDevice({
|
||||||
eventType,
|
eventType,
|
||||||
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
|
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
|
||||||
|
@ -437,6 +464,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private pickRooms(roomIds?: (string | Symbols.AnyRoom)[]): Room[] {
|
private pickRooms(roomIds?: (string | Symbols.AnyRoom)[]): Room[] {
|
||||||
const client = MatrixClientPeg.get();
|
const client = MatrixClientPeg.get();
|
||||||
|
|
|
@ -201,10 +201,72 @@ describe("StopGapWidgetDriver", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("raises an error if encrypted", async () => {
|
it("sends encrypted messages", async () => {
|
||||||
await expect(driver.sendToDevice("org.example.foo", true, contentMap)).rejects.toThrow(
|
const encryptToDeviceMessages = jest
|
||||||
"Encrypted to-device events are not supported",
|
.fn()
|
||||||
|
.mockImplementation(
|
||||||
|
(eventType, recipients: { userId: string; deviceId: string }[], content: object) => ({
|
||||||
|
eventType: "m.room.encrypted",
|
||||||
|
batch: recipients.map(({ userId, deviceId }) => ({
|
||||||
|
userId,
|
||||||
|
deviceId,
|
||||||
|
payload: {
|
||||||
|
eventType,
|
||||||
|
content,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
MatrixClientPeg.safeGet().getCrypto()!.encryptToDeviceMessages = encryptToDeviceMessages;
|
||||||
|
|
||||||
|
await driver.sendToDevice("org.example.foo", true, {
|
||||||
|
"@alice:example.org": {
|
||||||
|
aliceMobile: {
|
||||||
|
hello: "alice",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"@bob:example.org": {
|
||||||
|
bobDesktop: {
|
||||||
|
hello: "bob",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(encryptToDeviceMessages).toHaveBeenCalledWith(
|
||||||
|
"org.example.foo",
|
||||||
|
[{ deviceId: "aliceMobile", userId: "@alice:example.org" }],
|
||||||
|
{
|
||||||
|
hello: "alice",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(encryptToDeviceMessages).toHaveBeenCalledWith(
|
||||||
|
"org.example.foo",
|
||||||
|
[{ deviceId: "bobDesktop", userId: "@bob:example.org" }],
|
||||||
|
{
|
||||||
|
hello: "bob",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
expect(client.queueToDevice).toHaveBeenCalledWith({
|
||||||
|
eventType: "m.room.encrypted",
|
||||||
|
batch: expect.arrayContaining([
|
||||||
|
{
|
||||||
|
deviceId: "aliceMobile",
|
||||||
|
payload: { content: { hello: "alice" }, eventType: "org.example.foo" },
|
||||||
|
userId: "@alice:example.org",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
});
|
||||||
|
expect(client.queueToDevice).toHaveBeenCalledWith({
|
||||||
|
eventType: "m.room.encrypted",
|
||||||
|
batch: expect.arrayContaining([
|
||||||
|
{
|
||||||
|
deviceId: "bobDesktop",
|
||||||
|
payload: { content: { hello: "bob" }, eventType: "org.example.foo" },
|
||||||
|
userId: "@bob:example.org",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue