Skip to content

Commit

Permalink
make the hack accumulate state properly
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Sep 28, 2023
1 parent 6b8d012 commit a3c3a08
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions sync2/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,22 +891,37 @@ func (p *poller) parseRoomsResponse(ctx context.Context, res *SyncResponse) erro
}

func tempHackMakeTimelineLimited(s *SyncV2JoinResponse) {
var timelineState []json.RawMessage
var timelineMsgs []json.RawMessage
// Build a state map using the state block.
state := make(map[[2]string]json.RawMessage)
for _, event := range s.State.Events {
parsed := gjson.ParseBytes(event)
key := [2]string{parsed.Get("type").Str, parsed.Get("state_key").Str}
state[key] = event
}

// Scan the timeline. Add state events to the state block, dumping the rest of the
// timeline messages into an array.
var timelineMsgs []json.RawMessage
for _, event := range s.Timeline.Events {
if gjson.GetBytes(event, "state_key").Exists() {
timelineState = append(timelineState, event)
parsed := gjson.ParseBytes(event)
if parsed.Get("state_key").Exists() {
key := [2]string{parsed.Get("type").Str, parsed.Get("state_key").Str}
state[key] = event
} else {
timelineMsgs = append(timelineMsgs, event)
}
}

if len(timelineState) > 0 {
// If we found at least one state event in the timeline, edit the response in-place
// to make it look like a limited sync.
if len(state) > len(s.State.Events) {
logger.Debug().Msgf("HACK: move %d state events to state block and mark as limited", len(state)-len(s.State.Events))
s.Timeline.Limited = true
s.Timeline.Events = timelineMsgs
s.State.Events = append(s.State.Events, timelineState...)
logger.Debug().Msgf("HACK: move %d state events to state block and mark as limited", len(timelineState))
s.State.Events = make([]json.RawMessage, 0, len(state))
for _, event := range state {
s.State.Events = append(s.State.Events, event)
}
}
}

Expand Down

0 comments on commit a3c3a08

Please sign in to comment.