Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/18648.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only include latest membership events for each user/room combination in a single incremental legacy sync response.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this also fixes #1413 (join/leave) vs (leave/invite)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would, yes.

31 changes: 29 additions & 2 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,9 +2539,36 @@ async def _get_room_changes_for_incremental_sync(

assert since_token

mem_change_events_by_room_id: Dict[str, List[EventBase]] = {}
# Filter out older membership events for the same user in the same room, keeping only
# the most recent one based on stream_ordering.
#
# This prevents situations where a user leaves a room and is re-invited within
# the same incremental sync period, which would otherwise result in both the leave
# and invite appearing in the sync result and confusing clients about the user's
# actual state in the room.
most_recent_events_by_room_and_user: Dict[Tuple[str, str], EventBase] = {}
for event in membership_change_events:
mem_change_events_by_room_id.setdefault(event.room_id, []).append(event)
room_and_user_key = (event.room_id, event.state_key)
if room_and_user_key in most_recent_events_by_room_and_user:
existing_event = most_recent_events_by_room_and_user[room_and_user_key]

assert event.internal_metadata.stream_ordering
assert existing_event.internal_metadata.stream_ordering

# Replace it if the new event has a higher stream ordering
if (
event.internal_metadata.stream_ordering
> existing_event.internal_metadata.stream_ordering
):
most_recent_events_by_room_and_user[room_and_user_key] = event
else:
most_recent_events_by_room_and_user[room_and_user_key] = event

# Now rebuild mem_change_events_by_room_id using only the most recent membership event
# for each user in each room, ensuring the sync result reflects the latest state
mem_change_events_by_room_id: Dict[str, List[EventBase]] = {}
for (room_id, _), event in most_recent_events_by_room_and_user.items():
mem_change_events_by_room_id.setdefault(room_id, []).append(event)

newly_joined_rooms: List[str] = list(
sync_result_builder.forced_newly_joined_room_ids
Expand Down
Loading