Skip to content

Commit fd73d50

Browse files
authored
Add RoomWidgetClient.sendToDeviceViaWidgetApi() (#4475)
1 parent e72859a commit fd73d50

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

spec/unit/embedded.spec.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
import { createRoomWidgetClient, MsgType, UpdateDelayedEventAction } from "../../src/matrix";
3636
import { MatrixClient, ClientEvent, ITurnServer as IClientTurnServer } from "../../src/client";
3737
import { SyncState } from "../../src/sync";
38-
import { ICapabilities } from "../../src/embedded";
38+
import { ICapabilities, RoomWidgetClient } from "../../src/embedded";
3939
import { MatrixEvent } from "../../src/models/event";
4040
import { ToDeviceBatch } from "../../src/models/ToDeviceMessage";
4141
import { DeviceInfo } from "../../src/crypto/deviceinfo";
@@ -493,6 +493,23 @@ describe("RoomWidgetClient", () => {
493493
["@bob:example.org"]: { ["bobDesktop"]: { hello: "bob!" } },
494494
};
495495

496+
const encryptedContentMap = new Map<string, Map<string, object>>([
497+
["@alice:example.org", new Map([["aliceMobile", { hello: "alice!" }]])],
498+
["@bob:example.org", new Map([["bobDesktop", { hello: "bob!" }]])],
499+
]);
500+
501+
it("sends unencrypted (sendToDeviceViaWidgetApi)", async () => {
502+
await makeClient({ sendToDevice: ["org.example.foo"] });
503+
expect(widgetApi.requestCapabilityToSendToDevice).toHaveBeenCalledWith("org.example.foo");
504+
505+
await (client as RoomWidgetClient).sendToDeviceViaWidgetApi(
506+
"org.example.foo",
507+
false,
508+
unencryptedContentMap,
509+
);
510+
expect(widgetApi.sendToDevice).toHaveBeenCalledWith("org.example.foo", false, expectedRequestData);
511+
});
512+
496513
it("sends unencrypted (sendToDevice)", async () => {
497514
await makeClient({ sendToDevice: ["org.example.foo"] });
498515
expect(widgetApi.requestCapabilityToSendToDevice).toHaveBeenCalledWith("org.example.foo");
@@ -534,6 +551,17 @@ describe("RoomWidgetClient", () => {
534551
});
535552
});
536553

554+
it("sends encrypted (sendToDeviceViaWidgetApi)", async () => {
555+
await makeClient({ sendToDevice: ["org.example.foo"] });
556+
expect(widgetApi.requestCapabilityToSendToDevice).toHaveBeenCalledWith("org.example.foo");
557+
558+
await (client as RoomWidgetClient).sendToDeviceViaWidgetApi("org.example.foo", true, encryptedContentMap);
559+
expect(widgetApi.sendToDevice).toHaveBeenCalledWith("org.example.foo", true, {
560+
"@alice:example.org": { aliceMobile: { hello: "alice!" } },
561+
"@bob:example.org": { bobDesktop: { hello: "bob!" } },
562+
});
563+
});
564+
537565
it.each([
538566
{ encrypted: false, title: "unencrypted" },
539567
{ encrypted: true, title: "encrypted" },

src/embedded.ts

+21
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,27 @@ export class RoomWidgetClient extends MatrixClient {
420420
await this.widgetApi.sendToDevice((payload as { type: string }).type, true, recursiveMapToObject(contentMap));
421421
}
422422

423+
/**
424+
* Send an event to a specific list of devices via the widget API. Optionally encrypts the event.
425+
*
426+
* If you are using a full MatrixClient you would be calling {@link MatrixClient.getCrypto().encryptToDeviceMessages()} followed
427+
* by {@link MatrixClient.queueToDevice}.
428+
*
429+
* However, this is combined into a single step when running as an embedded widget client. So, we expose this method for those
430+
* that need it.
431+
*
432+
* @param eventType - Type of the event to send.
433+
* @param encrypted - Whether the event should be encrypted.
434+
* @param contentMap - The content to send. Map from user_id to device_id to content object.
435+
*/
436+
public async sendToDeviceViaWidgetApi(
437+
eventType: string,
438+
encrypted: boolean,
439+
contentMap: SendToDeviceContentMap,
440+
): Promise<void> {
441+
await this.widgetApi.sendToDevice(eventType, encrypted, recursiveMapToObject(contentMap));
442+
}
443+
423444
// Overridden since we get TURN servers automatically over the widget API,
424445
// and this method would otherwise complain about missing an access token
425446
public async checkTurnServers(): Promise<boolean> {

0 commit comments

Comments
 (0)