Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Jun 18, 2024
1 parent 81d36f3 commit 9791209
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 24 deletions.
33 changes: 21 additions & 12 deletions synapse/handlers/sliding_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,26 +769,29 @@ async def get_room_sync_data(
and rooms_for_user_membership_at_to_token.membership == Membership.JOIN
):
newly_joined = (
rooms_for_user_membership_at_to_token.event_pos.stream
> from_token.room_key.get_stream_pos_for_instance(
rooms_for_user_membership_at_to_token.event_pos.instance_name
rooms_for_user_membership_at_to_token.event_pos.persisted_after(
from_token.room_key
)
)

# We should return historical messages (before token range) in the
# following cases because we want clients to be able to show a basic
# screen of information:
# - Initial sync (because no `from_token` to limit us anyway)
# - When users `newly_joined`
# - TODO: For an incremental sync where we haven't sent it down this
# connection before
should_limit_timeline_to_token_range = (
from_token is not None and not newly_joined
)

timeline_events, new_room_key = await self.store.paginate_room_events(
room_id=room_id,
# We're going to paginate backwards from the `to_token`
from_key=to_token.room_key,
# We should return historical messages (before token range) in the
# following cases because we want clients to be able to show a basic
# screen of information:
# - Initial sync (because no `from_token` to limit us anyway)
# - When users `newly_joined`
# - TODO: For an incremental sync where we haven't sent it down this
# connection before
to_key=(
from_token.room_key
if from_token is not None and not newly_joined
if should_limit_timeline_to_token_range
else None
),
direction=Direction.BACKWARDS,
Expand Down Expand Up @@ -832,7 +835,7 @@ async def get_room_sync_data(
# old events in the timeline)
num_live = 0
if from_token is not None:
for timeline_event in timeline_events:
for timeline_event in reversed(timeline_events):
# This fields should be present for all persisted events
assert timeline_event.internal_metadata.stream_ordering is not None
assert timeline_event.internal_metadata.instance_name is not None
Expand All @@ -843,6 +846,12 @@ async def get_room_sync_data(
)
if persisted_position.persisted_after(from_token.room_key):
num_live += 1
else:
# Since we're iterating over the timeline events in
# reverse-chronological order, we can break once we hit an event
# that's not live. In the future, we could potentially optimize
# this more with a binary search (bisect).
break

prev_batch_token = prev_batch_token.copy_and_replace(
StreamKeyType.ROOM, new_room_key
Expand Down
10 changes: 6 additions & 4 deletions synapse/rest/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ class SlidingSyncRestServlet(RestServlet):
Response JSON::
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1337,
Expand Down Expand Up @@ -824,7 +824,8 @@ class SlidingSyncRestServlet(RestServlet):
"joined_count": 41,
"invited_count": 1,
"notification_count": 1,
"highlight_count": 0
"highlight_count": 0,
"num_live": 2"
},
// rooms from list
"!foo:bar": {
Expand All @@ -849,7 +850,8 @@ class SlidingSyncRestServlet(RestServlet):
"joined_count": 4,
"invited_count": 0,
"notification_count": 54,
"highlight_count": 3
"highlight_count": 3,
"num_live": 1,
},
// ... 99 more items
},
Expand Down Expand Up @@ -927,7 +929,7 @@ async def encode_response(
) -> JsonDict:
response: JsonDict = defaultdict(dict)

response["next_pos"] = await sliding_sync_result.next_pos.to_string(self.store)
response["pos"] = await sliding_sync_result.next_pos.to_string(self.store)
serialized_lists = self.encode_lists(sliding_sync_result.lists)
if serialized_lists:
response["lists"] = serialized_lists
Expand Down
3 changes: 3 additions & 0 deletions synapse/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,9 @@ class PersistedPosition:
stream: int

def persisted_after(self, token: AbstractMultiWriterStreamToken) -> bool:
"""
Checks whether this position happened after the token
"""
return token.get_stream_pos_for_instance(self.instance_name) < self.stream


Expand Down
Loading

0 comments on commit 9791209

Please sign in to comment.