Skip to content

Commit

Permalink
remove timestamp condition
Browse files Browse the repository at this point in the history
Add configuration for toStartOfTimeline
  • Loading branch information
toger5 committed Jun 14, 2024
1 parent 57a8f32 commit 5a35e3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/models/event-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -408,7 +417,7 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
* 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
Expand All @@ -420,11 +429,14 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
}

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) {
Expand Down Expand Up @@ -482,7 +494,7 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
// 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);
}
});

Expand Down

0 comments on commit 5a35e3b

Please sign in to comment.