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 035f222 commit 330643c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 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 @@ -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
22 changes: 17 additions & 5 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 @@ -423,8 +432,11 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
// 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 330643c

Please sign in to comment.