Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_dm filtering to Sliding Sync /sync #17244

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
f9e6e53
Configurable /sync/e2ee endpoint
MadLittleMods May 6, 2024
1e05a05
Add Sliding Sync `/sync/e2ee` endpoint for To-Device messages
MadLittleMods May 7, 2024
5e925f6
Share tests with test_sendtodevice
MadLittleMods May 8, 2024
69f9143
Comment on tests
MadLittleMods May 8, 2024
d4ff933
Prefer Sync v2 vs Sliding Sync distinction
MadLittleMods May 8, 2024
371ec57
Fix wait_for_sync_for_user in tests
MadLittleMods May 8, 2024
06d12e5
Ugly overloads
MadLittleMods May 8, 2024
b8b70ba
Fix lint
MadLittleMods May 8, 2024
c60a4f8
Add changelog
MadLittleMods May 8, 2024
10ffae6
Shared logic for `get_sync_result_builder()`
MadLittleMods May 8, 2024
6bf4896
Try calculate more
MadLittleMods May 9, 2024
8871dac
Share tests using inheritance
MadLittleMods May 9, 2024
0892283
Add comments docs
MadLittleMods May 9, 2024
adb7e20
Consolidate device_lists /sync tests
MadLittleMods May 9, 2024
f098355
Add `device_one_time_keys_count` tests
MadLittleMods May 9, 2024
6b7cfd7
Add tests for `device_unused_fallback_key_types` in `/sync`
MadLittleMods May 9, 2024
b9e5379
Describe test
MadLittleMods May 9, 2024
f9c9d44
Add stub Sliding Sync endpoint
MadLittleMods May 13, 2024
654e8f6
Add Pydantic model for the Sliding Sync API
MadLittleMods May 13, 2024
aee594a
Can't use StringConstraints
MadLittleMods May 14, 2024
2863fba
More optional
MadLittleMods May 15, 2024
2dd0cde
Fill out more options
MadLittleMods May 15, 2024
c8256b6
Start to map out response
MadLittleMods May 15, 2024
ee6baba
Iterating
MadLittleMods May 15, 2024
f3db068
Copy body to config
MadLittleMods May 15, 2024
9bdfa16
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee
MadLittleMods May 16, 2024
7331401
Lint
MadLittleMods May 16, 2024
b23abca
Fix test inheritance
MadLittleMods May 16, 2024
821a1b3
Add missing field to docstring
MadLittleMods May 16, 2024
35ca937
Format docstring
MadLittleMods May 16, 2024
4ad7a8b
No need to change this formatting from develop
MadLittleMods May 16, 2024
3092ab5
Calculate room derived membership info for device_lists
MadLittleMods May 20, 2024
3539abe
Membership in timeline for better derived info
MadLittleMods May 20, 2024
5f194f9
Exclude application services
MadLittleMods May 20, 2024
02cecfa
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee
MadLittleMods May 20, 2024
bfa8c63
Merge branch 'madlittlemods/msc3575-sliding-sync-e2ee' into madlittle…
MadLittleMods May 20, 2024
07d84ab
Start of gathering room list to display in sync
MadLittleMods May 20, 2024
6dadfe9
Try handle no from_token or to_token already newer
MadLittleMods May 20, 2024
9ffafe7
Try to think about this logic
MadLittleMods May 21, 2024
f6122ff
Use `client_patterns()` for endpoint URL
MadLittleMods May 21, 2024
2f112e7
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee
MadLittleMods May 21, 2024
c2221bb
Lint
MadLittleMods May 21, 2024
717b160
Adjust wording, add todo
MadLittleMods May 21, 2024
c826550
Add some tests
MadLittleMods May 21, 2024
fe48188
Handle more edge cases
MadLittleMods May 21, 2024
fd355f6
WIP
MadLittleMods May 21, 2024
dd9356a
Using unsigned prev_content
MadLittleMods May 21, 2024
17783c3
Log why no unsigned
MadLittleMods May 22, 2024
343de8f
Remove debug logs
MadLittleMods May 22, 2024
1b3a5bf
Fix referencing variable from other lexical scope
MadLittleMods May 22, 2024
c82a084
Update comments and test docstrings
MadLittleMods May 22, 2024
514aba5
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-e2ee
MadLittleMods May 22, 2024
9749795
Update filter to be more precise and avoid more work
MadLittleMods May 22, 2024
06ac1da
Restore copyright header
MadLittleMods May 22, 2024
3da6bc1
Use `@parameterized_class`
MadLittleMods May 22, 2024
d4b41aa
Fix lints
MadLittleMods May 22, 2024
89db566
Merge branch 'madlittlemods/msc3575-sliding-sync-e2ee' into madlittle…
MadLittleMods May 22, 2024
c7b8743
Add changelog
MadLittleMods May 22, 2024
a7c6476
Use `client_patterns()`
MadLittleMods May 22, 2024
13d6146
Fill out sliding window response types
MadLittleMods May 22, 2024
c7f7ae4
Start assembling lists
MadLittleMods May 22, 2024
4c7d7e6
Encode response
MadLittleMods May 22, 2024
6606ac1
Add docstring for parametized attributes
MadLittleMods May 23, 2024
ab0b844
Add actual typing for params (not just docstrings)
MadLittleMods May 23, 2024
a482545
Fix test after removing type ignore
MadLittleMods May 23, 2024
04eeee6
Merge branch 'madlittlemods/msc3575-sliding-sync-e2ee' into madlittle…
MadLittleMods May 23, 2024
8c3de84
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods May 23, 2024
37af87a
Add test to make sure we don't confuse multiple rooms
MadLittleMods May 23, 2024
a822a05
Revert as TODO says
MadLittleMods May 23, 2024
f9fa683
Fix another leaking loop variable
MadLittleMods May 23, 2024
d1bd02d
Add TODO to handle partial stated rooms
MadLittleMods May 23, 2024
b5b3e77
Fix Pydantic `conint`/`constr` usage with mypy
MadLittleMods May 23, 2024
65d9b79
Fix lints
MadLittleMods May 23, 2024
adc0e2f
Fix unserialize type
MadLittleMods May 23, 2024
b12fee5
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods May 28, 2024
44e9a92
Fill in rest docstring
MadLittleMods May 28, 2024
b632cbb
Add better comments
MadLittleMods May 28, 2024
abf139a
Fill out docstring todo
MadLittleMods May 28, 2024
a28569f
Add understanding of this skip
MadLittleMods May 28, 2024
950fd70
Tweak comments
MadLittleMods May 28, 2024
d65c694
Filter DM from account data
MadLittleMods May 29, 2024
1dd04d2
Add tests for DM filter
MadLittleMods May 29, 2024
3b67004
Rename `_create_dm_room`
MadLittleMods May 29, 2024
27465fc
Prefer not None
MadLittleMods May 29, 2024
7804feb
Add changelog
MadLittleMods May 29, 2024
770992c
Fix typo, reword
MadLittleMods May 29, 2024
8bf5a62
Add rest test
MadLittleMods May 29, 2024
34d67fd
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods May 29, 2024
f74cc3f
Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittl…
MadLittleMods May 30, 2024
49998e0
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods May 30, 2024
92ea286
Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittl…
MadLittleMods May 30, 2024
6b1eba4
Add rest test to make sure filters apply
MadLittleMods May 30, 2024
09609cb
WIP: TODO comments after pairing with Erik
MadLittleMods Jun 3, 2024
8f09313
Add instance name alongside stream_ordering (`RoomsForUser.event_pos`)
MadLittleMods Jun 3, 2024
803fbbe
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods Jun 3, 2024
a0c042e
Re-arrange how this list will be returned
MadLittleMods Jun 3, 2024
271a196
Use fully-qualified `PersistedEventPosition` when returning membershi…
MadLittleMods Jun 3, 2024
4155e18
Fix circular imports when running specific tests
MadLittleMods Jun 3, 2024
939695d
Update usage
MadLittleMods Jun 3, 2024
73c20d9
Use method to get instance name in tests
MadLittleMods Jun 3, 2024
7b41f41
Fix random lints
MadLittleMods Jun 3, 2024
09638ac
Add changelog
MadLittleMods Jun 3, 2024
8dca8f5
Merge branch 'madlittlemods/rooms-for-user-event-pos' into madlittlem…
MadLittleMods Jun 3, 2024
9c6ec25
Create `membership_snapshot_token` with `instance_map`
MadLittleMods Jun 4, 2024
1268a54
Properly compare tokens and event positions
MadLittleMods Jun 4, 2024
e4c66b8
Avoid serializing response that will never be heard
MadLittleMods Jun 4, 2024
35db057
Add support for kicks
MadLittleMods Jun 4, 2024
3514aa0
Add licensing headers
MadLittleMods Jun 4, 2024
970a0c6
Adjust wording
MadLittleMods Jun 4, 2024
64df6fb
Revert change that should be separated and is failing
MadLittleMods Jun 4, 2024
8bb357a
Note the extras
MadLittleMods Jun 4, 2024
03dd87a
Add test for `notifier.wait_for_stream_token(from_token)`
MadLittleMods Jun 4, 2024
9e46b2a
Fix typo
MadLittleMods Jun 4, 2024
54dbc27
Add None defaults
MadLittleMods Jun 4, 2024
f6a5905
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods Jun 4, 2024
555ba4b
Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittl…
MadLittleMods Jun 4, 2024
07f57a4
Give a summary of what rooms we're looking for
MadLittleMods Jun 4, 2024
d3ce27b
Balance parenthesis
MadLittleMods Jun 4, 2024
dfee21a
Switch fixup order to fix edge case with newly_left rooms
MadLittleMods Jun 4, 2024
3ce0892
Add test with multiple event persisters that fails the previous `get_…
MadLittleMods Jun 5, 2024
2864837
Allow new `get_sync_room_ids_for_user` implementation to work with mu…
MadLittleMods Jun 5, 2024
2af467d
Remove extra for-loop
MadLittleMods Jun 5, 2024
7bbe2ed
More clear way to express what membership we want to display
MadLittleMods Jun 5, 2024
1fc1b58
Remove assert since we no longer need that information
MadLittleMods Jun 5, 2024
6a6cdc6
Use Set because Tuple doesn't allow - operations
MadLittleMods Jun 5, 2024
278ba63
No need to check from/to token relationship
MadLittleMods Jun 5, 2024
5678307
Add validation for membership
MadLittleMods Jun 5, 2024
703cdc9
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods Jun 5, 2024
c7d1fc3
Fix separator label
MadLittleMods Jun 5, 2024
5078d36
Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittl…
MadLittleMods Jun 5, 2024
1158cc6
Add note
MadLittleMods Jun 5, 2024
0f6646d
Add test for no `from_token`
MadLittleMods Jun 6, 2024
0153a6e
Add test for `from_token` after `to_token`
MadLittleMods Jun 6, 2024
6f10b97
Simplify boolean logic and avoid set construction
MadLittleMods Jun 6, 2024
c89f012
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-0.0.1
MadLittleMods Jun 6, 2024
b457c0b
Merge branch 'madlittlemods/msc3575-sliding-sync-0.0.1' into madlittl…
MadLittleMods Jun 6, 2024
90315b0
Merge branch 'develop' into madlittlemods/msc3575-sliding-sync-filtering
MadLittleMods Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/17244.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `is_dm` filtering to experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint.
85 changes: 80 additions & 5 deletions synapse/handlers/sliding_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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
Expand Down Expand Up @@ -332,11 +332,15 @@ async def current_sync_for_user(
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)

Expand Down Expand Up @@ -608,3 +612,74 @@ async def get_sync_room_ids_for_user(
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
156 changes: 155 additions & 1 deletion tests/handlers/test_sliding_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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
Expand Down Expand Up @@ -1116,3 +1116,157 @@ def test_sharded_event_persisters(self) -> None:
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})
Loading
Loading