2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2019-11-05 11:56:39 +01:00
|
|
|
# Copyright 2017 Vector Creations Ltd
|
|
|
|
# Copyright 2018-2019 New Vector Ltd
|
2021-11-09 14:10:58 +01:00
|
|
|
# Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2015-01-27 15:28:56 +01:00
|
|
|
#
|
|
|
|
# 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.
|
2020-09-08 13:33:48 +02:00
|
|
|
import json
|
2021-10-01 13:02:32 +02:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Awaitable,
|
2021-10-27 17:26:30 +02:00
|
|
|
Callable,
|
2022-05-10 20:10:22 +02:00
|
|
|
Collection,
|
2021-10-27 17:26:30 +02:00
|
|
|
Dict,
|
2021-10-01 13:02:32 +02:00
|
|
|
Iterable,
|
|
|
|
List,
|
2022-02-28 18:40:24 +01:00
|
|
|
Mapping,
|
2021-10-01 13:02:32 +02:00
|
|
|
Optional,
|
|
|
|
Set,
|
|
|
|
TypeVar,
|
|
|
|
Union,
|
|
|
|
)
|
2020-01-20 18:34:13 +01:00
|
|
|
|
2017-03-23 15:42:07 +01:00
|
|
|
import jsonschema
|
2017-03-23 15:42:41 +01:00
|
|
|
from jsonschema import FormatChecker
|
2017-03-23 15:42:07 +01:00
|
|
|
|
2022-05-27 13:14:36 +02:00
|
|
|
from synapse.api.constants import EduTypes, EventContentFields
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.api.errors import SynapseError
|
2020-08-17 13:24:46 +02:00
|
|
|
from synapse.api.presence import UserPresenceState
|
2022-10-17 17:32:11 +02:00
|
|
|
from synapse.events import EventBase, relation_from_event
|
2021-10-01 13:02:32 +02:00
|
|
|
from synapse.types import JsonDict, RoomID, UserID
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2017-03-23 15:42:07 +01:00
|
|
|
FILTER_SCHEMA = {
|
2022-11-07 14:49:31 +01:00
|
|
|
"additionalProperties": True, # Allow new fields for forward compatibility
|
2017-03-23 15:42:07 +01:00
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2019-06-20 11:32:02 +02:00
|
|
|
"limit": {"type": "number"},
|
|
|
|
"senders": {"$ref": "#/definitions/user_id_array"},
|
|
|
|
"not_senders": {"$ref": "#/definitions/user_id_array"},
|
2017-03-23 15:42:07 +01:00
|
|
|
# TODO: We don't limit event type values but we probably should...
|
|
|
|
# check types are valid event types
|
2019-06-20 11:32:02 +02:00
|
|
|
"types": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"not_types": {"type": "array", "items": {"type": "string"}},
|
2022-10-17 17:32:11 +02:00
|
|
|
# MSC3874, filtering /messages.
|
|
|
|
"org.matrix.msc3874.rel_types": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"org.matrix.msc3874.not_rel_types": {
|
|
|
|
"type": "array",
|
|
|
|
"items": {"type": "string"},
|
|
|
|
},
|
2019-06-20 11:32:02 +02:00
|
|
|
},
|
2017-03-23 15:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ROOM_FILTER_SCHEMA = {
|
2022-11-07 14:49:31 +01:00
|
|
|
"additionalProperties": True, # Allow new fields for forward compatibility
|
2017-03-23 15:42:07 +01:00
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2019-06-20 11:32:02 +02:00
|
|
|
"not_rooms": {"$ref": "#/definitions/room_id_array"},
|
|
|
|
"rooms": {"$ref": "#/definitions/room_id_array"},
|
|
|
|
"ephemeral": {"$ref": "#/definitions/room_event_filter"},
|
|
|
|
"include_leave": {"type": "boolean"},
|
|
|
|
"state": {"$ref": "#/definitions/room_event_filter"},
|
|
|
|
"timeline": {"$ref": "#/definitions/room_event_filter"},
|
|
|
|
"account_data": {"$ref": "#/definitions/room_event_filter"},
|
|
|
|
},
|
2017-03-23 15:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ROOM_EVENT_FILTER_SCHEMA = {
|
2022-11-07 14:49:31 +01:00
|
|
|
"additionalProperties": True, # Allow new fields for forward compatibility
|
2017-03-23 15:42:07 +01:00
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2019-06-20 11:32:02 +02:00
|
|
|
"limit": {"type": "number"},
|
|
|
|
"senders": {"$ref": "#/definitions/user_id_array"},
|
|
|
|
"not_senders": {"$ref": "#/definitions/user_id_array"},
|
|
|
|
"types": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"not_types": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"rooms": {"$ref": "#/definitions/room_id_array"},
|
|
|
|
"not_rooms": {"$ref": "#/definitions/room_id_array"},
|
|
|
|
"contains_url": {"type": "boolean"},
|
|
|
|
"lazy_load_members": {"type": "boolean"},
|
|
|
|
"include_redundant_members": {"type": "boolean"},
|
2022-10-07 15:26:40 +02:00
|
|
|
"unread_thread_notifications": {"type": "boolean"},
|
2022-10-04 15:47:04 +02:00
|
|
|
"org.matrix.msc3773.unread_thread_notifications": {"type": "boolean"},
|
2019-11-01 11:30:51 +01:00
|
|
|
# Include or exclude events with the provided labels.
|
|
|
|
# cf https://github.com/matrix-org/matrix-doc/pull/2326
|
2019-10-30 16:56:33 +01:00
|
|
|
"org.matrix.labels": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"org.matrix.not_labels": {"type": "array", "items": {"type": "string"}},
|
2021-11-09 14:10:58 +01:00
|
|
|
# MSC3440, filtering by event relations.
|
2022-03-10 16:36:13 +01:00
|
|
|
"related_by_senders": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"related_by_rel_types": {"type": "array", "items": {"type": "string"}},
|
2019-06-20 11:32:02 +02:00
|
|
|
},
|
2017-03-23 15:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
USER_ID_ARRAY_SCHEMA = {
|
|
|
|
"type": "array",
|
2019-06-20 11:32:02 +02:00
|
|
|
"items": {"type": "string", "format": "matrix_user_id"},
|
2017-03-23 15:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ROOM_ID_ARRAY_SCHEMA = {
|
|
|
|
"type": "array",
|
2019-06-20 11:32:02 +02:00
|
|
|
"items": {"type": "string", "format": "matrix_room_id"},
|
2017-03-23 15:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
USER_FILTER_SCHEMA = {
|
|
|
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
|
|
|
"description": "schema for a Sync filter",
|
|
|
|
"type": "object",
|
|
|
|
"definitions": {
|
2017-03-23 15:42:41 +01:00
|
|
|
"room_id_array": ROOM_ID_ARRAY_SCHEMA,
|
2017-03-23 15:42:07 +01:00
|
|
|
"user_id_array": USER_ID_ARRAY_SCHEMA,
|
|
|
|
"filter": FILTER_SCHEMA,
|
|
|
|
"room_filter": ROOM_FILTER_SCHEMA,
|
2019-06-20 11:32:02 +02:00
|
|
|
"room_event_filter": ROOM_EVENT_FILTER_SCHEMA,
|
2017-03-23 15:42:07 +01:00
|
|
|
},
|
|
|
|
"properties": {
|
2019-06-20 11:32:02 +02:00
|
|
|
"presence": {"$ref": "#/definitions/filter"},
|
|
|
|
"account_data": {"$ref": "#/definitions/filter"},
|
|
|
|
"room": {"$ref": "#/definitions/room_filter"},
|
|
|
|
"event_format": {"type": "string", "enum": ["client", "federation"]},
|
2017-03-23 15:42:07 +01:00
|
|
|
"event_fields": {
|
|
|
|
"type": "array",
|
|
|
|
"items": {
|
|
|
|
"type": "string",
|
|
|
|
# Don't allow '\\' in event field filters. This makes matching
|
|
|
|
# events a lot easier as we can then use a negative lookbehind
|
|
|
|
# assertion to split '\.' If we allowed \\ then it would
|
|
|
|
# incorrectly split '\\.' See synapse.events.utils.serialize_event
|
2018-10-24 11:41:45 +02:00
|
|
|
#
|
|
|
|
# Note that because this is a regular expression, we have to escape
|
|
|
|
# each backslash in the pattern.
|
2019-06-20 11:32:02 +02:00
|
|
|
"pattern": r"^((?!\\\\).)*$",
|
|
|
|
},
|
|
|
|
},
|
2017-03-23 15:42:07 +01:00
|
|
|
},
|
2022-11-07 14:49:31 +01:00
|
|
|
"additionalProperties": True, # Allow new fields for forward compatibility
|
2017-03-23 15:42:07 +01:00
|
|
|
}
|
2016-01-28 18:19:51 +01:00
|
|
|
|
2015-01-27 15:28:56 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
@FormatChecker.cls_checks("matrix_room_id")
|
2022-09-06 15:29:16 +02:00
|
|
|
def matrix_room_id_validator(room_id: object) -> bool:
|
|
|
|
return isinstance(room_id, str) and RoomID.is_valid(room_id)
|
2017-03-23 15:42:41 +01:00
|
|
|
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
@FormatChecker.cls_checks("matrix_user_id")
|
2022-09-06 15:29:16 +02:00
|
|
|
def matrix_user_id_validator(user_id: object) -> bool:
|
|
|
|
return isinstance(user_id, str) and UserID.is_valid(user_id)
|
2017-03-23 15:42:41 +01:00
|
|
|
|
|
|
|
|
2020-09-04 12:54:56 +02:00
|
|
|
class Filtering:
|
2021-10-01 13:02:32 +02:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-11-09 14:10:58 +01:00
|
|
|
self._hs = hs
|
2022-02-23 12:04:02 +01:00
|
|
|
self.store = hs.get_datastores().main
|
2015-01-27 15:28:56 +01:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
self.DEFAULT_FILTER_COLLECTION = FilterCollection(hs, {})
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
async def get_user_filter(
|
|
|
|
self, user_localpart: str, filter_id: Union[int, str]
|
|
|
|
) -> "FilterCollection":
|
2020-08-06 14:30:06 +02:00
|
|
|
result = await self.store.get_user_filter(user_localpart, filter_id)
|
2021-11-09 14:10:58 +01:00
|
|
|
return FilterCollection(self._hs, result)
|
2015-01-27 15:28:56 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def add_user_filter(
|
|
|
|
self, user_localpart: str, user_filter: JsonDict
|
|
|
|
) -> Awaitable[int]:
|
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)
|
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
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def check_valid_filter(self, user_filter_json: JsonDict) -> None:
|
2015-01-28 17:45:18 +01:00
|
|
|
"""Check if the provided filter is valid.
|
|
|
|
|
|
|
|
This inspects all definitions contained within the filter.
|
|
|
|
|
|
|
|
Args:
|
2021-10-01 13:02:32 +02:00
|
|
|
user_filter_json: 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.
|
2017-03-23 15:42:07 +01:00
|
|
|
try:
|
2019-06-20 11:32:02 +02:00
|
|
|
jsonschema.validate(
|
|
|
|
user_filter_json, USER_FILTER_SCHEMA, format_checker=FormatChecker()
|
|
|
|
)
|
2017-03-23 15:42:07 +01:00
|
|
|
except jsonschema.ValidationError as e:
|
2018-09-27 13:38:50 +02:00
|
|
|
raise SynapseError(400, str(e))
|
2015-01-28 17:45:18 +01:00
|
|
|
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
# Filters work across events, presence EDUs, and account data.
|
|
|
|
FilterEvent = TypeVar("FilterEvent", EventBase, UserPresenceState, JsonDict)
|
|
|
|
|
|
|
|
|
2020-09-04 12:54:56 +02:00
|
|
|
class FilterCollection:
|
2021-11-09 14:10:58 +01:00
|
|
|
def __init__(self, hs: "HomeServer", filter_json: JsonDict):
|
2016-01-22 11:41:30 +01:00
|
|
|
self._filter_json = filter_json
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2016-01-22 11:41:30 +01:00
|
|
|
room_filter_json = self._filter_json.get("room", {})
|
2015-10-20 16:33:25 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
self._room_filter = Filter(
|
2021-11-09 14:10:58 +01:00
|
|
|
hs,
|
|
|
|
{k: v for k, v in room_filter_json.items() if k in ("rooms", "not_rooms")},
|
2019-06-20 11:32:02 +02:00
|
|
|
)
|
2015-10-20 16:33:25 +02:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
self._room_timeline_filter = Filter(hs, room_filter_json.get("timeline", {}))
|
|
|
|
self._room_state_filter = Filter(hs, room_filter_json.get("state", {}))
|
|
|
|
self._room_ephemeral_filter = Filter(hs, room_filter_json.get("ephemeral", {}))
|
2023-02-10 15:22:16 +01:00
|
|
|
self._room_account_data_filter = Filter(
|
|
|
|
hs, room_filter_json.get("account_data", {})
|
|
|
|
)
|
2021-11-09 14:10:58 +01:00
|
|
|
self._presence_filter = Filter(hs, filter_json.get("presence", {}))
|
2023-02-10 15:22:16 +01:00
|
|
|
self._global_account_data_filter = Filter(
|
|
|
|
hs, filter_json.get("account_data", {})
|
|
|
|
)
|
2015-12-01 19:41:32 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
self.include_leave = filter_json.get("room", {}).get("include_leave", False)
|
2016-11-22 11:14:05 +01:00
|
|
|
self.event_fields = filter_json.get("event_fields", [])
|
2018-09-04 16:18:25 +02:00
|
|
|
self.event_format = filter_json.get("event_format", "client")
|
2015-12-04 18:32:09 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def __repr__(self) -> str:
|
2016-01-28 18:19:51 +01:00
|
|
|
return "<FilterCollection %s>" % (json.dumps(self._filter_json),)
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def get_filter_json(self) -> JsonDict:
|
2016-01-22 11:41:30 +01:00
|
|
|
return self._filter_json
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def timeline_limit(self) -> int:
|
2021-10-27 17:26:30 +02:00
|
|
|
return self._room_timeline_filter.limit
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def presence_limit(self) -> int:
|
2021-10-27 17:26:30 +02:00
|
|
|
return self._presence_filter.limit
|
2015-10-13 15:12:43 +02:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def ephemeral_limit(self) -> int:
|
2021-10-27 17:26:30 +02:00
|
|
|
return self._room_ephemeral_filter.limit
|
2015-10-13 15:12:43 +02:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def lazy_load_members(self) -> bool:
|
2021-10-27 17:26:30 +02:00
|
|
|
return self._room_state_filter.lazy_load_members
|
2018-03-19 02:15:13 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def include_redundant_members(self) -> bool:
|
2021-10-27 17:26:30 +02:00
|
|
|
return self._room_state_filter.include_redundant_members
|
2018-07-26 23:51:30 +02:00
|
|
|
|
2022-10-04 15:47:04 +02:00
|
|
|
def unread_thread_notifications(self) -> bool:
|
|
|
|
return self._room_timeline_filter.unread_thread_notifications
|
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
async def filter_presence(
|
2023-02-03 21:03:23 +01:00
|
|
|
self, presence_states: Iterable[UserPresenceState]
|
2021-10-18 21:01:10 +02:00
|
|
|
) -> List[UserPresenceState]:
|
2023-02-03 21:03:23 +01:00
|
|
|
return await self._presence_filter.filter(presence_states)
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2023-02-10 15:22:16 +01:00
|
|
|
async def filter_global_account_data(
|
|
|
|
self, events: Iterable[JsonDict]
|
|
|
|
) -> List[JsonDict]:
|
|
|
|
return await self._global_account_data_filter.filter(events)
|
2015-12-01 19:41:32 +01:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
async def filter_room_state(self, events: Iterable[EventBase]) -> List[EventBase]:
|
|
|
|
return await self._room_state_filter.filter(
|
|
|
|
await self._room_filter.filter(events)
|
|
|
|
)
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
async def filter_room_timeline(
|
|
|
|
self, events: Iterable[EventBase]
|
|
|
|
) -> List[EventBase]:
|
|
|
|
return await self._room_timeline_filter.filter(
|
|
|
|
await self._room_filter.filter(events)
|
|
|
|
)
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
async def filter_room_ephemeral(self, events: Iterable[JsonDict]) -> List[JsonDict]:
|
|
|
|
return await self._room_ephemeral_filter.filter(
|
|
|
|
await self._room_filter.filter(events)
|
|
|
|
)
|
2015-10-13 15:12:43 +02:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
async def filter_room_account_data(
|
|
|
|
self, events: Iterable[JsonDict]
|
|
|
|
) -> List[JsonDict]:
|
2023-02-10 15:22:16 +01:00
|
|
|
return await self._room_account_data_filter.filter(
|
2021-11-09 14:10:58 +01:00
|
|
|
await self._room_filter.filter(events)
|
|
|
|
)
|
2015-11-02 17:23:15 +01:00
|
|
|
|
2023-02-02 19:45:12 +01:00
|
|
|
def blocks_all_rooms(self) -> bool:
|
|
|
|
return self._room_filter.filters_all_rooms()
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def blocks_all_presence(self) -> bool:
|
2016-11-22 17:38:35 +01:00
|
|
|
return (
|
2019-06-20 11:32:02 +02:00
|
|
|
self._presence_filter.filters_all_types()
|
|
|
|
or self._presence_filter.filters_all_senders()
|
2016-11-22 17:38:35 +01:00
|
|
|
)
|
|
|
|
|
2023-02-10 15:22:16 +01:00
|
|
|
def blocks_all_global_account_data(self) -> bool:
|
|
|
|
"""True if all global acount data will be filtered out."""
|
|
|
|
return (
|
|
|
|
self._global_account_data_filter.filters_all_types()
|
|
|
|
or self._global_account_data_filter.filters_all_senders()
|
|
|
|
)
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def blocks_all_room_ephemeral(self) -> bool:
|
2016-11-22 17:38:35 +01:00
|
|
|
return (
|
2019-06-20 11:32:02 +02:00
|
|
|
self._room_ephemeral_filter.filters_all_types()
|
|
|
|
or self._room_ephemeral_filter.filters_all_senders()
|
|
|
|
or self._room_ephemeral_filter.filters_all_rooms()
|
2016-11-22 17:38:35 +01:00
|
|
|
)
|
|
|
|
|
2023-02-10 15:22:16 +01:00
|
|
|
def blocks_all_room_account_data(self) -> bool:
|
|
|
|
return (
|
|
|
|
self._room_account_data_filter.filters_all_types()
|
|
|
|
or self._room_account_data_filter.filters_all_senders()
|
|
|
|
or self._room_account_data_filter.filters_all_rooms()
|
|
|
|
)
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def blocks_all_room_timeline(self) -> bool:
|
2016-11-22 17:38:35 +01:00
|
|
|
return (
|
2019-06-20 11:32:02 +02:00
|
|
|
self._room_timeline_filter.filters_all_types()
|
|
|
|
or self._room_timeline_filter.filters_all_senders()
|
|
|
|
or self._room_timeline_filter.filters_all_rooms()
|
2016-11-22 17:38:35 +01:00
|
|
|
)
|
|
|
|
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2020-09-04 12:54:56 +02:00
|
|
|
class Filter:
|
2021-11-09 14:10:58 +01:00
|
|
|
def __init__(self, hs: "HomeServer", filter_json: JsonDict):
|
|
|
|
self._hs = hs
|
2022-02-23 12:04:02 +01:00
|
|
|
self._store = hs.get_datastores().main
|
2015-10-20 16:33:25 +02:00
|
|
|
self.filter_json = filter_json
|
2015-01-29 18:41:48 +01:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
self.limit = filter_json.get("limit", 10)
|
|
|
|
self.lazy_load_members = filter_json.get("lazy_load_members", False)
|
|
|
|
self.include_redundant_members = filter_json.get(
|
|
|
|
"include_redundant_members", False
|
|
|
|
)
|
2022-10-07 15:26:40 +02:00
|
|
|
self.unread_thread_notifications: bool = filter_json.get(
|
|
|
|
"unread_thread_notifications", False
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
not self.unread_thread_notifications
|
|
|
|
and hs.config.experimental.msc3773_enabled
|
|
|
|
):
|
|
|
|
self.unread_thread_notifications = filter_json.get(
|
2022-10-04 15:47:04 +02:00
|
|
|
"org.matrix.msc3773.unread_thread_notifications", False
|
|
|
|
)
|
2021-10-27 17:26:30 +02:00
|
|
|
|
|
|
|
self.types = filter_json.get("types", None)
|
|
|
|
self.not_types = filter_json.get("not_types", [])
|
2016-07-14 16:15:52 +02:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
self.rooms = filter_json.get("rooms", None)
|
|
|
|
self.not_rooms = filter_json.get("not_rooms", [])
|
2016-07-14 16:15:52 +02:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
self.senders = filter_json.get("senders", None)
|
|
|
|
self.not_senders = filter_json.get("not_senders", [])
|
2016-07-14 16:15:52 +02:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
self.contains_url = filter_json.get("contains_url", None)
|
2016-07-14 16:15:52 +02:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
self.labels = filter_json.get("org.matrix.labels", None)
|
|
|
|
self.not_labels = filter_json.get("org.matrix.not_labels", [])
|
2019-10-30 16:56:33 +01:00
|
|
|
|
2022-10-17 17:32:11 +02:00
|
|
|
self.related_by_senders = filter_json.get("related_by_senders", None)
|
|
|
|
self.related_by_rel_types = filter_json.get("related_by_rel_types", None)
|
|
|
|
|
|
|
|
# For compatibility with _check_fields.
|
|
|
|
self.rel_types = None
|
|
|
|
self.not_rel_types = []
|
|
|
|
if hs.config.experimental.msc3874_enabled:
|
|
|
|
self.rel_types = filter_json.get("org.matrix.msc3874.rel_types", None)
|
|
|
|
self.not_rel_types = filter_json.get("org.matrix.msc3874.not_rel_types", [])
|
2022-03-10 16:36:13 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def filters_all_types(self) -> bool:
|
2023-01-09 14:43:50 +01:00
|
|
|
return self.types == [] or "*" in self.not_types
|
2016-11-22 17:38:35 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def filters_all_senders(self) -> bool:
|
2023-01-09 14:43:50 +01:00
|
|
|
return self.senders == [] or "*" in self.not_senders
|
2016-11-22 17:38:35 +01:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def filters_all_rooms(self) -> bool:
|
2023-01-09 14:43:50 +01:00
|
|
|
return self.rooms == [] or "*" in self.not_rooms
|
2016-11-22 17:38:35 +01:00
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
def _check(self, event: FilterEvent) -> bool:
|
2015-10-20 16:47:42 +02:00
|
|
|
"""Checks whether the filter matches the given event.
|
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
Args:
|
|
|
|
event: The event, account data, or presence to check against this
|
|
|
|
filter.
|
|
|
|
|
2015-10-20 16:47:42 +02:00
|
|
|
Returns:
|
2021-10-27 17:26:30 +02:00
|
|
|
True if the event matches the filter.
|
2015-10-20 16:47:42 +02:00
|
|
|
"""
|
2017-03-15 15:50:33 +01:00
|
|
|
# We usually get the full "events" as dictionaries coming through,
|
2021-12-30 19:47:12 +01:00
|
|
|
# except for presence which actually gets passed around as its own type.
|
2017-03-15 15:27:34 +01:00
|
|
|
if isinstance(event, UserPresenceState):
|
2021-10-27 17:26:30 +02:00
|
|
|
user_id = event.user_id
|
|
|
|
field_matchers = {
|
|
|
|
"senders": lambda v: user_id == v,
|
2022-05-27 13:14:36 +02:00
|
|
|
"types": lambda v: EduTypes.PRESENCE == v,
|
2021-10-27 17:26:30 +02:00
|
|
|
}
|
|
|
|
return self._check_fields(field_matchers)
|
2017-03-15 15:27:34 +01:00
|
|
|
else:
|
2021-10-27 17:26:30 +02:00
|
|
|
content = event.get("content")
|
2022-02-28 18:40:24 +01:00
|
|
|
# Content is assumed to be a mapping below, so ensure it is. This should
|
2021-10-27 17:26:30 +02:00
|
|
|
# always be true for events, but account_data has been allowed to
|
|
|
|
# have non-dict content.
|
2022-02-28 18:40:24 +01:00
|
|
|
if not isinstance(content, Mapping):
|
2021-10-27 17:26:30 +02:00
|
|
|
content = {}
|
|
|
|
|
2017-03-15 15:27:34 +01:00
|
|
|
sender = event.get("sender", None)
|
|
|
|
if not sender:
|
2017-03-15 17:01:01 +01:00
|
|
|
# Presence events had their 'sender' in content.user_id, but are
|
|
|
|
# now handled above. We don't know if anything else uses this
|
|
|
|
# form. TODO: Check this and probably remove it.
|
2021-10-27 17:26:30 +02:00
|
|
|
sender = content.get("user_id")
|
2017-03-15 15:27:34 +01:00
|
|
|
|
|
|
|
room_id = event.get("room_id", None)
|
|
|
|
ev_type = event.get("type", None)
|
2019-01-06 22:43:26 +01:00
|
|
|
|
|
|
|
# check if there is a string url field in the content for filtering purposes
|
2019-11-01 17:22:44 +01:00
|
|
|
labels = content.get(EventContentFields.LABELS, [])
|
2016-01-25 11:10:44 +01:00
|
|
|
|
2022-10-17 17:32:11 +02:00
|
|
|
# Check if the event has a relation.
|
|
|
|
rel_type = None
|
|
|
|
if isinstance(event, EventBase):
|
|
|
|
relation = relation_from_event(event)
|
|
|
|
if relation:
|
|
|
|
rel_type = relation.rel_type
|
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
field_matchers = {
|
|
|
|
"rooms": lambda v: room_id == v,
|
|
|
|
"senders": lambda v: sender == v,
|
|
|
|
"types": lambda v: _matches_wildcard(ev_type, v),
|
|
|
|
"labels": lambda v: v in labels,
|
2022-10-17 17:32:11 +02:00
|
|
|
"rel_types": lambda v: rel_type == v,
|
2021-10-27 17:26:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result = self._check_fields(field_matchers)
|
|
|
|
if not result:
|
|
|
|
return result
|
|
|
|
|
|
|
|
contains_url_filter = self.contains_url
|
|
|
|
if contains_url_filter is not None:
|
|
|
|
contains_url = isinstance(content.get("url"), str)
|
|
|
|
if contains_url_filter != contains_url:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2015-10-22 17:38:03 +02:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
def _check_fields(self, field_matchers: Dict[str, Callable[[str], bool]]) -> bool:
|
2015-10-22 17:38:03 +02:00
|
|
|
"""Checks whether the filter matches the given event fields.
|
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
Args:
|
|
|
|
field_matchers: A map of attribute name to callable to use for checking
|
|
|
|
particular fields.
|
|
|
|
|
|
|
|
The attribute name and an inverse (not_<attribute name>) must
|
|
|
|
exist on the Filter.
|
|
|
|
|
|
|
|
The callable should return true if the event's value matches the
|
|
|
|
filter's value.
|
|
|
|
|
2015-10-22 17:38:03 +02:00
|
|
|
Returns:
|
2021-10-01 13:02:32 +02:00
|
|
|
True if the event fields match
|
2015-10-22 17:38:03 +02:00
|
|
|
"""
|
2021-10-27 17:26:30 +02:00
|
|
|
|
|
|
|
for name, match_func in field_matchers.items():
|
|
|
|
# If the event matches one of the disallowed values, reject it.
|
2015-10-20 16:33:25 +02:00
|
|
|
not_name = "not_%s" % (name,)
|
2016-07-14 16:15:52 +02:00
|
|
|
disallowed_values = getattr(self, not_name)
|
2015-10-20 16:33:25 +02:00
|
|
|
if any(map(match_func, disallowed_values)):
|
2015-01-29 18:41:48 +01:00
|
|
|
return False
|
|
|
|
|
2023-01-09 14:43:50 +01:00
|
|
|
# Otherwise if the event does not match at least one of the allowed
|
|
|
|
# values, reject it.
|
2016-07-14 16:15:52 +02:00
|
|
|
allowed_values = getattr(self, name)
|
2015-10-20 16:33:25 +02:00
|
|
|
if allowed_values is not None:
|
|
|
|
if not any(map(match_func, allowed_values)):
|
2015-01-29 18:41:48 +01:00
|
|
|
return False
|
2016-07-14 16:15:52 +02:00
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
# Otherwise, accept it.
|
2015-01-29 18:41:48 +01:00
|
|
|
return True
|
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def filter_rooms(self, room_ids: Iterable[str]) -> Set[str]:
|
2015-10-21 11:09:26 +02:00
|
|
|
"""Apply the 'rooms' filter to a given list of rooms.
|
|
|
|
|
|
|
|
Args:
|
2021-10-01 13:02:32 +02:00
|
|
|
room_ids: A list of room_ids.
|
2015-10-21 11:09:26 +02:00
|
|
|
|
|
|
|
Returns:
|
2021-10-01 13:02:32 +02:00
|
|
|
A list of room_ids that match the filter
|
2015-10-21 11:09:26 +02:00
|
|
|
"""
|
|
|
|
room_ids = set(room_ids)
|
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
disallowed_rooms = set(self.not_rooms)
|
2015-10-21 11:09:26 +02:00
|
|
|
room_ids -= disallowed_rooms
|
|
|
|
|
2021-10-27 17:26:30 +02:00
|
|
|
allowed_rooms = self.rooms
|
2015-10-21 11:09:26 +02:00
|
|
|
if allowed_rooms is not None:
|
|
|
|
room_ids &= set(allowed_rooms)
|
|
|
|
|
|
|
|
return room_ids
|
|
|
|
|
2021-11-09 14:10:58 +01:00
|
|
|
async def _check_event_relations(
|
2022-05-10 20:10:22 +02:00
|
|
|
self, events: Collection[FilterEvent]
|
2021-11-09 14:10:58 +01:00
|
|
|
) -> List[FilterEvent]:
|
2022-05-10 20:10:22 +02:00
|
|
|
# The event IDs to check, mypy doesn't understand the isinstance check.
|
2021-11-09 14:10:58 +01:00
|
|
|
event_ids = [event.event_id for event in events if isinstance(event, EventBase)] # type: ignore[attr-defined]
|
|
|
|
event_ids_to_keep = set(
|
|
|
|
await self._store.events_have_relations(
|
2022-03-10 16:36:13 +01:00
|
|
|
event_ids, self.related_by_senders, self.related_by_rel_types
|
2021-11-09 14:10:58 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return [
|
|
|
|
event
|
|
|
|
for event in events
|
|
|
|
if not isinstance(event, EventBase) or event.event_id in event_ids_to_keep
|
|
|
|
]
|
|
|
|
|
|
|
|
async def filter(self, events: Iterable[FilterEvent]) -> List[FilterEvent]:
|
|
|
|
result = [event for event in events if self._check(event)]
|
|
|
|
|
2022-03-10 16:36:13 +01:00
|
|
|
if self.related_by_senders or self.related_by_rel_types:
|
2021-11-09 14:10:58 +01:00
|
|
|
return await self._check_event_relations(result)
|
|
|
|
|
|
|
|
return result
|
2015-10-20 16:33:25 +02:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def with_room_ids(self, room_ids: Iterable[str]) -> "Filter":
|
2019-01-22 12:12:48 +01:00
|
|
|
"""Returns a new filter with the given room IDs appended.
|
|
|
|
|
|
|
|
Args:
|
2021-10-01 13:02:32 +02:00
|
|
|
room_ids: The room_ids to add
|
2019-01-22 12:12:48 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
filter: A new filter including the given rooms and the old
|
|
|
|
filter's rooms.
|
|
|
|
"""
|
2021-11-09 14:10:58 +01:00
|
|
|
newFilter = Filter(self._hs, self.filter_json)
|
2019-01-22 12:12:48 +01:00
|
|
|
newFilter.rooms += room_ids
|
|
|
|
return newFilter
|
2019-01-18 11:04:47 +01:00
|
|
|
|
2015-10-20 16:33:25 +02:00
|
|
|
|
2021-10-01 13:02:32 +02:00
|
|
|
def _matches_wildcard(actual_value: Optional[str], filter_value: str) -> bool:
|
|
|
|
if filter_value.endswith("*") and isinstance(actual_value, str):
|
2015-10-20 16:33:25 +02:00
|
|
|
type_prefix = filter_value[:-1]
|
|
|
|
return actual_value.startswith(type_prefix)
|
|
|
|
else:
|
|
|
|
return actual_value == filter_value
|