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

Return early on /sync code paths if a '*' filter is used #1640

Merged
merged 1 commit into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ def filter_room_ephemeral(self, events):
def filter_room_account_data(self, events):
return self._room_account_data.filter(self._room_filter.filter(events))

def blocks_all_presence(self):
return (
self._presence_filter.filters_all_types() or
self._presence_filter.filters_all_senders()
)

def blocks_all_room_ephemeral(self):
return (
self._room_ephemeral_filter.filters_all_types() or
self._room_ephemeral_filter.filters_all_senders() or
self._room_ephemeral_filter.filters_all_rooms()
)

def blocks_all_room_timeline(self):
return (
self._room_timeline_filter.filters_all_types() or
self._room_timeline_filter.filters_all_senders() or
self._room_timeline_filter.filters_all_rooms()
)


class Filter(object):
def __init__(self, filter_json):
Expand All @@ -218,6 +238,15 @@ def __init__(self, filter_json):

self.contains_url = self.filter_json.get("contains_url", None)

def filters_all_types(self):
return "*" in self.not_types

def filters_all_senders(self):
return "*" in self.not_senders

def filters_all_rooms(self):
return "*" in self.not_rooms

def check(self, event):
"""Checks whether the filter matches the given event.

Expand Down
31 changes: 22 additions & 9 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def _load_filtered_recents(self, room_id, sync_config, now_token,
"""
with Measure(self.clock, "load_filtered_recents"):
timeline_limit = sync_config.filter_collection.timeline_limit()
block_all_timeline = sync_config.filter_collection.blocks_all_room_timeline()

if recents is None or newly_joined_room or timeline_limit < len(recents):
limited = True
Expand All @@ -293,7 +294,7 @@ def _load_filtered_recents(self, room_id, sync_config, now_token,
else:
recents = []

if not limited:
if not limited or block_all_timeline:
defer.returnValue(TimelineBatch(
events=recents,
prev_batch=now_token,
Expand Down Expand Up @@ -531,9 +532,14 @@ def generate_sync_result(self, sync_config, since_token=None, full_state=False):
)
newly_joined_rooms, newly_joined_users = res

yield self._generate_sync_entry_for_presence(
sync_result_builder, newly_joined_rooms, newly_joined_users
block_all_presence_data = (
since_token is None and
sync_config.filter_collection.blocks_all_presence()
)
if not block_all_presence_data:
yield self._generate_sync_entry_for_presence(
sync_result_builder, newly_joined_rooms, newly_joined_users
)

yield self._generate_sync_entry_for_to_device(sync_result_builder)

Expand Down Expand Up @@ -709,13 +715,20 @@ def _generate_sync_entry_for_rooms(self, sync_result_builder, account_data_by_ro
`(newly_joined_rooms, newly_joined_users)`
"""
user_id = sync_result_builder.sync_config.user.to_string()

now_token, ephemeral_by_room = yield self.ephemeral_by_room(
sync_result_builder.sync_config,
now_token=sync_result_builder.now_token,
since_token=sync_result_builder.since_token,
block_all_room_ephemeral = (
sync_result_builder.since_token is None and
sync_result_builder.sync_config.filter_collection.blocks_all_room_ephemeral()
)
sync_result_builder.now_token = now_token

if block_all_room_ephemeral:
ephemeral_by_room = {}
else:
now_token, ephemeral_by_room = yield self.ephemeral_by_room(
sync_result_builder.sync_config,
now_token=sync_result_builder.now_token,
since_token=sync_result_builder.since_token,
)
sync_result_builder.now_token = now_token

ignored_account_data = yield self.store.get_global_account_data_by_type_for_user(
"m.ignored_user_list", user_id=user_id,
Expand Down