Compare commits

...

137 Commits

Author SHA1 Message Date
Eric Eastwood
90315b0eb5 Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-filtering
Conflicts:
	synapse/handlers/sliding_sync.py
	tests/handlers/test_sliding_sync.py
	tests/rest/client/test_sync.py
2024-06-06 15:08:30 -05:00
Eric Eastwood
b457c0b2e2 Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittlemods/msc3575-sliding-sync-filtering 2024-06-06 14:40:59 -05:00
Eric Eastwood
c89f012d8b Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-06-06 12:10:21 -05:00
Eric Eastwood
6f10b9722d Simplify boolean logic and avoid set construction
See https://github.com/element-hq/synapse/pull/17187#discussion_r1629853770
2024-06-06 12:03:17 -05:00
Eric Eastwood
0153a6e535 Add test for from_token after to_token 2024-06-06 10:17:02 -05:00
Eric Eastwood
0f6646dbfd Add test for no from_token 2024-06-06 09:46:50 -05:00
Eric Eastwood
1158cc6d2d Add note 2024-06-05 15:42:07 -05:00
Eric Eastwood
5078d36bd3 Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittlemods/msc3575-sliding-sync-filtering
Conflicts:
	tests/handlers/test_sliding_sync.py
2024-06-05 14:44:30 -05:00
Eric Eastwood
c7d1fc33a1 Fix separator label 2024-06-05 14:20:02 -05:00
Eric Eastwood
703cdc9c3b Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-06-05 11:55:14 -05:00
Eric Eastwood
567830769b Add validation for membership 2024-06-05 11:42:34 -05:00
Eric Eastwood
278ba63953 No need to check from/to token relationship
See https://github.com/element-hq/synapse/pull/17187#discussion_r1627771338

`get_membership_changes_for_user(from_key=xxx, to_key=xxx)` will handle
getting out what we need and filter the results based on the tokens
(even in cases where the from_key is ahead of the to_key).
2024-06-05 11:22:51 -05:00
Eric Eastwood
6a6cdc61f3 Use Set because Tuple doesn't allow - operations 2024-06-05 11:03:06 -05:00
Eric Eastwood
1fc1b58ba2 Remove assert since we no longer need that information
See https://github.com/element-hq/synapse/pull/17187#discussion_r1627794551
2024-06-05 10:57:53 -05:00
Eric Eastwood
7bbe2ed8d8 More clear way to express what membership we want to display
See https://github.com/element-hq/synapse/pull/17187#discussion_r1627754354
2024-06-05 10:56:48 -05:00
Eric Eastwood
2af467d4a8 Remove extra for-loop
Co-authored-by: Erik Johnston <erikj@element.io>
2024-06-05 10:52:22 -05:00
Eric Eastwood
2864837b65 Allow new get_sync_room_ids_for_user implementation to work with multiple event persisters
Before, the problem scenario would get caught in one of the assertions because
we expect the to_token <= membership_snapshot_token or vice-versa but it's
possible the tokens are intertwined and neither is ahead of each other.
Especially since the `instance_map` in `membership_snapshot_token` is made up
from the `stream_ordering` of membership events at various stream positions
and processed on different instances (not current stream positions).

We get into trouble when stream positions are lagging between workers and our
now/`to_token` doesn't cleanly compare to `membership_snapshot_token`.

What we really want to assert is that the `to_token` <= the stream positions
at the time we asked for the room membership snapshot. Since
`get_rooms_for_local_user_where_membership_is()` doesn't return that
information, the closest we can get is to get the stream positions before we
ask for the room membership snapshot and consider that good enough to compare
against.
2024-06-04 19:50:12 -05:00
Eric Eastwood
3ce08925e3 Add test with multiple event persisters that fails the previous get_sync_room_ids_for_user implementation
The new implementation catches the problem with an assert
but I think it's possible to make it work as well.

```
SYNAPSE_POSTGRES=1 SYNAPSE_POSTGRES_USER=postgres SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.handlers.test_sliding_sync.GetSyncRoomIdsForUserEventShardTestCase
```
2024-06-04 19:27:28 -05:00
Eric Eastwood
dfee21a1f4 Switch fixup order to fix edge case with newly_left rooms
Previously, we added back newly_left rooms and our second
fixup was removing them. We can just switch the order
of the fixups to solve this.
2024-06-04 18:29:14 -05:00
Eric Eastwood
d3ce27b957 Balance parenthesis 2024-06-04 13:28:23 -05:00
Eric Eastwood
07f57a4066 Give a summary of what rooms we're looking for 2024-06-04 13:27:35 -05:00
Eric Eastwood
555ba4b891 Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittlemods/msc3575-sliding-sync-filtering
Conflicts:
	tests/handlers/test_sliding_sync.py
	tests/rest/client/test_sync.py
2024-06-04 13:04:55 -05:00
Eric Eastwood
f6a5905b6c Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
Conflicts:
	synapse/storage/databases/main/roommember.py
2024-06-04 12:59:33 -05:00
Eric Eastwood
54dbc278d2 Add None defaults 2024-06-04 12:40:39 -05:00
Eric Eastwood
9e46b2a53b Fix typo 2024-06-04 00:52:40 -05:00
Eric Eastwood
03dd87ab3c Add test for notifier.wait_for_stream_token(from_token) 2024-06-03 23:46:47 -05:00
Eric Eastwood
8bb357a35e Note the extras 2024-06-03 23:24:23 -05:00
Eric Eastwood
64df6fbefa Revert change that should be separated and is failing 2024-06-03 23:17:16 -05:00
Eric Eastwood
970a0c623a Adjust wording 2024-06-03 23:08:51 -05:00
Eric Eastwood
3514aa0ff2 Add licensing headers 2024-06-03 22:49:27 -05:00
Eric Eastwood
35db057982 Add support for kicks 2024-06-03 22:47:01 -05:00
Eric Eastwood
e4c66b8ac9 Avoid serializing response that will never be heard 2024-06-03 20:13:00 -05:00
Eric Eastwood
1268a5413f Properly compare tokens and event positions
Avoid flawed raw stream comparison.
2024-06-03 20:09:35 -05:00
Eric Eastwood
9c6ec25aca Create membership_snapshot_token with instance_map 2024-06-03 19:23:31 -05:00
Eric Eastwood
8dca8f548c Merge branch 'madlittlemods/rooms-for-user-event-pos' into madlittlemods/msc3575-sliding-sync-0.0.1
Conflicts:
	synapse/handlers/initial_sync.py
	tests/replication/storage/test_events.py
2024-06-03 18:08:56 -05:00
Eric Eastwood
09638ac31d Add changelog 2024-06-03 17:51:03 -05:00
Eric Eastwood
7b41f412c6 Fix random lints 2024-06-03 16:46:34 -05:00
Eric Eastwood
73c20d961f Use method to get instance name in tests 2024-06-03 16:45:08 -05:00
Eric Eastwood
939695dbb5 Update usage 2024-06-03 16:41:19 -05:00
Eric Eastwood
4155e18d76 Fix circular imports when running specific tests
Before:
```
$ SYNAPSE_POSTGRES=1 SYNAPSE_POSTGRES_USER=postgres SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.replication.storage.test_events
[...]
Traceback (most recent call last):
  File "pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.12/lib/python3.12/site-packages/twisted/trial/runner.py", line 711, in loadByName
    return self.suiteFactory([self.findByName(name, recurse=recurse)])
  File "pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.12/lib/python3.12/site-packages/twisted/trial/runner.py", line 474, in findByName
    obj = reflect.namedModule(searchName)
  File "pypoetry/virtualenvs/matrix-synapse-xCtC9ulO-py3.12/lib/python3.12/site-packages/twisted/python/reflect.py", line 156, in namedModule
    topLevel = __import__(name)
  File "synapse/tests/replication/storage/test_events.py", line 33, in <module>
    from synapse.handlers.room import RoomEventSource
  File "synapse/synapse/handlers/room.py", line 74, in <module>
    from synapse.rest.admin._base import assert_user_is_admin
  File "synapse/synapse/rest/__init__.py", line 24, in <module>
    from synapse.rest import admin
  File "synapse/synapse/rest/admin/__init__.py", line 41, in <module>
    from synapse.handlers.pagination import PURGE_HISTORY_ACTION_NAME
  File "synapse/synapse/handlers/pagination.py", line 30, in <module>
    from synapse.handlers.room import ShutdownRoomParams, ShutdownRoomResponse
builtins.ImportError: cannot import name 'ShutdownRoomParams' from partially initialized module 'synapse.handlers.room' (most likely due to a circular import) (synapse/synapse/handlers/room.py)
```
2024-06-03 16:20:16 -05:00
Eric Eastwood
271a196121 Use fully-qualified PersistedEventPosition when returning membership for user
Spawning from https://github.com/element-hq/synapse/pull/17187
2024-06-03 16:07:56 -05:00
Eric Eastwood
a0c042ef12 Re-arrange how this list will be returned 2024-06-03 16:05:18 -05:00
Eric Eastwood
803fbbe95e Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-06-03 16:02:06 -05:00
Eric Eastwood
8f09313d7d Add instance name alongside stream_ordering (RoomsForUser.event_pos) 2024-06-03 16:01:11 -05:00
Eric Eastwood
09609cb0db WIP: TODO comments after pairing with Erik 2024-06-03 14:28:45 -05:00
Eric Eastwood
6b1eba4fee Add rest test to make sure filters apply 2024-05-30 13:25:07 -05:00
Eric Eastwood
92ea28674f Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittlemods/msc3575-sliding-sync-filtering 2024-05-30 09:22:21 -05:00
Eric Eastwood
49998e053e Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-05-30 09:21:58 -05:00
Eric Eastwood
f74cc3f166 Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittlemods/msc3575-sliding-sync-filtering 2024-05-29 22:48:14 -05:00
Eric Eastwood
34d67fdcd1 Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-05-29 17:37:31 -05:00
Eric Eastwood
8bf5a623d7 Add rest test 2024-05-29 17:05:53 -05:00
Eric Eastwood
770992c791 Fix typo, reword 2024-05-29 16:48:05 -05:00
Eric Eastwood
7804febb63 Add changelog 2024-05-29 16:42:03 -05:00
Eric Eastwood
27465fcfa6 Prefer not None 2024-05-29 16:40:57 -05:00
Eric Eastwood
3b670046a9 Rename _create_dm_room 2024-05-29 16:37:21 -05:00
Eric Eastwood
1dd04d2896 Add tests for DM filter 2024-05-29 16:35:27 -05:00
Eric Eastwood
d65c694a71 Filter DM from account data 2024-05-29 15:32:06 -05:00
Eric Eastwood
950fd70948 Tweak comments 2024-05-28 15:51:05 -05:00
Eric Eastwood
a28569f79d Add understanding of this skip 2024-05-28 15:41:32 -05:00
Eric Eastwood
abf139a3b7 Fill out docstring todo 2024-05-28 15:41:11 -05:00
Eric Eastwood
b632cbb46a Add better comments 2024-05-28 15:05:35 -05:00
Eric Eastwood
44e9a92f01 Fill in rest docstring 2024-05-28 14:29:41 -05:00
Eric Eastwood
b12fee5f21 Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-05-28 09:51:04 -05:00
Eric Eastwood
adc0e2f5e8 Fix unserialize type 2024-05-23 17:24:19 -05:00
Eric Eastwood
65d9b7968d Fix lints 2024-05-23 17:22:27 -05:00
Eric Eastwood
b5b3e77e7e Fix Pydantic conint/constr usage with mypy
See https://github.com/pydantic/pydantic/issues/156#issuecomment-1130883884
2024-05-23 16:56:48 -05:00
Eric Eastwood
d1bd02d91d Add TODO to handle partial stated rooms 2024-05-23 16:16:26 -05:00
Eric Eastwood
f9fa683750 Fix another leaking loop variable
See https://github.com/element-hq/synapse/pull/17187#discussion_r1612211662
2024-05-23 15:18:53 -05:00
Eric Eastwood
a822a05bec Revert as TODO says 2024-05-23 14:25:17 -05:00
Eric Eastwood
37af87a563 Add test to make sure we don't confuse multiple rooms 2024-05-23 14:23:49 -05:00
Eric Eastwood
8c3de846d4 Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
Conflicts:
	synapse/rest/client/sync.py
2024-05-23 12:07:51 -05:00
Eric Eastwood
04eeee648e Merge branch 'madlittlemods/msc3575-sliding-sync-e2ee' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-05-23 12:06:56 -05:00
Eric Eastwood
a482545119 Fix test after removing type ignore 2024-05-23 09:46:39 -05:00
Eric Eastwood
ab0b844ce1 Add actual typing for params (not just docstrings)
See https://github.com/element-hq/synapse/pull/17167#discussion_r1611301044
2024-05-23 09:31:02 -05:00
Eric Eastwood
6606ac1d07 Add docstring for parametized attributes
See https://github.com/element-hq/synapse/pull/17167#discussion_r1611301044
2024-05-23 09:23:02 -05:00
Eric Eastwood
4c7d7e6365 Encode response 2024-05-22 17:08:34 -05:00
Eric Eastwood
c7f7ae4ec0 Start assembling lists 2024-05-22 16:28:44 -05:00
Eric Eastwood
13d61469b5 Fill out sliding window response types 2024-05-22 15:54:15 -05:00
Eric Eastwood
a7c64761e6 Use client_patterns() 2024-05-22 15:37:34 -05:00
Eric Eastwood
c7b8743454 Add changelog 2024-05-22 15:13:23 -05:00
Eric Eastwood
89db5663a1 Merge branch 'madlittlemods/msc3575-sliding-sync-e2ee' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-05-22 15:07:36 -05:00
Eric Eastwood
d4b41aaf43 Fix lints 2024-05-22 15:01:06 -05:00
Eric Eastwood
3da6bc1902 Use @parameterized_class
As suggested in https://github.com/element-hq/synapse/pull/17167#discussion_r1610255726
2024-05-22 14:50:35 -05:00
Eric Eastwood
06ac1da6ec Restore copyright header
See https://github.com/element-hq/synapse/pull/17167#discussion_r1609876335
2024-05-22 14:08:42 -05:00
Eric Eastwood
97497955ea Update filter to be more precise and avoid more work
- Added `room.account_data` and `room.presence` to avoid extra work in `_generate_sync_entry_for_rooms()`
 - Added a comment to the top-level `account_data` and `presence` filters that `(This is just here for good measure)`

See https://github.com/element-hq/synapse/pull/17167#discussion_r1610517164
2024-05-22 14:07:35 -05:00
Eric Eastwood
514aba5810 Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee
Conflicts:
	synapse/handlers/sync.py
2024-05-22 13:23:47 -05:00
Eric Eastwood
c82a084006 Update comments and test docstrings 2024-05-22 13:18:13 -05:00
Eric Eastwood
1b3a5bf006 Fix referencing variable from other lexical scope 2024-05-22 11:43:04 -05:00
Eric Eastwood
343de8f874 Remove debug logs 2024-05-22 11:22:06 -05:00
Eric Eastwood
17783c36d0 Log why no unsigned 2024-05-22 11:19:59 -05:00
Eric Eastwood
dd9356a211 Using unsigned prev_content 2024-05-21 17:57:14 -05:00
Eric Eastwood
fd355f6b62 WIP 2024-05-21 17:37:46 -05:00
Eric Eastwood
fe48188f7d Handle more edge cases 2024-05-21 15:05:19 -05:00
Eric Eastwood
c826550524 Add some tests 2024-05-21 14:24:03 -05:00
Eric Eastwood
717b160400 Adjust wording, add todo 2024-05-21 10:26:42 -05:00
Eric Eastwood
c2221bbcc3 Lint 2024-05-21 10:20:58 -05:00
Eric Eastwood
2f112e73df Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee 2024-05-21 10:08:23 -05:00
Eric Eastwood
f6122ff0a2 Use client_patterns() for endpoint URL
See https://github.com/element-hq/synapse/pull/17167#discussion_r1608170900
2024-05-21 09:54:31 -05:00
Eric Eastwood
9ffafe781d Try to think about this logic 2024-05-21 09:52:55 -05:00
Eric Eastwood
6dadfe9628 Try handle no from_token or to_token already newer 2024-05-20 18:44:38 -05:00
Eric Eastwood
07d84ab66c Start of gathering room list to display in sync 2024-05-20 18:18:11 -05:00
Eric Eastwood
bfa8c63e57 Merge branch 'madlittlemods/msc3575-sliding-sync-e2ee' into madlittlemods/msc3575-sliding-sync-0.0.1 2024-05-20 14:32:24 -05:00
Eric Eastwood
02cecfa626 Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee 2024-05-20 13:02:49 -05:00
Eric Eastwood
5f194f9b3d Exclude application services
See https://github.com/element-hq/synapse/pull/17167#discussion_r1595924522
2024-05-20 11:58:22 -05:00
Eric Eastwood
3539abe0aa Membership in timeline for better derived info 2024-05-20 11:46:25 -05:00
Eric Eastwood
3092ab5047 Calculate room derived membership info for device_lists
See https://github.com/element-hq/synapse/pull/17167#discussion_r1595923800
2024-05-20 11:32:56 -05:00
Eric Eastwood
4ad7a8b755 No need to change this formatting from develop 2024-05-16 17:24:28 -05:00
Eric Eastwood
35ca937608 Format docstring 2024-05-16 17:20:19 -05:00
Eric Eastwood
821a1b3acc Add missing field to docstring 2024-05-16 17:18:18 -05:00
Eric Eastwood
b23abca9e7 Fix test inheritance
See https://github.com/element-hq/synapse/pull/17167#discussion_r1594517041
2024-05-16 17:04:26 -05:00
Eric Eastwood
7331401e89 Lint 2024-05-16 13:36:34 -05:00
Eric Eastwood
9bdfa16b3e Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee
Conflicts:
	synapse/handlers/sync.py
	tests/handlers/test_sync.py
2024-05-16 13:10:58 -05:00
Eric Eastwood
f3db068c28 Copy body to config 2024-05-15 17:00:00 -05:00
Eric Eastwood
ee6baba7b6 Iterating 2024-05-15 14:53:53 -05:00
Eric Eastwood
c8256b6cbc Start to map out response 2024-05-15 11:18:47 -05:00
Eric Eastwood
2dd0cde7c7 Fill out more options 2024-05-15 09:50:36 -05:00
Eric Eastwood
2863fbadcc More optional 2024-05-15 09:35:28 -05:00
Eric Eastwood
aee594adf8 Can't use StringConstraints 2024-05-14 09:42:56 -05:00
Eric Eastwood
654e8f69ee Add Pydantic model for the Sliding Sync API 2024-05-13 15:38:46 -05:00
Eric Eastwood
f9c9d44360 Add stub Sliding Sync endpoint 2024-05-13 10:12:25 -05:00
Eric Eastwood
b9e5379836 Describe test 2024-05-09 15:15:40 -05:00
Eric Eastwood
6b7cfd7037 Add tests for device_unused_fallback_key_types in /sync 2024-05-09 15:11:27 -05:00
Eric Eastwood
f09835556e Add device_one_time_keys_count tests 2024-05-09 14:41:04 -05:00
Eric Eastwood
adb7e20ddd Consolidate device_lists /sync tests 2024-05-09 13:55:36 -05:00
Eric Eastwood
0892283f44 Add comments docs 2024-05-09 13:39:16 -05:00
Eric Eastwood
8871dac779 Share tests using inheritance
See https://github.com/element-hq/synapse/pull/17167#discussion_r1594517041
-> https://stackoverflow.com/questions/28333478/python-unittest-testcase-with-inheritance
2024-05-09 10:25:35 -05:00
Eric Eastwood
6bf48968eb Try calculate more 2024-05-08 23:50:31 -05:00
Eric Eastwood
10ffae6c50 Shared logic for get_sync_result_builder() 2024-05-08 15:14:40 -05:00
Eric Eastwood
c60a4f84ac Add changelog 2024-05-08 13:59:50 -05:00
Eric Eastwood
b8b70ba1ba Fix lint 2024-05-08 12:44:55 -05:00
Eric Eastwood
06d12e50a2 Ugly overloads 2024-05-08 12:43:57 -05:00
Eric Eastwood
371ec57555 Fix wait_for_sync_for_user in tests 2024-05-08 12:19:56 -05:00
Eric Eastwood
d4ff933748 Prefer Sync v2 vs Sliding Sync distinction
We were using the enum just to distinguish /sync v2
vs Sliding Sync /sync/e2ee so we should just make an
enum for that instead of trying to glom onto the
existing `sync_type` (overloading it).
2024-05-08 11:53:59 -05:00
Eric Eastwood
69f91436cf Comment on tests 2024-05-08 10:54:54 -05:00
Eric Eastwood
5e925f621c Share tests with test_sendtodevice 2024-05-08 10:48:59 -05:00
Eric Eastwood
1e05a05f03 Add Sliding Sync /sync/e2ee endpoint for To-Device messages
Based on:

 - MSC3575: Sliding Sync (aka Sync v3): https://github.com/matrix-org/matrix-spec-proposals/pull/3575
 - MSC3885: Sliding Sync Extension: To-Device messages: https://github.com/matrix-org/matrix-spec-proposals/pull/3885
 - MSC3884: Sliding Sync Extension: E2EE: https://github.com/matrix-org/matrix-spec-proposals/pull/3884
2024-05-07 18:16:35 -05:00
Eric Eastwood
f9e6e53130 Configurable /sync/e2ee endpoint 2024-05-06 17:11:32 -05:00
4 changed files with 363 additions and 6 deletions

View File

@@ -0,0 +1 @@
Add `is_dm` filtering to experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint.

View File

@@ -31,7 +31,7 @@ if TYPE_CHECKING or HAS_PYDANTIC_V2:
else:
from pydantic import Extra
from synapse.api.constants import Membership
from synapse.api.constants import AccountDataTypes, Membership
from synapse.events import EventBase
from synapse.rest.client.models import SlidingSyncBody
from synapse.types import JsonMapping, Requester, RoomStreamToken, StreamToken, UserID
@@ -332,11 +332,15 @@ class SlidingSyncHandler:
lists: Dict[str, SlidingSyncResult.SlidingWindowList] = {}
if sync_config.lists:
for list_key, list_config in sync_config.lists.items():
# TODO: Apply filters
#
# TODO: Exclude partially stated rooms unless the `required_state` has
# `["m.room.member", "$LAZY"]`
# Apply filters
filtered_room_ids = room_id_set
if list_config.filters is not None:
# TODO: To be absolutely correct, this could also take into account
# from/to tokens but some of the streams don't support looking back
# in time (like global account_data).
filtered_room_ids = await self.filter_rooms(
sync_config.user, room_id_set, list_config.filters
)
# TODO: Apply sorts
sorted_room_ids = sorted(filtered_room_ids)
@@ -608,3 +612,74 @@ class SlidingSyncHandler:
sync_room_id_set.add(room_id)
return sync_room_id_set
async def filter_rooms(
self,
user: UserID,
room_id_set: AbstractSet[str],
filters: SlidingSyncConfig.SlidingSyncList.Filters,
) -> AbstractSet[str]:
"""
Filter rooms based on the sync request.
"""
user_id = user.to_string()
# TODO: Apply filters
#
# TODO: Exclude partially stated rooms unless the `required_state` has
# `["m.room.member", "$LAZY"]`
filtered_room_id_set = set(room_id_set)
# Filter for Direct-Message (DM) rooms
if filters.is_dm is not None:
# We're using global account data (`m.direct`) instead of checking for
# `is_direct` on membership events because that property only appears for
# the invitee membership event (doesn't show up for the inviter). Account
# data is set by the client so it needs to be scrutinized.
dm_map = await self.store.get_global_account_data_by_type_for_user(
user_id, AccountDataTypes.DIRECT
)
logger.warn("dm_map: %s", dm_map)
# Flatten out the map
dm_room_id_set = set()
if dm_map:
for room_ids in dm_map.values():
# Account data should be a list of room IDs. Ignore anything else
if isinstance(room_ids, list):
for room_id in room_ids:
if isinstance(room_id, str):
dm_room_id_set.add(room_id)
if filters.is_dm:
# Only DM rooms please
filtered_room_id_set = filtered_room_id_set.intersection(dm_room_id_set)
else:
# Only non-DM rooms please
filtered_room_id_set = filtered_room_id_set.difference(dm_room_id_set)
if filters.spaces:
raise NotImplementedError()
if filters.is_encrypted:
raise NotImplementedError()
if filters.is_invite:
raise NotImplementedError()
if filters.room_types:
raise NotImplementedError()
if filters.not_room_types:
raise NotImplementedError()
if filters.room_name_like:
raise NotImplementedError()
if filters.tags:
raise NotImplementedError()
if filters.not_tags:
raise NotImplementedError()
return filtered_room_id_set

View File

@@ -22,7 +22,7 @@ from unittest.mock import patch
from twisted.test.proto_helpers import MemoryReactor
from synapse.api.constants import EventTypes, JoinRules, Membership
from synapse.api.constants import AccountDataTypes, EventTypes, JoinRules, Membership
from synapse.api.room_versions import RoomVersions
from synapse.rest import admin
from synapse.rest.client import knock, login, room
@@ -1116,3 +1116,157 @@ class GetSyncRoomIdsForUserEventShardTestCase(BaseMultiWorkerStreamTestCase):
room_id3,
},
)
class FilterRoomsTestCase(HomeserverTestCase):
"""
Tests Sliding Sync handler `filter_rooms()` to make sure it includes/excludes rooms
correctly.
"""
servlets = [
admin.register_servlets,
knock.register_servlets,
login.register_servlets,
room.register_servlets,
]
def default_config(self) -> JsonDict:
config = super().default_config()
# Enable sliding sync
config["experimental_features"] = {"msc3575_enabled": True}
return config
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.sliding_sync_handler = self.hs.get_sliding_sync_handler()
self.store = self.hs.get_datastores().main
def _create_dm_room(
self,
inviter_user_id: str,
inviter_tok: str,
invitee_user_id: str,
invitee_tok: str,
) -> str:
"""
Helper to create a DM room as the "inviter" and invite the "invitee" user to the room. The
"invitee" user also will join the room. The `m.direct` account data will be set
for both users.
"""
# Create a room and send an invite the other user
room_id = self.helper.create_room_as(
inviter_user_id,
is_public=False,
tok=inviter_tok,
)
self.helper.invite(
room_id,
src=inviter_user_id,
targ=invitee_user_id,
tok=inviter_tok,
extra_data={"is_direct": True},
)
# Person that was invited joins the room
self.helper.join(room_id, invitee_user_id, tok=invitee_tok)
# Mimic the client setting the room as a direct message in the global account
# data
self.get_success(
self.store.add_account_data_for_user(
invitee_user_id,
AccountDataTypes.DIRECT,
{inviter_user_id: [room_id]},
)
)
self.get_success(
self.store.add_account_data_for_user(
inviter_user_id,
AccountDataTypes.DIRECT,
{invitee_user_id: [room_id]},
)
)
return room_id
def test_filter_dm_rooms(self) -> None:
"""
Test filter for DM rooms
"""
user1_id = self.register_user("user1", "pass")
user1_tok = self.login(user1_id, "pass")
user2_id = self.register_user("user2", "pass")
user2_tok = self.login(user2_id, "pass")
# Create a normal room
room_id = self.helper.create_room_as(
user1_id,
is_public=False,
tok=user1_tok,
)
# Create a DM room
dm_room_id = self._create_dm_room(
inviter_user_id=user1_id,
inviter_tok=user1_tok,
invitee_user_id=user2_id,
invitee_tok=user2_tok,
)
# TODO: Better way to avoid the circular import? (see
# https://github.com/element-hq/synapse/pull/17187#discussion_r1619492779)
from synapse.handlers.sliding_sync import SlidingSyncConfig
filters = SlidingSyncConfig.SlidingSyncList.Filters(
is_dm=True,
)
# Try filtering the rooms
filtered_room_ids = self.get_success(
self.sliding_sync_handler.filter_rooms(
UserID.from_string(user1_id), {room_id, dm_room_id}, filters
)
)
self.assertEqual(filtered_room_ids, {dm_room_id})
def test_filter_non_dm_rooms(self) -> None:
"""
Test filter for non-DM rooms
"""
user1_id = self.register_user("user1", "pass")
user1_tok = self.login(user1_id, "pass")
user2_id = self.register_user("user2", "pass")
user2_tok = self.login(user2_id, "pass")
# Create a normal room
room_id = self.helper.create_room_as(
user1_id,
is_public=False,
tok=user1_tok,
)
# Create a DM room
dm_room_id = self._create_dm_room(
inviter_user_id=user1_id,
inviter_tok=user1_tok,
invitee_user_id=user2_id,
invitee_tok=user2_tok,
)
# TODO: Better way to avoid the circular import? (see
# https://github.com/element-hq/synapse/pull/17187#discussion_r1619492779)
from synapse.handlers.sliding_sync import SlidingSyncConfig
filters = SlidingSyncConfig.SlidingSyncList.Filters(
is_dm=False,
)
# Try filtering the rooms
filtered_room_ids = self.get_success(
self.sliding_sync_handler.filter_rooms(
UserID.from_string(user1_id), {room_id, dm_room_id}, filters
)
)
self.assertEqual(filtered_room_ids, {room_id})

View File

@@ -27,6 +27,7 @@ from twisted.test.proto_helpers import MemoryReactor
import synapse.rest.admin
from synapse.api.constants import (
AccountDataTypes,
EventContentFields,
EventTypes,
ReceiptTypes,
@@ -1226,10 +1227,59 @@ class SlidingSyncTestCase(unittest.HomeserverTestCase):
return config
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = hs.get_datastores().main
self.sync_endpoint = "/_matrix/client/unstable/org.matrix.msc3575/sync"
self.store = hs.get_datastores().main
self.event_sources = hs.get_event_sources()
def _create_dm_room(
self,
inviter_user_id: str,
inviter_tok: str,
invitee_user_id: str,
invitee_tok: str,
) -> str:
"""
Helper to create a DM room as the "inviter" and invite the "invitee" user to the
room. The "invitee" user also will join the room. The `m.direct` account data
will be set for both users.
"""
# Create a room and send an invite the other user
room_id = self.helper.create_room_as(
inviter_user_id,
is_public=False,
tok=inviter_tok,
)
self.helper.invite(
room_id,
src=inviter_user_id,
targ=invitee_user_id,
tok=inviter_tok,
extra_data={"is_direct": True},
)
# Person that was invited joins the room
self.helper.join(room_id, invitee_user_id, tok=invitee_tok)
# Mimic the client setting the room as a direct message in the global account
# data
self.get_success(
self.store.add_account_data_for_user(
invitee_user_id,
AccountDataTypes.DIRECT,
{inviter_user_id: [room_id]},
)
)
self.get_success(
self.store.add_account_data_for_user(
inviter_user_id,
AccountDataTypes.DIRECT,
{invitee_user_id: [room_id]},
)
)
return room_id
def test_sync_list(self) -> None:
"""
Test that room IDs show up in the Sliding Sync lists
@@ -1336,3 +1386,80 @@ class SlidingSyncTestCase(unittest.HomeserverTestCase):
self.assertEqual(
channel.json_body["next_pos"], future_position_token_serialized
)
def test_filter_list(self) -> None:
"""
Test that filters apply to lists
"""
user1_id = self.register_user("user1", "pass")
user1_tok = self.login(user1_id, "pass")
user2_id = self.register_user("user2", "pass")
user2_tok = self.login(user2_id, "pass")
# Create a DM room
dm_room_id = self._create_dm_room(
inviter_user_id=user1_id,
inviter_tok=user1_tok,
invitee_user_id=user2_id,
invitee_tok=user2_tok,
)
# Create a normal room
room_id = self.helper.create_room_as(user1_id, tok=user1_tok, is_public=True)
# Make the Sliding Sync request
channel = self.make_request(
"POST",
self.sync_endpoint,
{
"lists": {
"dms": {
"ranges": [[0, 99]],
"sort": ["by_recency"],
"required_state": [],
"timeline_limit": 1,
"filters": {"is_dm": True},
},
"foo-list": {
"ranges": [[0, 99]],
"sort": ["by_recency"],
"required_state": [],
"timeline_limit": 1,
"filters": {"is_dm": False},
},
}
},
access_token=user1_tok,
)
self.assertEqual(channel.code, 200, channel.json_body)
# Make sure it has the foo-list we requested
self.assertListEqual(
list(channel.json_body["lists"].keys()),
["dms", "foo-list"],
channel.json_body["lists"].keys(),
)
# Make sure the list includes the room we are joined to
self.assertListEqual(
list(channel.json_body["lists"]["dms"]["ops"]),
[
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [dm_room_id],
}
],
list(channel.json_body["lists"]["dms"]),
)
self.assertListEqual(
list(channel.json_body["lists"]["foo-list"]["ops"]),
[
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [room_id],
}
],
list(channel.json_body["lists"]["foo-list"]),
)