2015-01-27 15:28:56 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 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
|
2015-01-27 17:17:56 +01:00
|
|
|
|
2015-01-27 15:28:56 +01:00
|
|
|
|
|
|
|
class Filtering(object):
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(Filtering, self).__init__()
|
2015-01-27 18:48:13 +01:00
|
|
|
self.store = hs.get_datastore()
|
2015-01-27 15:28:56 +01:00
|
|
|
|
|
|
|
def get_user_filter(self, user_localpart, filter_id):
|
2015-01-29 18:41:48 +01:00
|
|
|
result = self.store.get_user_filter(user_localpart, filter_id)
|
2015-10-20 16:33:25 +02:00
|
|
|
result.addCallback(FilterCollection)
|
2015-01-29 18:41:48 +01:00
|
|
|
return result
|
2015-01-27 15:28:56 +01:00
|
|
|
|
2015-01-28 17:45:18 +01:00
|
|
|
def add_user_filter(self, user_localpart, user_filter):
|
|
|
|
self._check_valid_filter(user_filter)
|
|
|
|
return self.store.add_user_filter(user_localpart, user_filter)
|
2015-01-27 17:17:56 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2015-01-29 18:41:48 +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:
|
2015-01-29 18:41:48 +01:00
|
|
|
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 = [
|
2015-12-01 19:41:32 +01:00
|
|
|
"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:
|
2015-01-29 18:41:48 +01:00
|
|
|
if key in user_filter_json:
|
|
|
|
self._check_definition(user_filter_json[key])
|
2015-01-28 17:45:18 +01:00
|
|
|
|
2015-01-29 18:41:48 +01:00
|
|
|
if "room" in user_filter_json:
|
2015-01-28 17:45:18 +01:00
|
|
|
for key in room_level_definitions:
|
2015-01-29 18:41:48 +01:00
|
|
|
if key in user_filter_json["room"]:
|
|
|
|
self._check_definition(user_filter_json["room"][key])
|
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,)
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
class FilterCollection(object):
|
2015-01-29 18:41:48 +01:00
|
|
|
def __init__(self, filter_json):
|
|
|
|
self.filter_json = filter_json
|
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
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", {})
|
|
|
|
)
|
|
|
|
|
2015-11-18 16:31:04 +01:00
|
|
|
self.room_account_data = Filter(
|
|
|
|
self.filter_json.get("room", {}).get("account_data", {})
|
2015-11-02 17:23:15 +01:00
|
|
|
)
|
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
self.presence_filter = Filter(
|
|
|
|
self.filter_json.get("presence", {})
|
|
|
|
)
|
|
|
|
|
2015-12-01 19:41:32 +01:00
|
|
|
self.account_data = Filter(
|
|
|
|
self.filter_json.get("account_data", {})
|
|
|
|
)
|
|
|
|
|
2015-12-04 18:32:09 +01:00
|
|
|
self.include_leave = self.filter_json.get("room", {}).get(
|
|
|
|
"include_leave", False
|
|
|
|
)
|
|
|
|
|
2015-10-13 15:12:43 +02:00
|
|
|
def timeline_limit(self):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.room_timeline_filter.limit()
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2015-10-13 15:12:43 +02:00
|
|
|
def presence_limit(self):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.presence_filter.limit()
|
2015-10-13 15:12:43 +02:00
|
|
|
|
|
|
|
def ephemeral_limit(self):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.room_ephemeral_filter.limit()
|
2015-10-13 15:12:43 +02:00
|
|
|
|
|
|
|
def filter_presence(self, events):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.presence_filter.filter(events)
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2015-12-01 19:41:32 +01:00
|
|
|
def filter_account_data(self, events):
|
|
|
|
return self.account_data.filter(events)
|
|
|
|
|
2015-01-29 18:41:48 +01:00
|
|
|
def filter_room_state(self, events):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.room_state_filter.filter(events)
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2015-10-13 15:12:43 +02:00
|
|
|
def filter_room_timeline(self, events):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.room_timeline_filter.filter(events)
|
2015-01-29 18:41:48 +01:00
|
|
|
|
|
|
|
def filter_room_ephemeral(self, events):
|
2015-10-20 16:33:25 +02:00
|
|
|
return self.room_ephemeral_filter.filter(events)
|
2015-10-13 15:12:43 +02:00
|
|
|
|
2015-11-18 16:31:04 +01:00
|
|
|
def filter_room_account_data(self, events):
|
|
|
|
return self.room_account_data.filter(events)
|
2015-11-02 17:23:15 +01:00
|
|
|
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
class Filter(object):
|
|
|
|
def __init__(self, filter_json):
|
|
|
|
self.filter_json = filter_json
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
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
|
|
|
|
"""
|
2015-10-22 17:38:03 +02:00
|
|
|
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(
|
2015-10-22 18:08:59 +02:00
|
|
|
getattr(event, "room_id", None),
|
|
|
|
getattr(event, "sender", None),
|
2015-10-22 17:38:03 +02:00
|
|
|
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
|
|
|
|
"""
|
2015-10-20 16:33:25 +02:00
|
|
|
literal_keys = {
|
2015-10-22 17:38:03 +02:00
|
|
|
"rooms": lambda v: room_id == v,
|
|
|
|
"senders": lambda v: sender == v,
|
|
|
|
"types": lambda v: _matches_wildcard(event_type, v)
|
2015-10-20 16:33:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)):
|
2015-01-29 18:41:48 +01:00
|
|
|
return False
|
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
allowed_values = self.filter_json.get(name, None)
|
|
|
|
if allowed_values is not None:
|
|
|
|
if not any(map(match_func, allowed_values)):
|
2015-01-29 18:41:48 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-10-21 11:09:26 +02:00
|
|
|
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
|
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
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
|