Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 845732b

Browse files
authored
Fix rooms not being properly excluded from incremental sync (#13408)
1 parent a648a06 commit 845732b

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

changelog.d/13408.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in Synapse 1.57.0 where rooms listed in `exclude_rooms_from_sync` in the configuration file would not be properly excluded from incremental syncs.

synapse/handlers/sync.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,15 +1536,13 @@ async def _generate_sync_entry_for_rooms(
15361536
ignored_users = await self.store.ignored_users(user_id)
15371537
if since_token:
15381538
room_changes = await self._get_rooms_changed(
1539-
sync_result_builder, ignored_users, self.rooms_to_exclude
1539+
sync_result_builder, ignored_users
15401540
)
15411541
tags_by_room = await self.store.get_updated_tags(
15421542
user_id, since_token.account_data_key
15431543
)
15441544
else:
1545-
room_changes = await self._get_all_rooms(
1546-
sync_result_builder, ignored_users, self.rooms_to_exclude
1547-
)
1545+
room_changes = await self._get_all_rooms(sync_result_builder, ignored_users)
15481546
tags_by_room = await self.store.get_tags_for_user(user_id)
15491547

15501548
log_kv({"rooms_changed": len(room_changes.room_entries)})
@@ -1623,13 +1621,14 @@ async def _get_rooms_changed(
16231621
self,
16241622
sync_result_builder: "SyncResultBuilder",
16251623
ignored_users: FrozenSet[str],
1626-
excluded_rooms: List[str],
16271624
) -> _RoomChanges:
16281625
"""Determine the changes in rooms to report to the user.
16291626
16301627
This function is a first pass at generating the rooms part of the sync response.
16311628
It determines which rooms have changed during the sync period, and categorises
1632-
them into four buckets: "knock", "invite", "join" and "leave".
1629+
them into four buckets: "knock", "invite", "join" and "leave". It also excludes
1630+
from that list any room that appears in the list of rooms to exclude from sync
1631+
results in the server configuration.
16331632
16341633
1. Finds all membership changes for the user in the sync period (from
16351634
`since_token` up to `now_token`).
@@ -1655,7 +1654,7 @@ async def _get_rooms_changed(
16551654
# _have_rooms_changed. We could keep the results in memory to avoid a
16561655
# second query, at the cost of more complicated source code.
16571656
membership_change_events = await self.store.get_membership_changes_for_user(
1658-
user_id, since_token.room_key, now_token.room_key, excluded_rooms
1657+
user_id, since_token.room_key, now_token.room_key, self.rooms_to_exclude
16591658
)
16601659

16611660
mem_change_events_by_room_id: Dict[str, List[EventBase]] = {}
@@ -1862,7 +1861,6 @@ async def _get_all_rooms(
18621861
self,
18631862
sync_result_builder: "SyncResultBuilder",
18641863
ignored_users: FrozenSet[str],
1865-
ignored_rooms: List[str],
18661864
) -> _RoomChanges:
18671865
"""Returns entries for all rooms for the user.
18681866
@@ -1884,7 +1882,7 @@ async def _get_all_rooms(
18841882
room_list = await self.store.get_rooms_for_local_user_where_membership_is(
18851883
user_id=user_id,
18861884
membership_list=Membership.LIST,
1887-
excluded_rooms=ignored_rooms,
1885+
excluded_rooms=self.rooms_to_exclude,
18881886
)
18891887

18901888
room_entries = []
@@ -2150,7 +2148,9 @@ async def _generate_room_entry(
21502148
raise Exception("Unrecognized rtype: %r", room_builder.rtype)
21512149

21522150
async def get_rooms_for_user_at(
2153-
self, user_id: str, room_key: RoomStreamToken
2151+
self,
2152+
user_id: str,
2153+
room_key: RoomStreamToken,
21542154
) -> FrozenSet[str]:
21552155
"""Get set of joined rooms for a user at the given stream ordering.
21562156
@@ -2176,7 +2176,12 @@ async def get_rooms_for_user_at(
21762176
# If the membership's stream ordering is after the given stream
21772177
# ordering, we need to go and work out if the user was in the room
21782178
# before.
2179+
# We also need to check whether the room should be excluded from sync
2180+
# responses as per the homeserver config.
21792181
for joined_room in joined_rooms:
2182+
if joined_room.room_id in self.rooms_to_exclude:
2183+
continue
2184+
21802185
if not joined_room.event_pos.persisted_after(room_key):
21812186
joined_room_ids.add(joined_room.room_id)
21822187
continue

tests/rest/client/test_sync.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,3 +948,24 @@ def test_invite(self) -> None:
948948

949949
self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["invite"])
950950
self.assertIn(self.included_room_id, channel.json_body["rooms"]["invite"])
951+
952+
def test_incremental_sync(self) -> None:
953+
"""Tests that activity in the room is properly filtered out of incremental
954+
syncs.
955+
"""
956+
channel = self.make_request("GET", "/sync", access_token=self.tok)
957+
self.assertEqual(channel.code, 200, channel.result)
958+
next_batch = channel.json_body["next_batch"]
959+
960+
self.helper.send(self.excluded_room_id, tok=self.tok)
961+
self.helper.send(self.included_room_id, tok=self.tok)
962+
963+
channel = self.make_request(
964+
"GET",
965+
f"/sync?since={next_batch}",
966+
access_token=self.tok,
967+
)
968+
self.assertEqual(channel.code, 200, channel.result)
969+
970+
self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["join"])
971+
self.assertIn(self.included_room_id, channel.json_body["rooms"]["join"])

0 commit comments

Comments
 (0)