You've already forked matrix-js-sdk
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:
@@ -20,13 +20,19 @@ import { IDBFactory } from "fake-indexeddb";
|
|||||||
import Olm from "@matrix-org/olm";
|
import Olm from "@matrix-org/olm";
|
||||||
|
|
||||||
import { getSyncResponse, syncPromise } from "../../test-utils/test-utils";
|
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 * as testData from "../../test-utils/test-data";
|
||||||
import { E2EKeyResponder } from "../../test-utils/E2EKeyResponder";
|
import { E2EKeyResponder } from "../../test-utils/E2EKeyResponder";
|
||||||
import { SyncResponder } from "../../test-utils/SyncResponder";
|
import { SyncResponder } from "../../test-utils/SyncResponder";
|
||||||
import { E2EKeyReceiver } from "../../test-utils/E2EKeyReceiver";
|
import { E2EKeyReceiver } from "../../test-utils/E2EKeyReceiver";
|
||||||
import { encryptOlmEvent, establishOlmSession, getTestOlmAccountKeys } from "./olm-utils.ts";
|
import { encryptOlmEvent, establishOlmSession, getTestOlmAccountKeys } from "./olm-utils.ts";
|
||||||
import { type OlmEncryptionInfo } from "../../../src/crypto-api";
|
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
// reset fake-indexeddb after each test, to make sure we don't leak connections
|
// 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",
|
plaintype: "m.test.type",
|
||||||
});
|
});
|
||||||
|
|
||||||
const processedToDeviceResolver: PromiseWithResolvers<[IToDeviceEvent, OlmEncryptionInfo | null]> =
|
const processedToDeviceResolver: PromiseWithResolvers<ReceivedToDeviceMessage> = Promise.withResolvers();
|
||||||
Promise.withResolvers();
|
|
||||||
|
|
||||||
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (message, encryptionInfo) => {
|
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (payload) => {
|
||||||
processedToDeviceResolver.resolve([message, encryptionInfo]);
|
processedToDeviceResolver.resolve(payload);
|
||||||
});
|
});
|
||||||
|
|
||||||
const oldToDeviceResolver: PromiseWithResolvers<MatrixEvent> = Promise.withResolvers();
|
const oldToDeviceResolver: PromiseWithResolvers<MatrixEvent> = Promise.withResolvers();
|
||||||
@@ -205,7 +210,7 @@ describe("to-device-messages", () => {
|
|||||||
syncResponder.sendOrQueueSyncResponse({ to_device: { events: [toDeviceEvent] } });
|
syncResponder.sendOrQueueSyncResponse({ to_device: { events: [toDeviceEvent] } });
|
||||||
await syncPromise(aliceClient);
|
await syncPromise(aliceClient);
|
||||||
|
|
||||||
const [message, encryptionInfo] = await processedToDeviceResolver.promise;
|
const { message, encryptionInfo } = await processedToDeviceResolver.promise;
|
||||||
|
|
||||||
expect(message.type).toBe("m.test.type");
|
expect(message.type).toBe("m.test.type");
|
||||||
expect(message.content["body"]).toBe("foo");
|
expect(message.content["body"]).toBe("foo");
|
||||||
@@ -233,11 +238,10 @@ describe("to-device-messages", () => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const processedToDeviceResolver: PromiseWithResolvers<[IToDeviceEvent, OlmEncryptionInfo | null]> =
|
const processedToDeviceResolver: PromiseWithResolvers<ReceivedToDeviceMessage> = Promise.withResolvers();
|
||||||
Promise.withResolvers();
|
|
||||||
|
|
||||||
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (message, encryptionInfo) => {
|
aliceClient.on(ClientEvent.ReceivedToDeviceMessage, (payload) => {
|
||||||
processedToDeviceResolver.resolve([message, encryptionInfo]);
|
processedToDeviceResolver.resolve(payload);
|
||||||
});
|
});
|
||||||
|
|
||||||
const oldToDeviceResolver: PromiseWithResolvers<MatrixEvent> = Promise.withResolvers();
|
const oldToDeviceResolver: PromiseWithResolvers<MatrixEvent> = Promise.withResolvers();
|
||||||
@@ -249,7 +253,7 @@ describe("to-device-messages", () => {
|
|||||||
syncResponder.sendOrQueueSyncResponse({ to_device: { events: [toDeviceEvent] } });
|
syncResponder.sendOrQueueSyncResponse({ to_device: { events: [toDeviceEvent] } });
|
||||||
await syncPromise(aliceClient);
|
await syncPromise(aliceClient);
|
||||||
|
|
||||||
const [message, encryptionInfo] = await processedToDeviceResolver.promise;
|
const { message, encryptionInfo } = await processedToDeviceResolver.promise;
|
||||||
|
|
||||||
expect(message.type).toBe("m.test.type");
|
expect(message.type).toBe("m.test.type");
|
||||||
expect(message.content["body"]).toBe("foo");
|
expect(message.content["body"]).toBe("foo");
|
||||||
|
@@ -87,7 +87,7 @@ import { type IIdentityServerProvider } from "./@types/IIdentityServerProvider.t
|
|||||||
import { type MatrixScheduler } from "./scheduler.ts";
|
import { type MatrixScheduler } from "./scheduler.ts";
|
||||||
import { type BeaconEvent, type BeaconEventHandlerMap } from "./models/beacon.ts";
|
import { type BeaconEvent, type BeaconEventHandlerMap } from "./models/beacon.ts";
|
||||||
import { type AuthDict } from "./interactive-auth.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 type { EventTimelineSet } from "./models/event-timeline-set.ts";
|
||||||
import * as ContentHelpers from "./content-helpers.ts";
|
import * as ContentHelpers from "./content-helpers.ts";
|
||||||
import {
|
import {
|
||||||
@@ -220,7 +220,6 @@ import {
|
|||||||
CryptoEvent,
|
CryptoEvent,
|
||||||
type CryptoEventHandlerMap,
|
type CryptoEventHandlerMap,
|
||||||
type CryptoCallbacks,
|
type CryptoCallbacks,
|
||||||
type OlmEncryptionInfo,
|
|
||||||
} from "./crypto-api/index.ts";
|
} from "./crypto-api/index.ts";
|
||||||
import {
|
import {
|
||||||
type SecretStorageKeyDescription,
|
type SecretStorageKeyDescription,
|
||||||
@@ -1093,18 +1092,18 @@ export type ClientEventHandlerMap = {
|
|||||||
[ClientEvent.ToDeviceEvent]: (event: MatrixEvent) => void;
|
[ClientEvent.ToDeviceEvent]: (event: MatrixEvent) => void;
|
||||||
/**
|
/**
|
||||||
* Fires whenever the SDK receives a new to-device message.
|
* Fires whenever the SDK receives a new to-device message.
|
||||||
* @param message - The to-device message which caused this event to fire.
|
* @param payload - The message and encryptionInfo for this message (See {@link ReceivedToDeviceMessage}) which caused this event to fire.
|
||||||
* @param encryptionInfo - The encryptionInfo for this message, null if sent in clear.
|
|
||||||
* @example
|
* @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 claimed_sender = encryptionInfo ? encryptionInfo.sender : message.sender;
|
||||||
* var isVerified = encryptionInfo ? encryptionInfo.verified : false;
|
* var isVerified = encryptionInfo ? encryptionInfo.verified : false;
|
||||||
* var type = message.type;
|
* 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.
|
* Fires if a to-device event is received that cannot be decrypted.
|
||||||
* Encrypted to-device events will (generally) use plain Olm encryption,
|
* Encrypted to-device events will (generally) use plain Olm encryption,
|
||||||
|
@@ -1976,6 +1976,6 @@ export function processToDeviceMessages(toDeviceMessages: ReceivedToDeviceMessag
|
|||||||
client.emit(ClientEvent.ToDeviceEvent, deprecatedCompatibilityEvent);
|
client.emit(ClientEvent.ToDeviceEvent, deprecatedCompatibilityEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
client.emit(ClientEvent.ReceivedToDeviceMessage, processedEvent.message, processedEvent.encryptionInfo);
|
client.emit(ClientEvent.ReceivedToDeviceMessage, processedEvent);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user