diff options
author | Mark Haines <mjark@negativecurvature.net> | 2015-10-22 17:14:52 +0100 |
---|---|---|
committer | Mark Haines <mjark@negativecurvature.net> | 2015-10-22 17:14:52 +0100 |
commit | b051781ddb729b1105cd16ffa5112b1de0faf613 (patch) | |
tree | aeee8052828c51e46eeae5d0780b0393611066ae | |
parent | Merge pull request #324 from matrix-org/erikj/search (diff) | |
parent | Don't assume that the event has a room_id or sender (diff) | |
download | synapse-b051781ddb729b1105cd16ffa5112b1de0faf613.tar.xz |
Merge pull request #325 from matrix-org/markjh/filter_dicts
Support filtering events represented as dicts.
-rw-r--r-- | synapse/api/filtering.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/synapse/api/filtering.py b/synapse/api/filtering.py index ab14b47281..eb15d8c54a 100644 --- a/synapse/api/filtering.py +++ b/synapse/api/filtering.py @@ -183,10 +183,29 @@ class Filter(object): Returns: bool: True if the event matches """ + if isinstance(event, dict): + return self.check_fields( + event.get("room_id", None), + event.get("sender", None), + event.get("type", None), + ) + else: + return self.check_fields( + getattr(event, "room_id", None), + getattr(event, "sender", None), + event.type, + ) + + def check_fields(self, room_id, sender, event_type): + """Checks whether the filter matches the given event fields. + + Returns: + bool: True if the event fields match + """ literal_keys = { - "rooms": lambda v: event.room_id == v, - "senders": lambda v: event.sender == v, - "types": lambda v: _matches_wildcard(event.type, v) + "rooms": lambda v: room_id == v, + "senders": lambda v: sender == v, + "types": lambda v: _matches_wildcard(event_type, v) } for name, match_func in literal_keys.items(): |