1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-09 10:22:46 +03:00

review: use single payload for ReceivedToDeviceMessage

This commit is contained in:
Valere
2025-07-02 09:43:36 +02:00
parent 785479c506
commit c3ecd15297
3 changed files with 22 additions and 19 deletions

View File

@@ -20,13 +20,19 @@ import { IDBFactory } from "fake-indexeddb";
import Olm from "@matrix-org/olm";
import { getSyncResponse, syncPromise } from "../../test-utils/test-utils";
import { ClientEvent, createClient, type IToDeviceEvent, type MatrixClient, type MatrixEvent } from "../../../src";
import {
ClientEvent,
createClient,
type IToDeviceEvent,
type MatrixClient,
type MatrixEvent,
type ReceivedToDeviceMessage,
} from "../../../src";
import * as testData from "../../test-utils/test-data";
import { E2EKeyResponder } from "../../test-utils/E2EKeyResponder";
import { SyncResponder } from "../../test-utils/SyncResponder";
import { E2EKeyReceiver } from "../../test-utils/E2EKeyReceiver";
import { encryptOlmEvent, establishOlmSession, getTestOlmAccountKeys } from "./olm-utils.ts";
import { type OlmEncryptionInfo } from "../../../src/crypto-api";
afterEach(() => {
// reset fake-indexeddb after each test, to make sure we don't leak connections
@@ -187,11 +193,10 @@ describe("to-device-messages", () => {
plaintype: "m.test.type",
});
const processedToDeviceResolver: PromiseWithResolvers<[IToDeviceEvent, OlmEncryptionInfo | null]> =
Promise.withResolvers();
const processedToDeviceResolver: PromiseWithResolvers<ReceivedToDeviceMessage> = Promise.withResolvers();
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (message, encryptionInfo) => {
processedToDeviceResolver.resolve([message, encryptionInfo]);
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (payload) => {
processedToDeviceResolver.resolve(payload);
});
const oldToDeviceResolver: PromiseWithResolvers<MatrixEvent> = Promise.withResolvers();
@@ -205,7 +210,7 @@ describe("to-device-messages", () => {
syncResponder.sendOrQueueSyncResponse({ to_device: { events: [toDeviceEvent] } });
await syncPromise(aliceClient);
const [message, encryptionInfo] = await processedToDeviceResolver.promise;
const { message, encryptionInfo } = await processedToDeviceResolver.promise;
expect(message.type).toBe("m.test.type");
expect(message.content["body"]).toBe("foo");
@@ -233,11 +238,10 @@ describe("to-device-messages", () => {
},
};
const processedToDeviceResolver: PromiseWithResolvers<[IToDeviceEvent, OlmEncryptionInfo | null]> =
Promise.withResolvers();
const processedToDeviceResolver: PromiseWithResolvers<ReceivedToDeviceMessage> = Promise.withResolvers();
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (message, encryptionInfo) => {
processedToDeviceResolver.resolve([message, encryptionInfo]);
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (payload) => {
processedToDeviceResolver.resolve(payload);
});
const oldToDeviceResolver: PromiseWithResolvers<MatrixEvent> = Promise.withResolvers();
@@ -249,7 +253,7 @@ describe("to-device-messages", () => {
syncResponder.sendOrQueueSyncResponse({ to_device: { events: [toDeviceEvent] } });
await syncPromise(aliceClient);
const [message, encryptionInfo] = await processedToDeviceResolver.promise;
const { message, encryptionInfo } = await processedToDeviceResolver.promise;
expect(message.type).toBe("m.test.type");
expect(message.content["body"]).toBe("foo");

View File

@@ -87,7 +87,7 @@ import { type IIdentityServerProvider } from "./@types/IIdentityServerProvider.t
import { type MatrixScheduler } from "./scheduler.ts";
import { type BeaconEvent, type BeaconEventHandlerMap } from "./models/beacon.ts";
import { type AuthDict } from "./interactive-auth.ts";
import { type IMinimalEvent, type IRoomEvent, type IStateEvent, type IToDeviceEvent } from "./sync-accumulator.ts";
import { type IMinimalEvent, type IRoomEvent, type IStateEvent, type ReceivedToDeviceMessage } from "./sync-accumulator.ts";
import type { EventTimelineSet } from "./models/event-timeline-set.ts";
import * as ContentHelpers from "./content-helpers.ts";
import {
@@ -220,7 +220,6 @@ import {
CryptoEvent,
type CryptoEventHandlerMap,
type CryptoCallbacks,
type OlmEncryptionInfo,
} from "./crypto-api/index.ts";
import {
type SecretStorageKeyDescription,
@@ -1093,18 +1092,18 @@ export type ClientEventHandlerMap = {
[ClientEvent.ToDeviceEvent]: (event: MatrixEvent) => void;
/**
* Fires whenever the SDK receives a new to-device message.
* @param message - The to-device message which caused this event to fire.
* @param encryptionInfo - The encryptionInfo for this message, null if sent in clear.
* @param payload - The message and encryptionInfo for this message (See {@link ReceivedToDeviceMessage}) which caused this event to fire.
* @example
* ```
* matrixClient.on("receivedToDeviceMessage", function(message, encryptionInfo){
* matrixClient.on("receivedToDeviceMessage", function(payload){
* const { message, encryptionInfo } = payload;
* var claimed_sender = encryptionInfo ? encryptionInfo.sender : message.sender;
* var isVerified = encryptionInfo ? encryptionInfo.verified : false;
* var type = message.type;
* });
* ```
*/
[ClientEvent.ReceivedToDeviceMessage]: (message: IToDeviceEvent, encryptionInfo: OlmEncryptionInfo | null) => void;
[ClientEvent.ReceivedToDeviceMessage]: (payload: ReceivedToDeviceMessage) => void;
/**
* Fires if a to-device event is received that cannot be decrypted.
* Encrypted to-device events will (generally) use plain Olm encryption,

View File

@@ -1976,6 +1976,6 @@ export function processToDeviceMessages(toDeviceMessages: ReceivedToDeviceMessag
client.emit(ClientEvent.ToDeviceEvent, deprecatedCompatibilityEvent);
}
client.emit(ClientEvent.ReceivedToDeviceMessage, processedEvent.message, processedEvent.encryptionInfo);
client.emit(ClientEvent.ReceivedToDeviceMessage, processedEvent);
});
}