Handle state resets in rooms

This commit is contained in:
Erik Johnston 2024-08-29 19:13:16 +01:00
parent 676754d7a7
commit bc4cb1fc41
3 changed files with 38 additions and 17 deletions

View file

@ -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]]:
"""

View file

@ -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
}

View file

@ -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)