From 993aba1ce25aae82bd8127efad769095d032c025 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 3 Oct 2024 11:42:46 +0100 Subject: [PATCH 1/4] Move call invite filtering logic to `filter_events_for_client` This allows SSS to benefit from it as well. --- synapse/handlers/sync.py | 12 +----------- synapse/visibility.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index f4ea90fbd7..2f5dca3eef 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -46,7 +46,6 @@ from synapse.api.constants import ( Direction, EventContentFields, EventTypes, - JoinRules, Membership, ) from synapse.api.filtering import FilterCollection @@ -948,22 +947,13 @@ class SyncHandler: ) ) - filtered_recents = await filter_events_for_client( + loaded_recents = await filter_events_for_client( self._storage_controllers, sync_config.user.to_string(), loaded_recents, always_include_ids=current_state_ids, ) - loaded_recents = [] - for event in filtered_recents: - if event.type == EventTypes.CallInvite: - room_info = await self.store.get_room_with_stats(event.room_id) - assert room_info is not None - if room_info.join_rules == JoinRules.PUBLIC: - continue - loaded_recents.append(event) - log_kv({"loaded_recents_after_client_filtering": len(loaded_recents)}) loaded_recents.extend(recents) diff --git a/synapse/visibility.py b/synapse/visibility.py index 3a2782bade..9b92e109f1 100644 --- a/synapse/visibility.py +++ b/synapse/visibility.py @@ -40,6 +40,7 @@ from synapse.api.constants import ( EventTypes, EventUnsignedContentFields, HistoryVisibility, + JoinRules, Membership, ) from synapse.events import EventBase @@ -117,7 +118,7 @@ async def filter_events_for_client( [event.event_id for event in events], ) - types = (_HISTORY_VIS_KEY, (EventTypes.Member, user_id)) + types = (_HISTORY_VIS_KEY, (EventTypes.Member, user_id), (EventTypes.JoinRules, "")) # we exclude outliers at this point, and then handle them separately later event_id_to_state = await storage.state.get_state_for_events( @@ -156,6 +157,16 @@ async def filter_events_for_client( if filtered is None: return None + # Filter out call invites in public rooms, as this would potentially + # ring a lot of users. + if state_after_event is not None and event.type == EventTypes.CallInvite: + room_join_rules = state_after_event.get((EventTypes.JoinRules, "")) + if ( + room_join_rules is not None + and room_join_rules.content.get("join_rule") == JoinRules.PUBLIC + ): + return None + # Annotate the event with the user's membership after the event. # # Normally we just look in `state_after_event`, but if the event is an outlier From fe5de1bad42f38a6499bae2ed3b95efa14fba13a Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 3 Oct 2024 12:13:30 +0100 Subject: [PATCH 2/4] Bump up number of db txns in aggregations test It appears there's an additional DB transaction as the JoinRules state chunk is not cached for these tests. --- tests/rest/client/test_relations.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/rest/client/test_relations.py b/tests/rest/client/test_relations.py index f5a7602d0a..67fde03bfe 100644 --- a/tests/rest/client/test_relations.py +++ b/tests/rest/client/test_relations.py @@ -1181,7 +1181,7 @@ class BundledAggregationsTestCase(BaseRelationsTestCase): bundled_aggregations, ) - self._test_bundled_aggregations(RelationTypes.REFERENCE, assert_annotations, 6) + self._test_bundled_aggregations(RelationTypes.REFERENCE, assert_annotations, 7) def test_thread(self) -> None: """ @@ -1226,7 +1226,7 @@ class BundledAggregationsTestCase(BaseRelationsTestCase): # The "user" sent the root event and is making queries for the bundled # aggregations: they have participated. - self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(True), 6) + self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(True), 7) # The "user2" sent replies in the thread and is making queries for the # bundled aggregations: they have participated. # @@ -1287,7 +1287,7 @@ class BundledAggregationsTestCase(BaseRelationsTestCase): bundled_aggregations["latest_event"].get("unsigned"), ) - self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 6) + self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 7) def test_nested_thread(self) -> None: """ From 2a05868056cc1880d4fbdde03e2ef93b19a63184 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 3 Oct 2024 12:08:12 +0100 Subject: [PATCH 3/4] remove TODO --- synapse/handlers/sliding_sync/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/synapse/handlers/sliding_sync/__init__.py b/synapse/handlers/sliding_sync/__init__.py index 9fcc68ff25..79d306662d 100644 --- a/synapse/handlers/sliding_sync/__init__.py +++ b/synapse/handlers/sliding_sync/__init__.py @@ -673,8 +673,6 @@ class SlidingSyncHandler: != Membership.JOIN, filter_send_to_client=True, ) - # TODO: Filter out `EventTypes.CallInvite` in public rooms, - # see https://github.com/element-hq/synapse/issues/17359 # TODO: Handle timeline gaps (`get_timeline_gaps()`) From 65d8bea060794015353236cdc9d79b19868f5b77 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 3 Oct 2024 11:44:34 +0100 Subject: [PATCH 4/4] newsfile --- changelog.d/17782.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/17782.misc diff --git a/changelog.d/17782.misc b/changelog.d/17782.misc new file mode 100644 index 0000000000..d7321470d0 --- /dev/null +++ b/changelog.d/17782.misc @@ -0,0 +1 @@ +Improve event filtering for Simplified Sliding Sync. \ No newline at end of file