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

Commit

Permalink
Merge pull request #454 from matrix-org/markjh/room_filtering
Browse files Browse the repository at this point in the history
Add top level filters for filtering by room id

Documented by matrix-org/matrix-spec-proposals#246
  • Loading branch information
NegativeMjark committed Dec 22, 2015
2 parents 2e2eeb4 + b9b4466 commit e525b46
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
1 change: 0 additions & 1 deletion jenkins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export PYTHONDONTWRITEBYTECODE=yep
export TRIAL_FLAGS="--reporter=subunit"
export TOXSUFFIX="| subunit-1to2 | subunit2junitxml --no-passthrough --output-to=results.xml"
# Write coverage reports to a separate file for each process
# Include branch coverage
export COVERAGE_OPTS="-p"
export DUMP_COVERAGE_COMMAND="coverage help"

Expand Down
68 changes: 35 additions & 33 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,29 @@ def _check_valid_filter(self, user_filter_json):
self._check_definition(user_filter_json[key])

if "room" in user_filter_json:
self._check_definition_room_lists(user_filter_json["room"])
for key in room_level_definitions:
if key in user_filter_json["room"]:
self._check_definition(user_filter_json["room"][key])

def _check_definition_room_lists(self, definition):
"""Check that "rooms" and "not_rooms" are lists of room ids if they
are present
Args:
definition(dict): The filter definition
Raises:
SynapseError: If there was a problem with this definition.
"""
# check rooms are valid room IDs
room_id_keys = ["rooms", "not_rooms"]
for key in room_id_keys:
if key in definition:
if type(definition[key]) != list:
raise SynapseError(400, "Expected %s to be a list." % key)
for room_id in definition[key]:
RoomID.from_string(room_id)

def _check_definition(self, definition):
"""Check if the provided definition is valid.
Expand All @@ -85,14 +104,7 @@ def _check_definition(self, definition):
400, "Expected JSON object, not %s" % (definition,)
)

# check rooms are valid room IDs
room_id_keys = ["rooms", "not_rooms"]
for key in room_id_keys:
if key in definition:
if type(definition[key]) != list:
raise SynapseError(400, "Expected %s to be a list." % key)
for room_id in definition[key]:
RoomID.from_string(room_id)
self._check_definition_room_lists(definition)

# check senders are valid user IDs
user_id_keys = ["senders", "not_senders"]
Expand All @@ -119,29 +131,19 @@ class FilterCollection(object):
def __init__(self, filter_json):
self.filter_json = filter_json

self.room_timeline_filter = Filter(
self.filter_json.get("room", {}).get("timeline", {})
)

self.room_state_filter = Filter(
self.filter_json.get("room", {}).get("state", {})
)

self.room_ephemeral_filter = Filter(
self.filter_json.get("room", {}).get("ephemeral", {})
)

self.room_account_data = Filter(
self.filter_json.get("room", {}).get("account_data", {})
)
room_filter_json = self.filter_json.get("room", {})

self.presence_filter = Filter(
self.filter_json.get("presence", {})
)
self.room_filter = Filter({
k: v for k, v in room_filter_json.items()
if k in ("rooms", "not_rooms")
})

self.account_data = Filter(
self.filter_json.get("account_data", {})
)
self.room_timeline_filter = Filter(room_filter_json.get("timeline", {}))
self.room_state_filter = Filter(room_filter_json.get("state", {}))
self.room_ephemeral_filter = Filter(room_filter_json.get("ephemeral", {}))
self.room_account_data = Filter(room_filter_json.get("account_data", {}))
self.presence_filter = Filter(self.filter_json.get("presence", {}))
self.account_data = Filter(self.filter_json.get("account_data", {}))

self.include_leave = self.filter_json.get("room", {}).get(
"include_leave", False
Expand All @@ -163,16 +165,16 @@ def filter_account_data(self, events):
return self.account_data.filter(events)

def filter_room_state(self, events):
return self.room_state_filter.filter(events)
return self.room_state_filter.filter(self.room_filter.filter(events))

def filter_room_timeline(self, events):
return self.room_timeline_filter.filter(events)
return self.room_timeline_filter.filter(self.room_filter.filter(events))

def filter_room_ephemeral(self, events):
return self.room_ephemeral_filter.filter(events)
return self.room_ephemeral_filter.filter(self.room_filter.filter(events))

def filter_room_account_data(self, events):
return self.room_account_data.filter(events)
return self.room_account_data.filter(self.room_filter.filter(events))


class Filter(object):
Expand Down

0 comments on commit e525b46

Please sign in to comment.