MatrixSynapse/synapse/api/filtering.py

270 lines
9.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-01-07 05:26:29 +01:00
# Copyright 2015, 2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2015-01-28 17:45:18 +01:00
from synapse.api.errors import SynapseError
from synapse.types import UserID, RoomID
2016-01-28 18:19:51 +01:00
import ujson as json
class Filtering(object):
def __init__(self, hs):
super(Filtering, self).__init__()
self.store = hs.get_datastore()
def get_user_filter(self, user_localpart, filter_id):
result = self.store.get_user_filter(user_localpart, filter_id)
result.addCallback(FilterCollection)
return result
2015-01-28 17:45:18 +01:00
def add_user_filter(self, user_localpart, user_filter):
2016-01-22 11:41:30 +01:00
self.check_valid_filter(user_filter)
2015-01-28 17:45:18 +01:00
return self.store.add_user_filter(user_localpart, user_filter)
# TODO(paul): surely we should probably add a delete_user_filter or
# replace_user_filter at some point? There's no REST API specified for
# them however
2015-01-28 17:45:18 +01:00
2016-01-22 11:41:30 +01:00
def check_valid_filter(self, user_filter_json):
2015-01-28 17:45:18 +01:00
"""Check if the provided filter is valid.
This inspects all definitions contained within the filter.
Args:
user_filter_json(dict): The filter
2015-01-28 17:45:18 +01:00
Raises:
SynapseError: If the filter is not valid.
"""
# NB: Filters are the complete json blobs. "Definitions" are an
# individual top-level key e.g. public_user_data. Filters are made of
# many definitions.
top_level_definitions = [
"presence", "account_data"
2015-01-28 17:45:18 +01:00
]
room_level_definitions = [
2015-11-18 16:31:04 +01:00
"state", "timeline", "ephemeral", "account_data"
2015-01-28 17:45:18 +01:00
]
for key in top_level_definitions:
if key in user_filter_json:
self._check_definition(user_filter_json[key])
2015-01-28 17:45:18 +01:00
if "room" in user_filter_json:
2015-12-21 22:10:41 +01:00
self._check_definition_room_lists(user_filter_json["room"])
2015-01-28 17:45:18 +01:00
for key in room_level_definitions:
if key in user_filter_json["room"]:
self._check_definition(user_filter_json["room"][key])
2015-01-28 17:45:18 +01:00
2015-12-21 22:10:41 +01:00
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.
2015-12-21 22:10:41 +01:00
"""
# 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)
2015-01-28 17:45:18 +01:00
def _check_definition(self, definition):
"""Check if the provided definition is valid.
This inspects not only the types but also the values to make sure they
make sense.
Args:
definition(dict): The filter definition
Raises:
SynapseError: If there was a problem with this definition.
"""
# NB: Filters are the complete json blobs. "Definitions" are an
# individual top-level key e.g. public_user_data. Filters are made of
# many definitions.
if type(definition) != dict:
raise SynapseError(
400, "Expected JSON object, not %s" % (definition,)
)
2015-12-21 22:10:41 +01:00
self._check_definition_room_lists(definition)
2015-01-28 17:45:18 +01:00
# check senders are valid user IDs
user_id_keys = ["senders", "not_senders"]
for key in user_id_keys:
if key in definition:
if type(definition[key]) != list:
raise SynapseError(400, "Expected %s to be a list." % key)
for user_id in definition[key]:
UserID.from_string(user_id)
# TODO: We don't limit event type values but we probably should...
# check types are valid event types
event_keys = ["types", "not_types"]
for key in event_keys:
if key in definition:
if type(definition[key]) != list:
raise SynapseError(400, "Expected %s to be a list." % key)
for event_type in definition[key]:
if not isinstance(event_type, basestring):
raise SynapseError(400, "Event type should be a string")
class FilterCollection(object):
def __init__(self, filter_json):
2016-01-22 11:41:30 +01:00
self._filter_json = filter_json
2016-01-22 11:41:30 +01:00
room_filter_json = self._filter_json.get("room", {})
2016-01-22 11:41:30 +01:00
self._room_filter = Filter({
2015-12-21 22:10:41 +01:00
k: v for k, v in room_filter_json.items()
if k in ("rooms", "not_rooms")
})
2016-01-22 11:41:30 +01:00
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(filter_json.get("presence", {}))
self._account_data = Filter(filter_json.get("account_data", {}))
2016-01-22 11:41:30 +01:00
self.include_leave = filter_json.get("room", {}).get(
"include_leave", False
)
2016-01-28 18:19:51 +01:00
def __repr__(self):
return "<FilterCollection %s>" % (json.dumps(self._filter_json),)
2016-01-22 11:41:30 +01:00
def get_filter_json(self):
return self._filter_json
def timeline_limit(self):
2016-01-22 11:41:30 +01:00
return self._room_timeline_filter.limit()
def presence_limit(self):
2016-01-22 11:41:30 +01:00
return self._presence_filter.limit()
def ephemeral_limit(self):
2016-01-22 11:41:30 +01:00
return self._room_ephemeral_filter.limit()
def filter_presence(self, events):
2016-01-22 11:41:30 +01:00
return self._presence_filter.filter(events)
def filter_account_data(self, events):
2016-01-22 11:41:30 +01:00
return self._account_data.filter(events)
def filter_room_state(self, events):
2016-01-22 11:41:30 +01:00
return self._room_state_filter.filter(self._room_filter.filter(events))
def filter_room_timeline(self, events):
2016-01-22 11:41:30 +01:00
return self._room_timeline_filter.filter(self._room_filter.filter(events))
def filter_room_ephemeral(self, events):
2016-01-22 11:41:30 +01:00
return self._room_ephemeral_filter.filter(self._room_filter.filter(events))
2015-11-18 16:31:04 +01:00
def filter_room_account_data(self, events):
2016-01-22 11:41:30 +01:00
return self._room_account_data.filter(self._room_filter.filter(events))
2015-11-02 17:23:15 +01:00
class Filter(object):
def __init__(self, filter_json):
self.filter_json = filter_json
def check(self, event):
2015-10-20 16:47:42 +02:00
"""Checks whether the filter matches the given event.
Returns:
bool: True if the event matches
"""
2016-01-25 11:10:44 +01:00
sender = event.get("sender", None)
if not sender:
# Presence events have their 'sender' in content.user_id
2016-01-25 14:12:35 +01:00
sender = event.get("content", {}).get("user_id", None)
2016-01-25 11:10:44 +01:00
return self.check_fields(
event.get("room_id", None),
sender,
event.get("type", None),
)
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: room_id == v,
"senders": lambda v: sender == v,
"types": lambda v: _matches_wildcard(event_type, v)
}
for name, match_func in literal_keys.items():
not_name = "not_%s" % (name,)
disallowed_values = self.filter_json.get(not_name, [])
if any(map(match_func, disallowed_values)):
return False
allowed_values = self.filter_json.get(name, None)
if allowed_values is not None:
if not any(map(match_func, allowed_values)):
return False
return True
def filter_rooms(self, room_ids):
"""Apply the 'rooms' filter to a given list of rooms.
Args:
room_ids (list): A list of room_ids.
Returns:
list: A list of room_ids that match the filter
"""
room_ids = set(room_ids)
disallowed_rooms = set(self.filter_json.get("not_rooms", []))
room_ids -= disallowed_rooms
allowed_rooms = self.filter_json.get("rooms", None)
if allowed_rooms is not None:
room_ids &= set(allowed_rooms)
return room_ids
def filter(self, events):
return filter(self.check, events)
def limit(self):
return self.filter_json.get("limit", 10)
def _matches_wildcard(actual_value, filter_value):
if filter_value.endswith("*"):
type_prefix = filter_value[:-1]
return actual_value.startswith(type_prefix)
else:
return actual_value == filter_value
2016-01-22 11:41:30 +01:00
DEFAULT_FILTER_COLLECTION = FilterCollection({})