From 5a35e3b170082aace83c21771494cea97eeeddc2 Mon Sep 17 00:00:00 2001 From: Timo Date: Fri, 14 Jun 2024 17:17:21 +0200 Subject: [PATCH] remove timestamp condition Add configuration for toStartOfTimeline --- src/models/event-timeline.ts | 6 +++--- src/models/room-state.ts | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/models/event-timeline.ts b/src/models/event-timeline.ts index 912dcaf2130..0429b5cee73 100644 --- a/src/models/event-timeline.ts +++ b/src/models/event-timeline.ts @@ -151,7 +151,7 @@ export class EventTimeline { throw new Error("Cannot initialise state after events are added"); } - this.startState?.setStateEvents(stateEvents, { timelineWasEmpty }); + this.startState?.setStateEvents(stateEvents, { timelineWasEmpty, toStartOfTimeline: true }); this.endState?.setStateEvents(stateEvents, { timelineWasEmpty }); } @@ -267,7 +267,7 @@ export class EventTimeline { /** * Get a pagination token * - * @param direction - EventTimeline.BACKWARDS to get the pagination + * @param direction - EventTimeline.BACKWARDS to get the pagination * token for going backwards in time; EventTimeline.FORWARDS to get the * pagination token for going forwards in time. * @@ -375,7 +375,7 @@ export class EventTimeline { // modify state but only on unfiltered timelineSets if (event.isState() && timelineSet.room.getUnfilteredTimelineSet() === timelineSet) { - roomState?.setStateEvents([event], { timelineWasEmpty }); + roomState?.setStateEvents([event], { timelineWasEmpty, toStartOfTimeline }); // it is possible that the act of setting the state event means we // can set more metadata (specifically sender/target props), so try // it again if the prop wasn't previously set. It may also mean that diff --git a/src/models/room-state.ts b/src/models/room-state.ts index 7b5f3125829..1fda01556c7 100644 --- a/src/models/room-state.ts +++ b/src/models/room-state.ts @@ -43,7 +43,16 @@ export interface IMarkerFoundOptions { */ timelineWasEmpty?: boolean; } - +export interface ISetStateEventsOptions { + /** Whether the event is inserted at the start of the timeline + * or at the end. This is relevant for doing a sanity check: + * "Is the event id of the added event the same as the replaces_state id of the current event" + * We need to do this because sync sometimes feeds previous state events. + * If set to true the sanity check is inverted + * "Is the event id of the current event the same as the replaces_state id of the added event" + */ + toStartOfTimeline?: boolean; +} // possible statuses for out-of-band member loading enum OobStatus { NotStarted, @@ -408,7 +417,7 @@ export class RoomState extends TypedEventEmitter * Fires {@link RoomStateEvent.Events} * Fires {@link RoomStateEvent.Marker} */ - public setStateEvents(stateEvents: MatrixEvent[], markerFoundOptions?: IMarkerFoundOptions): void { + public setStateEvents(stateEvents: MatrixEvent[], options?: IMarkerFoundOptions & ISetStateEventsOptions): void { this.updateModifiedTime(); // update the core event dict @@ -420,11 +429,14 @@ export class RoomState extends TypedEventEmitter } const lastStateEvent = this.getStateEventMatching(event); - // Safety measurs to not update the room (and emit the update) with older state. + // Safety measure to not update the room (and emit the update) with older state. // The sync loop really should not send old events but it does very regularly. // Logging on return in those two conditions results in a large amount of logging. (on startup and when running element) - if ((lastStateEvent?.event.origin_server_ts ?? 0) > (event.event.origin_server_ts ?? 1)) return; - if (lastStateEvent?.event.unsigned?.replaces_state === event.event.event_id) return; + if (options?.toStartOfTimeline) { + if (event.event.unsigned?.replaces_state === lastStateEvent?.event.event_id) return; + } else { + if (lastStateEvent?.event.unsigned?.replaces_state === event.event.event_id) return; + } this.setStateEvent(event); if (event.getType() === EventType.RoomMember) { @@ -482,7 +494,7 @@ export class RoomState extends TypedEventEmitter // assume all our sentinels are now out-of-date this.sentinels = {}; } else if (UNSTABLE_MSC2716_MARKER.matches(event.getType())) { - this.emit(RoomStateEvent.Marker, event, markerFoundOptions); + this.emit(RoomStateEvent.Marker, event, options); } });