Skip to content

Commit

Permalink
Add back unencrypted path in StopGapWidgetDriver.sendToDevice (#28295)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros authored Oct 25, 2024
1 parent da5c97f commit 7de5c84
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,30 @@ export class StopGapWidgetDriver extends WidgetDriver {
await client._unstable_updateDelayedEvent(delayId, action);
}

/**
* 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,
})),
),
});
}

private pickRooms(roomIds?: (string | Symbols.AnyRoom)[]): Room[] {
const client = MatrixClientPeg.get();
if (!client) throw new Error("Not attached to a client");
Expand Down
38 changes: 38 additions & 0 deletions test/unit-tests/stores/widgets/StopGapWidgetDriver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,44 @@ describe("StopGapWidgetDriver", () => {
expect(listener).toHaveBeenCalledWith(openIdUpdate);
});

describe("sendToDevice", () => {
const contentMap = {
"@alice:example.org": {
"*": {
hello: "alice",
},
},
"@bob:example.org": {
bobDesktop: {
hello: "bob",
},
},
};

let driver: WidgetDriver;

beforeEach(() => {
driver = mkDefaultDriver();
});

it("sends unencrypted messages", async () => {
await driver.sendToDevice("org.example.foo", false, contentMap);
expect(client.queueToDevice).toHaveBeenCalledWith({
eventType: "org.example.foo",
batch: [
{ deviceId: "*", payload: { hello: "alice" }, userId: "@alice:example.org" },
{ deviceId: "bobDesktop", payload: { hello: "bob" }, userId: "@bob:example.org" },
],
});
});

it("raises an error if encrypted", async () => {
await expect(driver.sendToDevice("org.example.foo", true, contentMap)).rejects.toThrow(
"Encrypted to-device events are not supported",
);
});
});

describe("getTurnServers", () => {
let driver: WidgetDriver;

Expand Down

0 comments on commit 7de5c84

Please sign in to comment.