@@ -1057,22 +1057,42 @@ async def get_room_sync_data(
10571057 )
10581058 )
10591059
1060- # Figure out the last bump event in the room
1061- #
1062- # By default, just choose the membership event position for any non-join membership
1063- bump_stamp = room_membership_for_user_at_to_token .event_pos .stream
1060+ # Figure out the last bump event in the room. If the bump stamp hasn't
1061+ # changed we omit it from the response.
1062+ bump_stamp = None
1063+
1064+ always_return_bump_stamp = (
1065+ # We use the membership event position for any non-join
1066+ room_membership_for_user_at_to_token .membership != Membership .JOIN
1067+ # We didn't fetch any timeline events but we should still check for
1068+ # a bump_stamp that might be somewhere
1069+ or limited is None
1070+ # There might be a bump event somewhere before the timeline events
1071+ # that we fetched, that we didn't previously send down
1072+ or limited is True
1073+ # Always give the client some frame of reference if this is the
1074+ # first time they are seeing the room down the connection
1075+ or initial
1076+ )
1077+
10641078 # If we're joined to the room, we need to find the last bump event before the
10651079 # `to_token`
10661080 if room_membership_for_user_at_to_token .membership == Membership .JOIN :
1067- # Try and get a bump stamp, if not we just fall back to the
1068- # membership token.
1081+ # Try and get a bump stamp
10691082 new_bump_stamp = await self ._get_bump_stamp (
1070- room_id , to_token , timeline_events
1083+ room_id ,
1084+ to_token ,
1085+ timeline_events ,
1086+ check_outside_timeline = always_return_bump_stamp ,
10711087 )
10721088 if new_bump_stamp is not None :
10731089 bump_stamp = new_bump_stamp
10741090
1075- if bump_stamp < 0 :
1091+ if bump_stamp is None and always_return_bump_stamp :
1092+ # By default, just choose the membership event position for any non-join membership
1093+ bump_stamp = room_membership_for_user_at_to_token .event_pos .stream
1094+
1095+ if bump_stamp is not None and bump_stamp < 0 :
10761096 # We never want to send down negative stream orderings, as you can't
10771097 # sensibly compare positive and negative stream orderings (they have
10781098 # different meanings).
@@ -1165,14 +1185,23 @@ async def get_room_sync_data(
11651185
11661186 @trace
11671187 async def _get_bump_stamp (
1168- self , room_id : str , to_token : StreamToken , timeline : List [EventBase ]
1188+ self ,
1189+ room_id : str ,
1190+ to_token : StreamToken ,
1191+ timeline : List [EventBase ],
1192+ check_outside_timeline : bool ,
11691193 ) -> Optional [int ]:
1170- """Get a bump stamp for the room, if we have a bump event
1194+ """Get a bump stamp for the room, if we have a bump event and it has
1195+ changed.
11711196
11721197 Args:
11731198 room_id
11741199 to_token: The upper bound of token to return
11751200 timeline: The list of events we have fetched.
1201+ limited: If the timeline was limited.
1202+ check_outside_timeline: Whether we need to check for bump stamp for
1203+ events before the timeline if we didn't find a bump stamp in
1204+ the timeline events.
11761205 """
11771206
11781207 # First check the timeline events we're returning to see if one of
@@ -1192,6 +1221,11 @@ async def _get_bump_stamp(
11921221 if new_bump_stamp > 0 :
11931222 return new_bump_stamp
11941223
1224+ if not check_outside_timeline :
1225+ # If we are not a limited sync, then we know the bump stamp can't
1226+ # have changed.
1227+ return None
1228+
11951229 # We can quickly query for the latest bump event in the room using the
11961230 # sliding sync tables.
11971231 latest_room_bump_stamp = await self .store .get_latest_bump_stamp_for_room (
0 commit comments