1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-06 12:02:40 +03:00

Simplify code paths for building to-device MatrixEvents (#4729)

We don't need all the complicated stuff when we have a to-device event, so
let's simplify.
This commit is contained in:
Richard van der Hoff
2025-02-24 17:51:42 +00:00
committed by GitHub
parent bcaa7f63c7
commit ea770282ea
3 changed files with 12 additions and 11 deletions

View File

@@ -25,7 +25,8 @@ export interface MapperOpts {
preventReEmit?: boolean; preventReEmit?: boolean;
// decrypt event proactively // decrypt event proactively
decrypt?: boolean; decrypt?: boolean;
// the event is a to_device event
/** @deprecated no longer used */
toDevice?: boolean; toDevice?: boolean;
} }
@@ -34,10 +35,6 @@ export function eventMapperFor(client: MatrixClient, options: MapperOpts): Event
const decrypt = options.decrypt !== false; const decrypt = options.decrypt !== false;
function mapper(plainOldJsObject: Partial<IEvent>): MatrixEvent { function mapper(plainOldJsObject: Partial<IEvent>): MatrixEvent {
if (options.toDevice) {
delete plainOldJsObject.room_id;
}
const room = client.getRoom(plainOldJsObject.room_id); const room = client.getRoom(plainOldJsObject.room_id);
let event: MatrixEvent | undefined; let event: MatrixEvent | undefined;
@@ -74,9 +71,6 @@ export function eventMapperFor(client: MatrixClient, options: MapperOpts): Event
event.setThread(thread); event.setThread(thread);
} }
// TODO: once we get rid of the old libolm-backed crypto, we can restrict this to room events (rather than
// to-device events), because the rust implementation decrypts to-device messages at a higher level.
// Generally we probably want to use a different eventMapper implementation for to-device events because
if (event.isEncrypted()) { if (event.isEncrypted()) {
if (!preventReEmit) { if (!preventReEmit) {
client.reEmitter.reEmit(event, [MatrixEventEvent.Decrypted]); client.reEmitter.reEmit(event, [MatrixEventEvent.Decrypted]);

View File

@@ -28,6 +28,7 @@ import {
defaultClientOpts, defaultClientOpts,
defaultSyncApiOpts, defaultSyncApiOpts,
type SetPresence, type SetPresence,
mapToDeviceEvent,
} from "./sync.ts"; } from "./sync.ts";
import { type MatrixEvent } from "./models/event.ts"; import { type MatrixEvent } from "./models/event.ts";
import { import {
@@ -151,7 +152,7 @@ class ExtensionToDevice implements Extension<ExtensionToDeviceRequest, Extension
events = await this.cryptoCallbacks.preprocessToDeviceMessages(events); events = await this.cryptoCallbacks.preprocessToDeviceMessages(events);
} }
events events
.map(this.client.getEventMapper()) .map(mapToDeviceEvent)
.map((toDeviceEvent) => { .map((toDeviceEvent) => {
// map is a cheap inline forEach // map is a cheap inline forEach
// We want to flag m.key.verification.start events as cancelled // We want to flag m.key.verification.start events as cancelled

View File

@@ -54,7 +54,7 @@ import {
type ITimeline, type ITimeline,
type IToDeviceEvent, type IToDeviceEvent,
} from "./sync-accumulator.ts"; } from "./sync-accumulator.ts";
import { type MatrixEvent } from "./models/event.ts"; import { MatrixEvent, type IEvent } from "./models/event.ts";
import { type MatrixError, Method } from "./http-api/index.ts"; import { type MatrixError, Method } from "./http-api/index.ts";
import { type ISavedSync } from "./store/index.ts"; import { type ISavedSync } from "./store/index.ts";
import { EventType } from "./@types/event.ts"; import { EventType } from "./@types/event.ts";
@@ -1152,7 +1152,7 @@ export class SyncApi {
const cancelledKeyVerificationTxns: string[] = []; const cancelledKeyVerificationTxns: string[] = [];
toDeviceMessages toDeviceMessages
.map(client.getEventMapper({ toDevice: true })) .map(mapToDeviceEvent)
.map((toDeviceEvent) => { .map((toDeviceEvent) => {
// map is a cheap inline forEach // map is a cheap inline forEach
// We want to flag m.key.verification.start events as cancelled // We want to flag m.key.verification.start events as cancelled
@@ -1945,3 +1945,9 @@ export function _createAndReEmitRoom(client: MatrixClient, roomId: string, opts:
return room; return room;
} }
export function mapToDeviceEvent(plainOldJsObject: Partial<IEvent>): MatrixEvent {
// to-device events should not have a `room_id` property, but let's be sure
delete plainOldJsObject.room_id;
return new MatrixEvent(plainOldJsObject);
}