This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Support filtering by relations per MSC3440 #11236
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c85c5ab
Failing test-case.
clokep e090779
Convert filter methods to async.
clokep 22379b2
Remove unnecessary assert.
clokep 2feea47
Give Filter access to the HomeServer.
clokep 4091102
Filter events by relation.
clokep 41ad65c
Filter in the database.
clokep cd292a8
Newsfragment
clokep de35d4d
Make check internal.
clokep 9b36909
Check all events for relations at once.
clokep a1579f1
Handle an event having multiple relations.
clokep cd38c05
Use the experimental configuration flag.
clokep 59346cc
Fix typo.
clokep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support filtering by relation senders & types per [MSC3440](https://github.com/matrix-org/matrix-doc/pull/3440). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Copyright 2015, 2016 OpenMarket Ltd | ||
# Copyright 2017 Vector Creations Ltd | ||
# Copyright 2018-2019 New Vector Ltd | ||
# Copyright 2019 The Matrix.org Foundation C.I.C. | ||
# Copyright 2019-2021 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
|
@@ -86,6 +86,9 @@ | |
# cf https://github.com/matrix-org/matrix-doc/pull/2326 | ||
"org.matrix.labels": {"type": "array", "items": {"type": "string"}}, | ||
"org.matrix.not_labels": {"type": "array", "items": {"type": "string"}}, | ||
# MSC3440, filtering by event relations. | ||
"io.element.relation_senders": {"type": "array", "items": {"type": "string"}}, | ||
"io.element.relation_types": {"type": "array", "items": {"type": "string"}}, | ||
}, | ||
} | ||
|
||
|
@@ -146,14 +149,16 @@ def matrix_user_id_validator(user_id_str: str) -> UserID: | |
|
||
class Filtering: | ||
def __init__(self, hs: "HomeServer"): | ||
super().__init__() | ||
self._hs = hs | ||
self.store = hs.get_datastore() | ||
|
||
self.DEFAULT_FILTER_COLLECTION = FilterCollection(hs, {}) | ||
|
||
async def get_user_filter( | ||
self, user_localpart: str, filter_id: Union[int, str] | ||
) -> "FilterCollection": | ||
result = await self.store.get_user_filter(user_localpart, filter_id) | ||
return FilterCollection(result) | ||
return FilterCollection(self._hs, result) | ||
|
||
def add_user_filter( | ||
self, user_localpart: str, user_filter: JsonDict | ||
|
@@ -191,21 +196,22 @@ def check_valid_filter(self, user_filter_json: JsonDict) -> None: | |
|
||
|
||
class FilterCollection: | ||
def __init__(self, filter_json: JsonDict): | ||
def __init__(self, hs: "HomeServer", filter_json: JsonDict): | ||
self._filter_json = filter_json | ||
|
||
room_filter_json = self._filter_json.get("room", {}) | ||
|
||
self._room_filter = Filter( | ||
{k: v for k, v in room_filter_json.items() if k in ("rooms", "not_rooms")} | ||
hs, | ||
{k: v for k, v in room_filter_json.items() if k in ("rooms", "not_rooms")}, | ||
) | ||
|
||
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", {})) | ||
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", {})) | ||
self._room_account_data = Filter(hs, room_filter_json.get("account_data", {})) | ||
self._presence_filter = Filter(hs, filter_json.get("presence", {})) | ||
self._account_data = Filter(hs, filter_json.get("account_data", {})) | ||
|
||
self.include_leave = filter_json.get("room", {}).get("include_leave", False) | ||
self.event_fields = filter_json.get("event_fields", []) | ||
|
@@ -232,25 +238,37 @@ def lazy_load_members(self) -> bool: | |
def include_redundant_members(self) -> bool: | ||
return self._room_state_filter.include_redundant_members | ||
|
||
def filter_presence( | ||
async def filter_presence( | ||
self, events: Iterable[UserPresenceState] | ||
) -> List[UserPresenceState]: | ||
return self._presence_filter.filter(events) | ||
return await self._presence_filter.filter(events) | ||
|
||
def filter_account_data(self, events: Iterable[JsonDict]) -> List[JsonDict]: | ||
return self._account_data.filter(events) | ||
async def filter_account_data(self, events: Iterable[JsonDict]) -> List[JsonDict]: | ||
return await self._account_data.filter(events) | ||
|
||
def filter_room_state(self, events: Iterable[EventBase]) -> List[EventBase]: | ||
return self._room_state_filter.filter(self._room_filter.filter(events)) | ||
async def filter_room_state(self, events: Iterable[EventBase]) -> List[EventBase]: | ||
return await self._room_state_filter.filter( | ||
await self._room_filter.filter(events) | ||
) | ||
|
||
def filter_room_timeline(self, events: Iterable[EventBase]) -> List[EventBase]: | ||
return self._room_timeline_filter.filter(self._room_filter.filter(events)) | ||
async def filter_room_timeline( | ||
self, events: Iterable[EventBase] | ||
) -> List[EventBase]: | ||
return await self._room_timeline_filter.filter( | ||
await self._room_filter.filter(events) | ||
) | ||
|
||
def filter_room_ephemeral(self, events: Iterable[JsonDict]) -> List[JsonDict]: | ||
return self._room_ephemeral_filter.filter(self._room_filter.filter(events)) | ||
async def filter_room_ephemeral(self, events: Iterable[JsonDict]) -> List[JsonDict]: | ||
return await self._room_ephemeral_filter.filter( | ||
await self._room_filter.filter(events) | ||
) | ||
|
||
def filter_room_account_data(self, events: Iterable[JsonDict]) -> List[JsonDict]: | ||
return self._room_account_data.filter(self._room_filter.filter(events)) | ||
async def filter_room_account_data( | ||
self, events: Iterable[JsonDict] | ||
) -> List[JsonDict]: | ||
return await self._room_account_data.filter( | ||
await self._room_filter.filter(events) | ||
) | ||
|
||
def blocks_all_presence(self) -> bool: | ||
return ( | ||
|
@@ -274,7 +292,9 @@ def blocks_all_room_timeline(self) -> bool: | |
|
||
|
||
class Filter: | ||
def __init__(self, filter_json: JsonDict): | ||
def __init__(self, hs: "HomeServer", filter_json: JsonDict): | ||
self._hs = hs | ||
self._store = hs.get_datastore() | ||
self.filter_json = filter_json | ||
|
||
self.limit = filter_json.get("limit", 10) | ||
|
@@ -297,6 +317,20 @@ def __init__(self, filter_json: JsonDict): | |
self.labels = filter_json.get("org.matrix.labels", None) | ||
self.not_labels = filter_json.get("org.matrix.not_labels", []) | ||
|
||
# Ideally these would be rejected at the endpoint if they were provided | ||
# and not supported, but that would involve modifying the JSON schema | ||
# based on the homeserver configuration. | ||
if hs.config.experimental.msc3440_enabled: | ||
self.relation_senders = self.filter_json.get( | ||
"io.element.relation_senders", None | ||
) | ||
self.relation_types = self.filter_json.get( | ||
"io.element.relation_types", None | ||
) | ||
else: | ||
self.relation_senders = None | ||
self.relation_types = None | ||
|
||
def filters_all_types(self) -> bool: | ||
return "*" in self.not_types | ||
|
||
|
@@ -306,7 +340,7 @@ def filters_all_senders(self) -> bool: | |
def filters_all_rooms(self) -> bool: | ||
return "*" in self.not_rooms | ||
|
||
def check(self, event: FilterEvent) -> bool: | ||
def _check(self, event: FilterEvent) -> bool: | ||
"""Checks whether the filter matches the given event. | ||
|
||
Args: | ||
|
@@ -420,8 +454,30 @@ def filter_rooms(self, room_ids: Iterable[str]) -> Set[str]: | |
|
||
return room_ids | ||
|
||
def filter(self, events: Iterable[FilterEvent]) -> List[FilterEvent]: | ||
return list(filter(self.check, events)) | ||
async def _check_event_relations( | ||
self, events: Iterable[FilterEvent] | ||
) -> List[FilterEvent]: | ||
# The event IDs to check, mypy doesn't understand the ifinstance check. | ||
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( | ||
event_ids, self.relation_senders, self.relation_types | ||
) | ||
) | ||
|
||
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)] | ||
|
||
if self.relation_senders or self.relation_types: | ||
return await self._check_event_relations(result) | ||
|
||
return result | ||
|
||
def with_room_ids(self, room_ids: Iterable[str]) -> "Filter": | ||
"""Returns a new filter with the given room IDs appended. | ||
|
@@ -433,7 +489,7 @@ def with_room_ids(self, room_ids: Iterable[str]) -> "Filter": | |
filter: A new filter including the given rooms and the old | ||
filter's rooms. | ||
""" | ||
newFilter = Filter(self.filter_json) | ||
newFilter = Filter(self._hs, self.filter_json) | ||
newFilter.rooms += room_ids | ||
return newFilter | ||
|
||
|
@@ -444,6 +500,3 @@ def _matches_wildcard(actual_value: Optional[str], filter_value: str) -> bool: | |
return actual_value.startswith(type_prefix) | ||
else: | ||
return actual_value == filter_value | ||
|
||
|
||
DEFAULT_FILTER_COLLECTION = FilterCollection({}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needed to be moved somewhere that it can have access to a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separating this out to do a single query for all events might be a bit premature optimization, but doing a separate query per event seemed quite expensive.