diff --git a/src/stores/widgets/StopGapWidget.ts b/src/stores/widgets/StopGapWidget.ts index 672c1b27b4..673ceac20a 100644 --- a/src/stores/widgets/StopGapWidget.ts +++ b/src/stores/widgets/StopGapWidget.ts @@ -13,6 +13,7 @@ import { type MatrixClient, ClientEvent, RoomStateEvent, + type ReceivedToDeviceMessage, } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; import { @@ -360,7 +361,7 @@ export class StopGapWidget extends EventEmitter { this.client.on(ClientEvent.Event, this.onEvent); this.client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted); this.client.on(RoomStateEvent.Events, this.onStateUpdate); - this.client.on(ClientEvent.ToDeviceEvent, this.onToDeviceEvent); + this.client.on(ClientEvent.ReceivedToDeviceMessage, this.onToDeviceMessage); this.messaging.on( `action:${WidgetApiFromWidgetAction.UpdateAlwaysOnScreen}`, @@ -493,7 +494,7 @@ export class StopGapWidget extends EventEmitter { this.client.off(ClientEvent.Event, this.onEvent); this.client.off(MatrixEventEvent.Decrypted, this.onEventDecrypted); this.client.off(RoomStateEvent.Events, this.onStateUpdate); - this.client.off(ClientEvent.ToDeviceEvent, this.onToDeviceEvent); + this.client.off(ClientEvent.ReceivedToDeviceMessage, this.onToDeviceMessage); } private onEvent = (ev: MatrixEvent): void => { @@ -513,10 +514,10 @@ export class StopGapWidget extends EventEmitter { }); }; - private onToDeviceEvent = async (ev: MatrixEvent): Promise => { - await this.client.decryptEventIfNeeded(ev); - if (ev.isDecryptionFailure()) return; - await this.messaging?.feedToDevice(ev.getEffectiveEvent() as IRoomEvent, ev.isEncrypted()); + private onToDeviceMessage = async (payload: ReceivedToDeviceMessage): Promise => { + const { message, encryptionInfo } = payload; + // TODO: Update the widget API to use a proper IToDeviceMessage instead of a IRoomEvent + await this.messaging?.feedToDevice(message as IRoomEvent, encryptionInfo != null); }; /** diff --git a/test/unit-tests/stores/widgets/StopGapWidget-test.ts b/test/unit-tests/stores/widgets/StopGapWidget-test.ts index d61070b1e4..9ce5f10240 100644 --- a/test/unit-tests/stores/widgets/StopGapWidget-test.ts +++ b/test/unit-tests/stores/widgets/StopGapWidget-test.ts @@ -83,16 +83,42 @@ describe("StopGapWidget", () => { }); it("feeds incoming to-device messages to the widget", async () => { - const event = mkEvent({ - event: true, - type: "org.example.foo", - user: "@alice:example.org", - content: { hello: "world" }, - }); + const receivedToDevice = { + message: { + type: "org.example.foo", + sender: "@alice:example.org", + content: { + hello: "world", + }, + }, + encryptionInfo: null, + }; - client.emit(ClientEvent.ToDeviceEvent, event); + client.emit(ClientEvent.ReceivedToDeviceMessage, receivedToDevice); await Promise.resolve(); // flush promises - expect(messaging.feedToDevice).toHaveBeenCalledWith(event.getEffectiveEvent(), false); + expect(messaging.feedToDevice).toHaveBeenCalledWith(receivedToDevice.message, false); + }); + + it("feeds incoming encrypted to-device messages to the widget", async () => { + const receivedToDevice = { + message: { + type: "org.example.foo", + sender: "@alice:example.org", + content: { + hello: "world", + }, + }, + encryptionInfo: { + senderVerified: false, + sender: "@alice:example.org", + senderCurve25519KeyBase64: "", + senderDevice: "ABCDEFGHI", + }, + }; + + client.emit(ClientEvent.ReceivedToDeviceMessage, receivedToDevice); + await Promise.resolve(); // flush promises + expect(messaging.feedToDevice).toHaveBeenCalledWith(receivedToDevice.message, true); }); it("feeds incoming state updates to the widget", () => {