summary refs log tree commit diff
path: root/synapse/handlers
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2024-07-10 15:54:46 +0100
committerErik Johnston <erik@matrix.org>2024-07-10 15:54:46 +0100
commita9c2e97ed4cd82fea0fcd210ee87155c3821ab93 (patch)
tree36d6c575e2aad94ce5ffab15a4d7196093ca476b /synapse/handlers
parentFixup (diff)
downloadsynapse-a9c2e97ed4cd82fea0fcd210ee87155c3821ab93.tar.xz
Fixup
Diffstat (limited to 'synapse/handlers')
-rw-r--r--synapse/handlers/sliding_sync.py34
1 files changed, 26 insertions, 8 deletions
diff --git a/synapse/handlers/sliding_sync.py b/synapse/handlers/sliding_sync.py
index 4d56ba7b26..cd0ba7d1f6 100644
--- a/synapse/handlers/sliding_sync.py
+++ b/synapse/handlers/sliding_sync.py
@@ -1012,22 +1012,40 @@ class SlidingSyncHandler:
             A sorted list of room IDs by `stream_ordering` along with membership information.
         """
 
-        self.store._events_stream_cache._entity_to_key
-
-        last_activity_in_room_map = {}
-        to_fetch = []
+        # Assemble a map of room ID to the `stream_ordering` of the last activity that the
+        # user should see in the room (<= `to_token`)
+        last_activity_in_room_map: Dict[str, int] = {}
         for room_id, room_for_user in sync_room_map.items():
+            # If they are fully-joined to the room, let's find the latest activity
+            # at/before the `to_token`.
             if room_for_user.membership == Membership.JOIN:
                 stream_pos = self.store._events_stream_cache._entity_to_key.get(room_id)
                 if stream_pos is not None:
                     last_activity_in_room_map[room_id] = stream_pos
-                else:
-                    to_fetch.append(room_id)
+                    continue
+
+                last_event_result = await self.store.get_rough_stream_ordering_for_room(
+                    room_id
+                )
+
+                # If the room has no events at/before the `to_token`, this is probably a
+                # mistake in the code that generates the `sync_room_map` since that should
+                # only give us rooms that the user had membership in during the token range.
+                assert last_event_result is not None
+
+                _, event_pos = last_event_result
+
+                last_activity_in_room_map[room_id] = event_pos.stream
             else:
+                # Otherwise, if the user has left/been invited/knocked/been banned from
+                # a room, they shouldn't see anything past that point.
+                #
+                # FIXME: It's possible that people should see beyond this point in
+                # invited/knocked cases if for example the room has
+                # `invite`/`world_readable` history visibility, see
+                # https://github.com/matrix-org/matrix-spec-proposals/pull/3575#discussion_r1653045932
                 last_activity_in_room_map[room_id] = room_for_user.event_pos.stream
 
-        last_activity_in_room_map.update(await self.store.rough_get_last_pos(to_fetch))
-
         return sorted(
             sync_room_map.values(),
             # Sort by the last activity (stream_ordering) in the room