Skip to content

Commit 7de5c84

Browse files
authored
Add back unencrypted path in StopGapWidgetDriver.sendToDevice (#28295)
1 parent da5c97f commit 7de5c84

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/stores/widgets/StopGapWidgetDriver.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,30 @@ export class StopGapWidgetDriver extends WidgetDriver {
414414
await client._unstable_updateDelayedEvent(delayId, action);
415415
}
416416

417+
/**
418+
* Implements {@link WidgetDriver#sendToDevice}
419+
* Encrypted to-device events are not supported.
420+
*/
421+
public async sendToDevice(
422+
eventType: string,
423+
encrypted: boolean,
424+
contentMap: { [userId: string]: { [deviceId: string]: object } },
425+
): Promise<void> {
426+
if (encrypted) throw new Error("Encrypted to-device events are not supported");
427+
428+
const client = MatrixClientPeg.safeGet();
429+
await client.queueToDevice({
430+
eventType,
431+
batch: Object.entries(contentMap).flatMap(([userId, userContentMap]) =>
432+
Object.entries(userContentMap).map(([deviceId, content]) => ({
433+
userId,
434+
deviceId,
435+
payload: content,
436+
})),
437+
),
438+
});
439+
}
440+
417441
private pickRooms(roomIds?: (string | Symbols.AnyRoom)[]): Room[] {
418442
const client = MatrixClientPeg.get();
419443
if (!client) throw new Error("Not attached to a client");

test/unit-tests/stores/widgets/StopGapWidgetDriver-test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,44 @@ describe("StopGapWidgetDriver", () => {
170170
expect(listener).toHaveBeenCalledWith(openIdUpdate);
171171
});
172172

173+
describe("sendToDevice", () => {
174+
const contentMap = {
175+
"@alice:example.org": {
176+
"*": {
177+
hello: "alice",
178+
},
179+
},
180+
"@bob:example.org": {
181+
bobDesktop: {
182+
hello: "bob",
183+
},
184+
},
185+
};
186+
187+
let driver: WidgetDriver;
188+
189+
beforeEach(() => {
190+
driver = mkDefaultDriver();
191+
});
192+
193+
it("sends unencrypted messages", async () => {
194+
await driver.sendToDevice("org.example.foo", false, contentMap);
195+
expect(client.queueToDevice).toHaveBeenCalledWith({
196+
eventType: "org.example.foo",
197+
batch: [
198+
{ deviceId: "*", payload: { hello: "alice" }, userId: "@alice:example.org" },
199+
{ deviceId: "bobDesktop", payload: { hello: "bob" }, userId: "@bob:example.org" },
200+
],
201+
});
202+
});
203+
204+
it("raises an error if encrypted", async () => {
205+
await expect(driver.sendToDevice("org.example.foo", true, contentMap)).rejects.toThrow(
206+
"Encrypted to-device events are not supported",
207+
);
208+
});
209+
});
210+
173211
describe("getTurnServers", () => {
174212
let driver: WidgetDriver;
175213

0 commit comments

Comments
 (0)