Widgets: Use the new ClientEvent.ReceivedToDeviceMessage instead of ToDeviceEvent (#30239)

This commit is contained in:
Valere Fedronic 2025-07-10 10:04:29 +02:00 committed by GitHub
parent 366eeb7d61
commit 2d92b73e5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 14 deletions

View File

@ -13,6 +13,7 @@ import {
type MatrixClient, type MatrixClient,
ClientEvent, ClientEvent,
RoomStateEvent, RoomStateEvent,
type ReceivedToDeviceMessage,
} from "matrix-js-sdk/src/matrix"; } from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types"; import { KnownMembership } from "matrix-js-sdk/src/types";
import { import {
@ -360,7 +361,7 @@ export class StopGapWidget extends EventEmitter {
this.client.on(ClientEvent.Event, this.onEvent); this.client.on(ClientEvent.Event, this.onEvent);
this.client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted); this.client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
this.client.on(RoomStateEvent.Events, this.onStateUpdate); this.client.on(RoomStateEvent.Events, this.onStateUpdate);
this.client.on(ClientEvent.ToDeviceEvent, this.onToDeviceEvent); this.client.on(ClientEvent.ReceivedToDeviceMessage, this.onToDeviceMessage);
this.messaging.on( this.messaging.on(
`action:${WidgetApiFromWidgetAction.UpdateAlwaysOnScreen}`, `action:${WidgetApiFromWidgetAction.UpdateAlwaysOnScreen}`,
@ -493,7 +494,7 @@ export class StopGapWidget extends EventEmitter {
this.client.off(ClientEvent.Event, this.onEvent); this.client.off(ClientEvent.Event, this.onEvent);
this.client.off(MatrixEventEvent.Decrypted, this.onEventDecrypted); this.client.off(MatrixEventEvent.Decrypted, this.onEventDecrypted);
this.client.off(RoomStateEvent.Events, this.onStateUpdate); 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 => { private onEvent = (ev: MatrixEvent): void => {
@ -513,10 +514,10 @@ export class StopGapWidget extends EventEmitter {
}); });
}; };
private onToDeviceEvent = async (ev: MatrixEvent): Promise<void> => { private onToDeviceMessage = async (payload: ReceivedToDeviceMessage): Promise<void> => {
await this.client.decryptEventIfNeeded(ev); const { message, encryptionInfo } = payload;
if (ev.isDecryptionFailure()) return; // TODO: Update the widget API to use a proper IToDeviceMessage instead of a IRoomEvent
await this.messaging?.feedToDevice(ev.getEffectiveEvent() as IRoomEvent, ev.isEncrypted()); await this.messaging?.feedToDevice(message as IRoomEvent, encryptionInfo != null);
}; };
/** /**

View File

@ -83,16 +83,42 @@ describe("StopGapWidget", () => {
}); });
it("feeds incoming to-device messages to the widget", async () => { it("feeds incoming to-device messages to the widget", async () => {
const event = mkEvent({ const receivedToDevice = {
event: true, message: {
type: "org.example.foo", type: "org.example.foo",
user: "@alice:example.org", sender: "@alice:example.org",
content: { hello: "world" }, content: {
}); hello: "world",
},
},
encryptionInfo: null,
};
client.emit(ClientEvent.ToDeviceEvent, event); client.emit(ClientEvent.ReceivedToDeviceMessage, receivedToDevice);
await Promise.resolve(); // flush promises 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", () => { it("feeds incoming state updates to the widget", () => {