Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/ClientWidgetApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
} from "./interfaces/DownloadFileAction";
import { IThemeChangeActionRequestData } from "./interfaces/ThemeChangeAction";
import { IUpdateStateToWidgetRequestData } from "./interfaces/UpdateStateAction";
import { IToDeviceMessage } from "./interfaces/IToDeviceMessage";

/**
* API handler for the client side of widgets. This raises events
Expand Down Expand Up @@ -1153,13 +1154,12 @@ export class ClientWidgetApi extends EventEmitter {
* able to receive the event due to permissions, rejects if the widget
* failed to handle the event.
*/
public async feedToDevice(rawEvent: IRoomEvent, encrypted: boolean): Promise<void> {
if (this.canReceiveToDeviceEvent(rawEvent.type)) {
await this.transport.send<ISendToDeviceToWidgetRequestData>(
WidgetApiToWidgetAction.SendToDevice,
// it's compatible, but missing the index signature
{ ...rawEvent, encrypted } as ISendToDeviceToWidgetRequestData,
);
public async feedToDevice(message: IToDeviceMessage, encrypted: boolean): Promise<void> {
if (this.canReceiveToDeviceEvent(message.type)) {
await this.transport.send<ISendToDeviceToWidgetRequestData>(WidgetApiToWidgetAction.SendToDevice, {
...message,
encrypted,
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IRoomAccountData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
export interface IRoomAccountData {
type: string;
room_id: string; // eslint-disable-line camelcase
content: unknown;
content: Record<string, unknown>;
}
4 changes: 2 additions & 2 deletions src/interfaces/IRoomEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export interface IRoomEvent {
room_id: string; // eslint-disable-line camelcase
state_key?: string; // eslint-disable-line camelcase
origin_server_ts: number; // eslint-disable-line camelcase
content: unknown;
unsigned: unknown;
content: Record<string, unknown>;
unsigned: Record<string, unknown>;
//MSC4354
sticky?: {
duration_ms: number;
Expand Down
21 changes: 21 additions & 0 deletions src/interfaces/IToDeviceMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2025 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface IToDeviceMessage {
content: Record<string, unknown>;
sender: string;
type: string;
}
4 changes: 2 additions & 2 deletions src/interfaces/SendToDeviceAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { IWidgetApiRequest, IWidgetApiRequestData } from "./IWidgetApiRequest";
import { WidgetApiFromWidgetAction, WidgetApiToWidgetAction } from "./WidgetApiAction";
import { IWidgetApiResponseData } from "./IWidgetApiResponse";
import { IRoomEvent } from "./IRoomEvent";
import { IToDeviceMessage } from "./IToDeviceMessage";

export interface ISendToDeviceFromWidgetRequestData extends IWidgetApiRequestData {
type: string;
Expand All @@ -38,7 +38,7 @@ export interface ISendToDeviceFromWidgetActionResponse extends ISendToDeviceFrom
response: ISendToDeviceFromWidgetResponseData;
}

export interface ISendToDeviceToWidgetRequestData extends IWidgetApiRequestData, IRoomEvent {
export interface ISendToDeviceToWidgetRequestData extends IWidgetApiRequestData, IToDeviceMessage {
encrypted: boolean;
}

Expand Down
45 changes: 40 additions & 5 deletions test/ClientWidgetApi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
} from "../src";
import { IGetMediaConfigActionFromWidgetActionRequest } from "../src/interfaces/GetMediaConfigAction";
import { IReadRoomAccountDataFromWidgetActionRequest } from "../src/interfaces/ReadRoomAccountDataAction";
import { IToDeviceMessage } from "../src/interfaces/IToDeviceMessage";

jest.mock("../src/transport/PostmessageTransport");

Expand Down Expand Up @@ -886,11 +887,11 @@ describe("ClientWidgetApi", () => {
describe("receiving events", () => {
const roomId = "!room:example.org";
const otherRoomId = "!other-room:example.org";
const event = createRoomEvent({ room_id: roomId, type: "m.room.message", content: "hello" });
const event = createRoomEvent({ room_id: roomId, type: "m.room.message", content: { hello: "there" } });
const eventFromOtherRoom = createRoomEvent({
room_id: otherRoomId,
type: "m.room.message",
content: "test",
content: { test: "test" },
});

it("forwards events to the widget from one room only", async () => {
Expand Down Expand Up @@ -1095,6 +1096,36 @@ describe("ClientWidgetApi", () => {
});
});

describe("receiving to device messages", () => {
it.each([true, false])("forwards device messages to the widget", async (encrypted) => {
const event: IToDeviceMessage = {
content: { foo: "bar" },
type: "org.example.mytype",
sender: "@alice:example.org",
};
// Give the widget capabilities to receive from just one room
await loadIframe(["org.matrix.msc3819.receive.to_device:org.example.mytype"]);

// Event from the matching room should be forwarded
await clientWidgetApi.feedToDevice(event, encrypted);
expect(transport.send).toHaveBeenCalledWith(WidgetApiToWidgetAction.SendToDevice, { ...event, encrypted });
});
it("ignores messages not allowed by capabilities", async () => {
const event: IToDeviceMessage = {
content: { foo: "bar" },
type: "org.example.othertype",
sender: "@alice:example.org",
};
// Give the widget capabilities to receive from just one room
await loadIframe(["org.matrix.msc3819.receive.to_device:org.example.mytype"]);
// Clear all prior messages.
jest.mocked(transport.send).mockClear();
// Event from the matching room should be forwarded
await clientWidgetApi.feedToDevice(event, false);
expect(transport.send).not.toHaveBeenCalled();
});
});

describe("update_delayed_event action", () => {
it("fails to cancel delayed events", async () => {
const event: IUpdateDelayedEventFromWidgetActionRequest = {
Expand Down Expand Up @@ -1727,7 +1758,7 @@ describe("ClientWidgetApi", () => {
it("reads events from a specific room", async () => {
const roomId = "!room:example.org";
jest.spyOn(clientWidgetApi, "getWidgetVersions").mockResolvedValue([]);
const event = createRoomEvent({ room_id: roomId, type: "net.example.test", content: "test" });
const event = createRoomEvent({ room_id: roomId, type: "net.example.test", content: { test: "test" } });
driver.readRoomTimeline.mockImplementation(async (rId) => {
if (rId === roomId) return [event];
return [];
Expand Down Expand Up @@ -1772,8 +1803,12 @@ describe("ClientWidgetApi", () => {
const roomId = "!room:example.org";
const otherRoomId = "!other-room:example.org";
jest.spyOn(clientWidgetApi, "getWidgetVersions").mockResolvedValue([]);
const event = createRoomEvent({ room_id: roomId, type: "net.example.test", content: "test" });
const otherRoomEvent = createRoomEvent({ room_id: otherRoomId, type: "net.example.test", content: "hi" });
const event = createRoomEvent({ room_id: roomId, type: "net.example.test", content: { test: "test" } });
const otherRoomEvent = createRoomEvent({
room_id: otherRoomId,
type: "net.example.test",
content: { hi: "there" },
});
driver.getKnownRooms.mockReturnValue([roomId, otherRoomId]);
driver.readRoomTimeline.mockImplementation(async (rId) => {
if (rId === roomId) return [event];
Expand Down