From bc4cb1fc41f844e87389b6ebbe8497bbf974bf2d Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 29 Aug 2024 19:13:16 +0100 Subject: [PATCH] Handle state resets in rooms --- synapse/handlers/sliding_sync/room_lists.py | 41 ++++++++++++++------ synapse/storage/databases/main/roommember.py | 4 +- synapse/storage/roommember.py | 10 ++++- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/synapse/handlers/sliding_sync/room_lists.py b/synapse/handlers/sliding_sync/room_lists.py index 213ee29011..c5d86c1d76 100644 --- a/synapse/handlers/sliding_sync/room_lists.py +++ b/synapse/handlers/sliding_sync/room_lists.py @@ -258,11 +258,13 @@ class SlidingSyncRoomLists: # We keep the current state of the room though room_type=existing_room.room_type, is_encrypted=existing_room.is_encrypted, - tombstone_successor_room_id=existing_room.tombstone_successor_room_id, ) else: # This can happen if we get "state reset" out of the room # after the `to_token`. + room_type = await self.store.get_room_type(room_id) + encryption = await self.store.get_room_encryption(room_id) + room_membership_for_user_map[room_id] = RoomsForUserSlidingSync( room_id=room_id, sender=change.sender, @@ -270,10 +272,8 @@ class SlidingSyncRoomLists: event_id=change.event_id, event_pos=change.event_pos, room_version_id=change.room_version_id, - # We keep the current state of the room though - room_type=existing_room.room_type, - is_encrypted=existing_room.is_encrypted, - tombstone_successor_room_id=existing_room.tombstone_successor_room_id, + room_type=room_type, + is_encrypted=encryption is not None, ) newly_joined_room_ids, newly_left_room_map = ( @@ -281,9 +281,31 @@ class SlidingSyncRoomLists: user_id, from_token=from_token, to_token=to_token ) ) - dm_room_ids = await self._get_dm_rooms_for_user(user_id) + # Handle state resets in the from -> to token range. + state_reset_rooms = ( + newly_left_room_map.keys() - room_membership_for_user_map.keys() + ) + if state_reset_rooms: + room_membership_for_user_map = dict(room_membership_for_user_map) + for room_id in ( + newly_left_room_map.keys() - room_membership_for_user_map.keys() + ): + room_type = await self.store.get_room_type(room_id) + encryption = await self.store.get_room_encryption(room_id) + + room_membership_for_user_map[room_id] = RoomsForUserSlidingSync( + room_id=room_id, + sender=None, + membership=Membership.LEAVE, + event_id=None, + event_pos=newly_left_room_map[room_id], + room_version_id=await self.store.get_room_version_id(room_id), + room_type=room_type, + is_encrypted=encryption is not None, + ) + if sync_config.lists: sync_room_map = { room_id: room_membership_for_user @@ -485,11 +507,6 @@ class SlidingSyncRoomLists: if room_id in rooms_should_send } - # TODO: Handle state reset rooms with newly_left_room_map - - if newly_left_room_map.keys() - room_membership_for_user_map.keys(): - raise NotImplementedError() - return SlidingSyncInterestedRooms( lists=lists, relevant_room_map=relevant_room_map, @@ -753,7 +770,7 @@ class SlidingSyncRoomLists: async def _get_rewind_changes_to_current_membership_to_token( self, user: UserID, - rooms_for_user: Mapping[str, RoomsForUser], + rooms_for_user: Mapping[str, Union[RoomsForUser, RoomsForUserSlidingSync]], to_token: StreamToken, ) -> Mapping[str, Optional[RoomsForUser]]: """ diff --git a/synapse/storage/databases/main/roommember.py b/synapse/storage/databases/main/roommember.py index a124c3d60f..eb6d3ca617 100644 --- a/synapse/storage/databases/main/roommember.py +++ b/synapse/storage/databases/main/roommember.py @@ -1403,8 +1403,7 @@ class RoomMemberWorkerStore(EventsWorkerStore, CacheInvalidationWorkerStore): r.room_version, m.event_instance_name, m.event_stream_ordering, COALESCE(j.room_type, m.room_type), - COALESCE(j.is_encrypted, m.is_encrypted), - COALESCE(j.tombstone_successor_room_id, m.tombstone_successor_room_id) + COALESCE(j.is_encrypted, m.is_encrypted) FROM sliding_sync_membership_snapshots AS m INNER JOIN rooms AS r USING (room_id) LEFT JOIN sliding_sync_joined_rooms AS j ON (j.room_id = m.room_id AND m.membership = 'join') @@ -1422,7 +1421,6 @@ class RoomMemberWorkerStore(EventsWorkerStore, CacheInvalidationWorkerStore): event_pos=PersistedEventPosition(row[5], row[6]), room_type=row[7], is_encrypted=row[8], - tombstone_successor_room_id=row[9], ) for row in txn } diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index af61d11f1d..09213627ec 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -40,10 +40,16 @@ class RoomsForUser: @attr.s(slots=True, frozen=True, weakref_slot=False, auto_attribs=True) -class RoomsForUserSlidingSync(RoomsForUser): +class RoomsForUserSlidingSync: + room_id: str + sender: Optional[str] + membership: str + event_id: Optional[str] + event_pos: PersistedEventPosition + room_version_id: str + room_type: Optional[str] is_encrypted: bool - tombstone_successor_room_id: Optional[str] @attr.s(slots=True, frozen=True, weakref_slot=False, auto_attribs=True)