2018-07-17 06:43:18 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-07-17 06:43:18 -04:00
|
|
|
#
|
|
|
|
#
|
2019-10-30 12:47:37 -04:00
|
|
|
import json
|
2023-09-18 09:02:12 -04:00
|
|
|
from typing import List
|
2019-10-30 14:01:56 -04:00
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
from parameterized import parameterized, parameterized_class
|
2021-11-09 05:26:07 -05:00
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2019-05-01 10:32:38 -04:00
|
|
|
import synapse.rest.admin
|
2021-07-28 04:05:11 -04:00
|
|
|
from synapse.api.constants import (
|
2024-06-13 14:56:58 -04:00
|
|
|
AccountDataTypes,
|
2021-07-28 04:05:11 -04:00
|
|
|
EventContentFields,
|
|
|
|
EventTypes,
|
2022-04-28 13:34:12 -04:00
|
|
|
ReceiptTypes,
|
2021-07-28 04:05:11 -04:00
|
|
|
RelationTypes,
|
|
|
|
)
|
2022-02-07 08:21:19 -05:00
|
|
|
from synapse.rest.client import devices, knock, login, read_marker, receipts, room, sync
|
2022-02-23 08:33:19 -05:00
|
|
|
from synapse.server import HomeServer
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
from synapse.types import JsonDict, RoomStreamToken, StreamKeyType
|
2022-02-23 08:33:19 -05:00
|
|
|
from synapse.util import Clock
|
2018-07-17 06:43:18 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
2021-06-09 14:39:51 -04:00
|
|
|
from tests.federation.transport.test_knocking import (
|
|
|
|
KnockingStrippedStateEventHelperMixin,
|
|
|
|
)
|
2018-11-02 09:19:23 -04:00
|
|
|
from tests.server import TimedOutException
|
2018-07-17 06:43:18 -04:00
|
|
|
|
|
|
|
|
2018-08-17 11:08:45 -04:00
|
|
|
class FilterTestCase(unittest.HomeserverTestCase):
|
|
|
|
user_id = "@apple:test"
|
2019-10-30 12:47:37 -04:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
2018-07-17 06:43:18 -04:00
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_sync_argless(self) -> None:
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", "/sync")
|
2018-07-17 06:43:18 -04:00
|
|
|
|
2018-08-17 11:08:45 -04:00
|
|
|
self.assertEqual(channel.code, 200)
|
2021-06-23 10:57:41 -04:00
|
|
|
self.assertIn("next_batch", channel.json_body)
|
2018-11-02 09:19:23 -04:00
|
|
|
|
|
|
|
|
2019-10-30 12:47:37 -04:00
|
|
|
class SyncFilterTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_sync_filter_labels(self) -> None:
|
2019-10-30 13:28:41 -04:00
|
|
|
"""Test that we can filter by a label."""
|
2019-10-30 12:47:37 -04:00
|
|
|
sync_filter = json.dumps(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"types": [EventTypes.Message],
|
|
|
|
"org.matrix.labels": ["#fun"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
events = self._test_sync_filter_labels(sync_filter)
|
|
|
|
|
2019-10-30 13:28:41 -04:00
|
|
|
self.assertEqual(len(events), 2, [event["content"] for event in events])
|
|
|
|
self.assertEqual(events[0]["content"]["body"], "with right label", events[0])
|
|
|
|
self.assertEqual(events[1]["content"]["body"], "with right label", events[1])
|
2019-10-30 12:47:37 -04:00
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_sync_filter_not_labels(self) -> None:
|
2019-10-30 13:28:41 -04:00
|
|
|
"""Test that we can filter by the absence of a label."""
|
2019-10-30 12:47:37 -04:00
|
|
|
sync_filter = json.dumps(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"types": [EventTypes.Message],
|
|
|
|
"org.matrix.not_labels": ["#fun"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
events = self._test_sync_filter_labels(sync_filter)
|
|
|
|
|
2019-10-30 13:28:41 -04:00
|
|
|
self.assertEqual(len(events), 3, [event["content"] for event in events])
|
2019-10-30 12:47:37 -04:00
|
|
|
self.assertEqual(events[0]["content"]["body"], "without label", events[0])
|
|
|
|
self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
|
2019-10-30 14:01:56 -04:00
|
|
|
self.assertEqual(
|
|
|
|
events[2]["content"]["body"], "with two wrong labels", events[2]
|
|
|
|
)
|
2019-10-30 13:28:41 -04:00
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_sync_filter_labels_not_labels(self) -> None:
|
2019-10-30 13:28:41 -04:00
|
|
|
"""Test that we can filter by both a label and the absence of another label."""
|
|
|
|
sync_filter = json.dumps(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"types": [EventTypes.Message],
|
|
|
|
"org.matrix.labels": ["#work"],
|
|
|
|
"org.matrix.not_labels": ["#notfun"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
events = self._test_sync_filter_labels(sync_filter)
|
|
|
|
|
|
|
|
self.assertEqual(len(events), 1, [event["content"] for event in events])
|
|
|
|
self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
|
2019-10-30 12:47:37 -04:00
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def _test_sync_filter_labels(self, sync_filter: str) -> List[JsonDict]:
|
2019-10-30 12:47:37 -04:00
|
|
|
user_id = self.register_user("kermit", "test")
|
|
|
|
tok = self.login("kermit", "test")
|
|
|
|
|
|
|
|
room_id = self.helper.create_room_as(user_id, tok=tok)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
2019-10-30 13:28:41 -04:00
|
|
|
"body": "with right label",
|
2019-11-01 12:22:44 -04:00
|
|
|
EventContentFields.LABELS: ["#fun"],
|
2019-10-30 12:47:37 -04:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
2019-10-30 14:01:56 -04:00
|
|
|
content={"msgtype": "m.text", "body": "without label"},
|
2019-10-30 12:47:37 -04:00
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "with wrong label",
|
2019-11-01 12:22:44 -04:00
|
|
|
EventContentFields.LABELS: ["#work"],
|
2019-10-30 12:47:37 -04:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
2019-10-30 13:28:41 -04:00
|
|
|
"body": "with two wrong labels",
|
2019-11-01 12:22:44 -04:00
|
|
|
EventContentFields.LABELS: ["#work", "#notfun"],
|
2019-10-30 13:28:41 -04:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "with right label",
|
2019-11-01 12:22:44 -04:00
|
|
|
EventContentFields.LABELS: ["#fun"],
|
2019-10-30 12:47:37 -04:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2019-10-30 12:47:37 -04:00
|
|
|
"GET", "/sync?filter=%s" % sync_filter, access_token=tok
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
return channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
|
|
|
|
|
|
|
|
|
2018-11-02 09:19:23 -04:00
|
|
|
class SyncTypingTests(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
2019-05-02 06:59:16 -04:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2018-11-02 09:19:23 -04:00
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
user_id = True
|
|
|
|
hijack_auth = False
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_sync_backwards_typing(self) -> None:
|
2018-11-02 09:19:23 -04:00
|
|
|
"""
|
|
|
|
If the typing serial goes backwards and the typing handler is then reset
|
|
|
|
(such as when the master restarts and sets the typing serial to 0), we
|
|
|
|
do not incorrectly return typing information that had a serial greater
|
|
|
|
than the now-reset serial.
|
|
|
|
"""
|
|
|
|
typing_url = "/rooms/%s/typing/%s?access_token=%s"
|
|
|
|
sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
|
|
|
|
|
|
|
|
# Register the user who gets notified
|
|
|
|
user_id = self.register_user("user", "pass")
|
|
|
|
access_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
# Register the user who sends the message
|
|
|
|
other_user_id = self.register_user("otheruser", "pass")
|
|
|
|
other_access_token = self.login("otheruser", "pass")
|
|
|
|
|
|
|
|
# Create a room
|
|
|
|
room = self.helper.create_room_as(user_id, tok=access_token)
|
|
|
|
|
|
|
|
# Invite the other person
|
|
|
|
self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
|
|
|
|
|
|
|
|
# The other user joins
|
|
|
|
self.helper.join(room=room, user=other_user_id, tok=other_access_token)
|
|
|
|
|
|
|
|
# The other user sends some messages
|
|
|
|
self.helper.send(room, body="Hi!", tok=other_access_token)
|
|
|
|
self.helper.send(room, body="There!", tok=other_access_token)
|
|
|
|
|
|
|
|
# Start typing.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2018-11-02 09:19:23 -04:00
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": true, "timeout": 30000}',
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", "/sync?access_token=%s" % (access_token,))
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Stop typing.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2018-11-02 09:19:23 -04:00
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": false}',
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
|
|
|
|
# Start typing.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2018-11-02 09:19:23 -04:00
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": true, "timeout": 30000}',
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
|
|
|
|
# Should return immediately
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", sync_url % (access_token, next_batch))
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Reset typing serial back to 0, as if the master had.
|
|
|
|
typing = self.hs.get_typing_handler()
|
|
|
|
typing._latest_room_serial = 0
|
|
|
|
|
|
|
|
# Since it checks the state token, we need some state to update to
|
|
|
|
# invalidate the stream token.
|
|
|
|
self.helper.send(room, body="There!", tok=other_access_token)
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", sync_url % (access_token, next_batch))
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# This should time out! But it does not, because our stream token is
|
|
|
|
# ahead, and therefore it's saying the typing (that we've actually
|
|
|
|
# already seen) is new, since it's got a token above our new, now-reset
|
|
|
|
# stream token.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request("GET", sync_url % (access_token, next_batch))
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code)
|
2018-11-02 09:19:23 -04:00
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Clear the typing information, so that it doesn't think everything is
|
|
|
|
# in the future.
|
|
|
|
typing._reset()
|
|
|
|
|
|
|
|
# Now it SHOULD fail as it never completes!
|
2020-11-15 17:49:21 -05:00
|
|
|
with self.assertRaises(TimedOutException):
|
|
|
|
self.make_request("GET", sync_url % (access_token, next_batch))
|
2020-09-02 12:19:37 -04:00
|
|
|
|
|
|
|
|
2023-01-25 14:38:20 -05:00
|
|
|
class SyncKnockTestCase(KnockingStrippedStateEventHelperMixin):
|
2021-06-09 14:39:51 -04:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
knock.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-06-09 14:39:51 -04:00
|
|
|
self.url = "/sync?since=%s"
|
|
|
|
self.next_batch = "s0"
|
|
|
|
|
|
|
|
# Register the first user (used to create the room to knock on).
|
|
|
|
self.user_id = self.register_user("kermit", "monkey")
|
|
|
|
self.tok = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# Create the room we'll knock on.
|
|
|
|
self.room_id = self.helper.create_room_as(
|
|
|
|
self.user_id,
|
|
|
|
is_public=False,
|
2021-06-15 07:45:14 -04:00
|
|
|
room_version="7",
|
2021-06-09 14:39:51 -04:00
|
|
|
tok=self.tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Register the second user (used to knock on the room).
|
|
|
|
self.knocker = self.register_user("knocker", "monkey")
|
|
|
|
self.knocker_tok = self.login("knocker", "monkey")
|
|
|
|
|
|
|
|
# Perform an initial sync for the knocking user.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url % self.next_batch,
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Store the next batch for the next request.
|
|
|
|
self.next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Set up some room state to test with.
|
|
|
|
self.expected_room_state = self.send_example_state_events_to_room(
|
|
|
|
hs, self.room_id, self.user_id
|
|
|
|
)
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_knock_room_state(self) -> None:
|
2021-06-09 14:39:51 -04:00
|
|
|
"""Tests that /sync returns state from a room after knocking on it."""
|
|
|
|
# Knock on a room
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
2022-05-04 11:59:22 -04:00
|
|
|
f"/_matrix/client/r0/knock/{self.room_id}",
|
2021-06-09 14:39:51 -04:00
|
|
|
b"{}",
|
|
|
|
self.knocker_tok,
|
|
|
|
)
|
2022-02-28 07:12:29 -05:00
|
|
|
self.assertEqual(200, channel.code, channel.result)
|
2021-06-09 14:39:51 -04:00
|
|
|
|
|
|
|
# We expect to see the knock event in the stripped room state later
|
|
|
|
self.expected_room_state[EventTypes.Member] = {
|
2021-06-15 07:45:14 -04:00
|
|
|
"content": {"membership": "knock", "displayname": "knocker"},
|
2021-06-09 14:39:51 -04:00
|
|
|
"state_key": "@knocker:test",
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check that /sync includes stripped state from the room
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url % self.next_batch,
|
|
|
|
access_token=self.knocker_tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Extract the stripped room state events from /sync
|
2021-06-15 07:45:14 -04:00
|
|
|
knock_entry = channel.json_body["rooms"]["knock"]
|
2021-06-09 14:39:51 -04:00
|
|
|
room_state_events = knock_entry[self.room_id]["knock_state"]["events"]
|
|
|
|
|
|
|
|
# Validate that the knock membership event came last
|
|
|
|
self.assertEqual(room_state_events[-1]["type"], EventTypes.Member)
|
|
|
|
|
|
|
|
# Validate the stripped room state events
|
|
|
|
self.check_knock_room_state_against_room_state(
|
|
|
|
room_state_events, self.expected_room_state
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-09-02 12:19:37 -04:00
|
|
|
class UnreadMessagesTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
read_marker.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
sync.register_servlets,
|
2021-07-28 04:05:11 -04:00
|
|
|
receipts.register_servlets,
|
2020-09-02 12:19:37 -04:00
|
|
|
]
|
|
|
|
|
2022-03-31 15:05:13 -04:00
|
|
|
def default_config(self) -> JsonDict:
|
|
|
|
config = super().default_config()
|
2022-05-04 11:59:22 -04:00
|
|
|
config["experimental_features"] = {
|
|
|
|
"msc2654_enabled": True,
|
|
|
|
}
|
2022-03-31 15:05:13 -04:00
|
|
|
return config
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-09-02 12:19:37 -04:00
|
|
|
self.url = "/sync?since=%s"
|
|
|
|
self.next_batch = "s0"
|
|
|
|
|
|
|
|
# Register the first user (used to check the unread counts).
|
|
|
|
self.user_id = self.register_user("kermit", "monkey")
|
|
|
|
self.tok = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# Create the room we'll check unread counts for.
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
|
|
|
|
|
|
|
|
# Register the second user (used to send events to the room).
|
|
|
|
self.user2 = self.register_user("kermit2", "monkey")
|
|
|
|
self.tok2 = self.login("kermit2", "monkey")
|
|
|
|
|
|
|
|
# Change the power levels of the room so that the second user can send state
|
|
|
|
# events.
|
|
|
|
self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
EventTypes.PowerLevels,
|
|
|
|
{
|
|
|
|
"users": {self.user_id: 100, self.user2: 100},
|
|
|
|
"users_default": 0,
|
|
|
|
"events": {
|
|
|
|
"m.room.name": 50,
|
|
|
|
"m.room.power_levels": 100,
|
|
|
|
"m.room.history_visibility": 100,
|
|
|
|
"m.room.canonical_alias": 50,
|
|
|
|
"m.room.avatar": 50,
|
|
|
|
"m.room.tombstone": 100,
|
|
|
|
"m.room.server_acl": 100,
|
|
|
|
"m.room.encryption": 100,
|
|
|
|
},
|
|
|
|
"events_default": 0,
|
|
|
|
"state_default": 50,
|
|
|
|
"ban": 50,
|
|
|
|
"kick": 50,
|
|
|
|
"redact": 50,
|
|
|
|
"invite": 0,
|
|
|
|
},
|
|
|
|
tok=self.tok,
|
|
|
|
)
|
|
|
|
|
2022-09-01 08:31:54 -04:00
|
|
|
def test_unread_counts(self) -> None:
|
2020-09-02 12:19:37 -04:00
|
|
|
"""Tests that /sync returns the right value for the unread count (MSC2654)."""
|
|
|
|
|
|
|
|
# Check that our own messages don't increase the unread count.
|
|
|
|
self.helper.send(self.room_id, "hello", tok=self.tok)
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
|
|
|
# Join the new user and check that this doesn't increase the unread count.
|
|
|
|
self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
|
|
|
# Check that the new user sending a message increases our unread count.
|
|
|
|
res = self.helper.send(self.room_id, "hello", tok=self.tok2)
|
|
|
|
self._check_unread_count(1)
|
|
|
|
|
|
|
|
# Send a read receipt to tell the server we've read the latest event.
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-09-02 12:19:37 -04:00
|
|
|
"POST",
|
2022-05-04 11:59:22 -04:00
|
|
|
f"/rooms/{self.room_id}/read_markers",
|
2022-07-17 17:28:45 -04:00
|
|
|
{ReceiptTypes.READ: res["event_id"]},
|
2020-09-02 12:19:37 -04:00
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check that the unread counter is back to 0.
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
2022-05-05 08:31:25 -04:00
|
|
|
# Check that private read receipts don't break unread counts
|
2021-07-28 04:05:11 -04:00
|
|
|
res = self.helper.send(self.room_id, "hello", tok=self.tok2)
|
|
|
|
self._check_unread_count(1)
|
|
|
|
|
|
|
|
# Send a read receipt to tell the server we've read the latest event.
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
2022-09-01 08:31:54 -04:00
|
|
|
f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
|
2022-05-04 11:59:22 -04:00
|
|
|
{},
|
2021-07-28 04:05:11 -04:00
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check that the unread counter is back to 0.
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
2020-09-02 12:19:37 -04:00
|
|
|
# Check that room name changes increase the unread counter.
|
|
|
|
self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
"m.room.name",
|
|
|
|
{"name": "my super room"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
self._check_unread_count(1)
|
|
|
|
|
|
|
|
# Check that room topic changes increase the unread counter.
|
|
|
|
self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
"m.room.topic",
|
|
|
|
{"topic": "welcome!!!"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
self._check_unread_count(2)
|
|
|
|
|
|
|
|
# Check that encrypted messages increase the unread counter.
|
|
|
|
self.helper.send_event(self.room_id, EventTypes.Encrypted, {}, tok=self.tok2)
|
|
|
|
self._check_unread_count(3)
|
|
|
|
|
|
|
|
# Check that custom events with a body increase the unread counter.
|
2022-05-16 08:42:45 -04:00
|
|
|
result = self.helper.send_event(
|
2020-09-02 12:19:37 -04:00
|
|
|
self.room_id,
|
|
|
|
"org.matrix.custom_type",
|
|
|
|
{"body": "hello"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
2022-05-16 08:42:45 -04:00
|
|
|
event_id = result["event_id"]
|
2020-09-02 12:19:37 -04:00
|
|
|
self._check_unread_count(4)
|
|
|
|
|
|
|
|
# Check that edits don't increase the unread counter.
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=self.room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"body": "hello",
|
|
|
|
"msgtype": "m.text",
|
2022-05-16 08:42:45 -04:00
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationTypes.REPLACE,
|
|
|
|
"event_id": event_id,
|
|
|
|
},
|
2020-09-02 12:19:37 -04:00
|
|
|
},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
self._check_unread_count(4)
|
|
|
|
|
|
|
|
# Check that notices don't increase the unread counter.
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=self.room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={"body": "hello", "msgtype": "m.notice"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
self._check_unread_count(4)
|
|
|
|
|
|
|
|
# Check that tombstone events changes increase the unread counter.
|
2022-05-04 11:59:22 -04:00
|
|
|
res1 = self.helper.send_state(
|
2020-09-02 12:19:37 -04:00
|
|
|
self.room_id,
|
|
|
|
EventTypes.Tombstone,
|
|
|
|
{"replacement_room": "!someroom:test"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
self._check_unread_count(5)
|
2022-05-04 11:59:22 -04:00
|
|
|
res2 = self.helper.send(self.room_id, "hello", tok=self.tok2)
|
|
|
|
|
2022-08-05 11:09:33 -04:00
|
|
|
# Make sure both m.read and m.read.private advance
|
2022-05-04 11:59:22 -04:00
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
f"/rooms/{self.room_id}/receipt/m.read/{res1['event_id']}",
|
|
|
|
{},
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
self._check_unread_count(1)
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
2022-09-01 08:31:54 -04:00
|
|
|
f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res2['event_id']}",
|
2022-05-04 11:59:22 -04:00
|
|
|
{},
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
2022-08-05 11:09:33 -04:00
|
|
|
# We test for all three receipt types that influence notification counts
|
|
|
|
@parameterized.expand(
|
|
|
|
[
|
|
|
|
ReceiptTypes.READ,
|
|
|
|
ReceiptTypes.READ_PRIVATE,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_read_receipts_only_go_down(self, receipt_type: str) -> None:
|
2022-05-04 11:59:22 -04:00
|
|
|
# Join the new user
|
|
|
|
self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
|
|
|
|
|
|
|
|
# Send messages
|
|
|
|
res1 = self.helper.send(self.room_id, "hello", tok=self.tok2)
|
|
|
|
res2 = self.helper.send(self.room_id, "hello", tok=self.tok2)
|
|
|
|
|
|
|
|
# Read last event
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
2022-09-01 08:31:54 -04:00
|
|
|
f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res2['event_id']}",
|
2022-05-04 11:59:22 -04:00
|
|
|
{},
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
2022-08-05 11:09:33 -04:00
|
|
|
# Make sure neither m.read nor m.read.private make the
|
2022-05-04 11:59:22 -04:00
|
|
|
# read receipt go up to an older event
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
2022-09-01 08:31:54 -04:00
|
|
|
f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res1['event_id']}",
|
2022-05-04 11:59:22 -04:00
|
|
|
{},
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
self._check_unread_count(0)
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
f"/rooms/{self.room_id}/receipt/m.read/{res1['event_id']}",
|
|
|
|
{},
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
self._check_unread_count(0)
|
2020-09-02 12:19:37 -04:00
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def _check_unread_count(self, expected_count: int) -> None:
|
2020-09-02 12:19:37 -04:00
|
|
|
"""Syncs and compares the unread count with the expected value."""
|
|
|
|
|
2020-12-15 09:44:04 -05:00
|
|
|
channel = self.make_request(
|
2020-09-02 12:19:37 -04:00
|
|
|
"GET",
|
|
|
|
self.url % self.next_batch,
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
2022-05-04 11:59:22 -04:00
|
|
|
room_entry = (
|
|
|
|
channel.json_body.get("rooms", {}).get("join", {}).get(self.room_id, {})
|
|
|
|
)
|
2020-09-02 12:19:37 -04:00
|
|
|
self.assertEqual(
|
2022-05-04 11:59:22 -04:00
|
|
|
room_entry.get("org.matrix.msc2654.unread_count", 0),
|
2020-09-02 12:19:37 -04:00
|
|
|
expected_count,
|
|
|
|
room_entry,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Store the next batch for the next request.
|
|
|
|
self.next_batch = channel.json_body["next_batch"]
|
2021-06-17 11:23:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SyncCacheTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_noop_sync_does_not_tightloop(self) -> None:
|
2021-06-17 11:23:11 -04:00
|
|
|
"""If the sync times out, we shouldn't cache the result
|
|
|
|
|
2023-11-15 08:02:11 -05:00
|
|
|
Essentially a regression test for https://github.com/matrix-org/synapse/issues/8518.
|
2021-06-17 11:23:11 -04:00
|
|
|
"""
|
|
|
|
self.user_id = self.register_user("kermit", "monkey")
|
|
|
|
self.tok = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# we should immediately get an initial sync response
|
|
|
|
channel = self.make_request("GET", "/sync", access_token=self.tok)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# now, make an incremental sync request, with a timeout
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/sync?since={next_batch}&timeout=10000",
|
|
|
|
access_token=self.tok,
|
|
|
|
await_result=False,
|
|
|
|
)
|
|
|
|
# that should block for 10 seconds
|
|
|
|
with self.assertRaises(TimedOutException):
|
|
|
|
channel.await_result(timeout_ms=9900)
|
|
|
|
channel.await_result(timeout_ms=200)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# we expect the next_batch in the result to be the same as before
|
|
|
|
self.assertEqual(channel.json_body["next_batch"], next_batch)
|
|
|
|
|
|
|
|
# another incremental sync should also block.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/sync?since={next_batch}&timeout=10000",
|
|
|
|
access_token=self.tok,
|
|
|
|
await_result=False,
|
|
|
|
)
|
|
|
|
# that should block for 10 seconds
|
|
|
|
with self.assertRaises(TimedOutException):
|
|
|
|
channel.await_result(timeout_ms=9900)
|
|
|
|
channel.await_result(timeout_ms=200)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
2022-02-07 08:21:19 -05:00
|
|
|
|
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
@parameterized_class(
|
|
|
|
("sync_endpoint", "experimental_features"),
|
|
|
|
[
|
|
|
|
("/sync", {}),
|
|
|
|
(
|
|
|
|
"/_matrix/client/unstable/org.matrix.msc3575/sync/e2ee",
|
|
|
|
# Enable sliding sync
|
|
|
|
{"msc3575_enabled": True},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2022-02-07 08:21:19 -05:00
|
|
|
class DeviceListSyncTestCase(unittest.HomeserverTestCase):
|
2024-05-23 13:06:16 -04:00
|
|
|
"""
|
|
|
|
Tests regarding device list (`device_lists`) changes.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
sync_endpoint: The endpoint under test to use for syncing.
|
|
|
|
experimental_features: The experimental features homeserver config to use.
|
|
|
|
"""
|
|
|
|
|
|
|
|
sync_endpoint: str
|
|
|
|
experimental_features: JsonDict
|
|
|
|
|
2022-02-07 08:21:19 -05:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
2024-05-23 13:06:16 -04:00
|
|
|
room.register_servlets,
|
2022-02-07 08:21:19 -05:00
|
|
|
sync.register_servlets,
|
|
|
|
devices.register_servlets,
|
|
|
|
]
|
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
def default_config(self) -> JsonDict:
|
|
|
|
config = super().default_config()
|
|
|
|
config["experimental_features"] = self.experimental_features
|
|
|
|
return config
|
|
|
|
|
|
|
|
def test_receiving_local_device_list_changes(self) -> None:
|
|
|
|
"""Tests that a local users that share a room receive each other's device list
|
|
|
|
changes.
|
|
|
|
"""
|
|
|
|
# Register two users
|
|
|
|
test_device_id = "TESTDEVICE"
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
bob_user_id = self.register_user("bob", "ponyponypony")
|
|
|
|
bob_access_token = self.login(bob_user_id, "ponyponypony")
|
|
|
|
|
|
|
|
# Create a room for them to coexist peacefully in
|
|
|
|
new_room_id = self.helper.create_room_as(
|
|
|
|
alice_user_id, is_public=True, tok=alice_access_token
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(new_room_id)
|
|
|
|
|
|
|
|
# Have Bob join the room
|
|
|
|
self.helper.invite(
|
|
|
|
new_room_id, alice_user_id, bob_user_id, tok=alice_access_token
|
|
|
|
)
|
|
|
|
self.helper.join(new_room_id, bob_user_id, tok=bob_access_token)
|
|
|
|
|
|
|
|
# Now have Bob initiate an initial sync (in order to get a since token)
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.sync_endpoint,
|
|
|
|
access_token=bob_access_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
next_batch_token = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# ...and then an incremental sync. This should block until the sync stream is woken up,
|
|
|
|
# which we hope will happen as a result of Alice updating their device list.
|
|
|
|
bob_sync_channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"{self.sync_endpoint}?since={next_batch_token}&timeout=30000",
|
|
|
|
access_token=bob_access_token,
|
|
|
|
# Start the request, then continue on.
|
|
|
|
await_result=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Have alice update their device list
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
f"/devices/{test_device_id}",
|
|
|
|
{
|
|
|
|
"display_name": "New Device Name",
|
|
|
|
},
|
|
|
|
access_token=alice_access_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check that bob's incremental sync contains the updated device list.
|
|
|
|
# If not, the client would only receive the device list update on the
|
|
|
|
# *next* sync.
|
|
|
|
bob_sync_channel.await_result()
|
|
|
|
self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body)
|
|
|
|
|
|
|
|
changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get(
|
|
|
|
"changed", []
|
|
|
|
)
|
|
|
|
self.assertIn(alice_user_id, changed_device_lists, bob_sync_channel.json_body)
|
|
|
|
|
|
|
|
def test_not_receiving_local_device_list_changes(self) -> None:
|
|
|
|
"""Tests a local users DO NOT receive device updates from each other if they do not
|
|
|
|
share a room.
|
|
|
|
"""
|
|
|
|
# Register two users
|
|
|
|
test_device_id = "TESTDEVICE"
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
bob_user_id = self.register_user("bob", "ponyponypony")
|
|
|
|
bob_access_token = self.login(bob_user_id, "ponyponypony")
|
|
|
|
|
|
|
|
# These users do not share a room. They are lonely.
|
|
|
|
|
|
|
|
# Have Bob initiate an initial sync (in order to get a since token)
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.sync_endpoint,
|
|
|
|
access_token=bob_access_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
next_batch_token = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# ...and then an incremental sync. This should block until the sync stream is woken up,
|
|
|
|
# which we hope will happen as a result of Alice updating their device list.
|
|
|
|
bob_sync_channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"{self.sync_endpoint}?since={next_batch_token}&timeout=1000",
|
|
|
|
access_token=bob_access_token,
|
|
|
|
# Start the request, then continue on.
|
|
|
|
await_result=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Have alice update their device list
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
f"/devices/{test_device_id}",
|
|
|
|
{
|
|
|
|
"display_name": "New Device Name",
|
|
|
|
},
|
|
|
|
access_token=alice_access_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check that bob's incremental sync does not contain the updated device list.
|
|
|
|
bob_sync_channel.await_result()
|
|
|
|
self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body)
|
|
|
|
|
|
|
|
changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get(
|
|
|
|
"changed", []
|
|
|
|
)
|
|
|
|
self.assertNotIn(
|
|
|
|
alice_user_id, changed_device_lists, bob_sync_channel.json_body
|
|
|
|
)
|
|
|
|
|
2022-02-23 08:33:19 -05:00
|
|
|
def test_user_with_no_rooms_receives_self_device_list_updates(self) -> None:
|
2022-02-07 08:21:19 -05:00
|
|
|
"""Tests that a user with no rooms still receives their own device list updates"""
|
2024-05-23 13:06:16 -04:00
|
|
|
test_device_id = "TESTDEVICE"
|
2022-02-07 08:21:19 -05:00
|
|
|
|
|
|
|
# Register a user and login, creating a device
|
2024-05-23 13:06:16 -04:00
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
2022-02-07 08:21:19 -05:00
|
|
|
|
|
|
|
# Request an initial sync
|
2024-05-23 13:06:16 -04:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET", self.sync_endpoint, access_token=alice_access_token
|
|
|
|
)
|
2022-02-07 08:21:19 -05:00
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Now, make an incremental sync request.
|
|
|
|
# It won't return until something has happened
|
|
|
|
incremental_sync_channel = self.make_request(
|
|
|
|
"GET",
|
2024-05-23 13:06:16 -04:00
|
|
|
f"{self.sync_endpoint}?since={next_batch}&timeout=30000",
|
|
|
|
access_token=alice_access_token,
|
2022-02-07 08:21:19 -05:00
|
|
|
await_result=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Change our device's display name
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
2024-05-23 13:06:16 -04:00
|
|
|
f"devices/{test_device_id}",
|
2022-02-07 08:21:19 -05:00
|
|
|
{
|
|
|
|
"display_name": "freeze ray",
|
|
|
|
},
|
2024-05-23 13:06:16 -04:00
|
|
|
access_token=alice_access_token,
|
2022-02-07 08:21:19 -05:00
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# The sync should now have returned
|
|
|
|
incremental_sync_channel.await_result(timeout_ms=20000)
|
|
|
|
self.assertEqual(incremental_sync_channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# We should have received notification that the (user's) device has changed
|
|
|
|
device_list_changes = incremental_sync_channel.json_body.get(
|
|
|
|
"device_lists", {}
|
|
|
|
).get("changed", [])
|
|
|
|
|
|
|
|
self.assertIn(
|
2024-05-23 13:06:16 -04:00
|
|
|
alice_user_id, device_list_changes, incremental_sync_channel.json_body
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@parameterized_class(
|
|
|
|
("sync_endpoint", "experimental_features"),
|
|
|
|
[
|
|
|
|
("/sync", {}),
|
|
|
|
(
|
|
|
|
"/_matrix/client/unstable/org.matrix.msc3575/sync/e2ee",
|
|
|
|
# Enable sliding sync
|
|
|
|
{"msc3575_enabled": True},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
class DeviceOneTimeKeysSyncTestCase(unittest.HomeserverTestCase):
|
|
|
|
"""
|
|
|
|
Tests regarding device one time keys (`device_one_time_keys_count`) changes.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
sync_endpoint: The endpoint under test to use for syncing.
|
|
|
|
experimental_features: The experimental features homeserver config to use.
|
|
|
|
"""
|
|
|
|
|
|
|
|
sync_endpoint: str
|
|
|
|
experimental_features: JsonDict
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
devices.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def default_config(self) -> JsonDict:
|
|
|
|
config = super().default_config()
|
|
|
|
config["experimental_features"] = self.experimental_features
|
|
|
|
return config
|
|
|
|
|
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
|
|
|
self.e2e_keys_handler = hs.get_e2e_keys_handler()
|
|
|
|
|
|
|
|
def test_no_device_one_time_keys(self) -> None:
|
|
|
|
"""
|
|
|
|
Tests when no one time keys set, it still has the default `signed_curve25519` in
|
|
|
|
`device_one_time_keys_count`
|
|
|
|
"""
|
|
|
|
test_device_id = "TESTDEVICE"
|
|
|
|
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request an initial sync
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET", self.sync_endpoint, access_token=alice_access_token
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check for those one time key counts
|
|
|
|
self.assertDictEqual(
|
|
|
|
channel.json_body["device_one_time_keys_count"],
|
|
|
|
# Note that "signed_curve25519" is always returned in key count responses
|
|
|
|
# regardless of whether we uploaded any keys for it. This is necessary until
|
|
|
|
# https://github.com/matrix-org/matrix-doc/issues/3298 is fixed.
|
|
|
|
{"signed_curve25519": 0},
|
|
|
|
channel.json_body["device_one_time_keys_count"],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_returns_device_one_time_keys(self) -> None:
|
|
|
|
"""
|
|
|
|
Tests that one time keys for the device/user are counted correctly in the `/sync`
|
|
|
|
response
|
|
|
|
"""
|
|
|
|
test_device_id = "TESTDEVICE"
|
|
|
|
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# Upload one time keys for the user/device
|
|
|
|
keys: JsonDict = {
|
|
|
|
"alg1:k1": "key1",
|
|
|
|
"alg2:k2": {"key": "key2", "signatures": {"k1": "sig1"}},
|
|
|
|
"alg2:k3": {"key": "key3"},
|
|
|
|
}
|
|
|
|
res = self.get_success(
|
|
|
|
self.e2e_keys_handler.upload_keys_for_user(
|
|
|
|
alice_user_id, test_device_id, {"one_time_keys": keys}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# Note that "signed_curve25519" is always returned in key count responses
|
|
|
|
# regardless of whether we uploaded any keys for it. This is necessary until
|
|
|
|
# https://github.com/matrix-org/matrix-doc/issues/3298 is fixed.
|
|
|
|
self.assertDictEqual(
|
|
|
|
res,
|
|
|
|
{"one_time_key_counts": {"alg1": 1, "alg2": 2, "signed_curve25519": 0}},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request an initial sync
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET", self.sync_endpoint, access_token=alice_access_token
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check for those one time key counts
|
|
|
|
self.assertDictEqual(
|
|
|
|
channel.json_body["device_one_time_keys_count"],
|
|
|
|
{"alg1": 1, "alg2": 2, "signed_curve25519": 0},
|
|
|
|
channel.json_body["device_one_time_keys_count"],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@parameterized_class(
|
|
|
|
("sync_endpoint", "experimental_features"),
|
|
|
|
[
|
|
|
|
("/sync", {}),
|
|
|
|
(
|
|
|
|
"/_matrix/client/unstable/org.matrix.msc3575/sync/e2ee",
|
|
|
|
# Enable sliding sync
|
|
|
|
{"msc3575_enabled": True},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
class DeviceUnusedFallbackKeySyncTestCase(unittest.HomeserverTestCase):
|
|
|
|
"""
|
|
|
|
Tests regarding device one time keys (`device_unused_fallback_key_types`) changes.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
sync_endpoint: The endpoint under test to use for syncing.
|
|
|
|
experimental_features: The experimental features homeserver config to use.
|
|
|
|
"""
|
|
|
|
|
|
|
|
sync_endpoint: str
|
|
|
|
experimental_features: JsonDict
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
devices.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def default_config(self) -> JsonDict:
|
|
|
|
config = super().default_config()
|
|
|
|
config["experimental_features"] = self.experimental_features
|
|
|
|
return config
|
|
|
|
|
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
|
|
|
self.store = self.hs.get_datastores().main
|
|
|
|
self.e2e_keys_handler = hs.get_e2e_keys_handler()
|
|
|
|
|
|
|
|
def test_no_device_unused_fallback_key(self) -> None:
|
|
|
|
"""
|
|
|
|
Test when no unused fallback key is set, it just returns an empty list. The MSC
|
|
|
|
says "The device_unused_fallback_key_types parameter must be present if the
|
|
|
|
server supports fallback keys.",
|
|
|
|
https://github.com/matrix-org/matrix-spec-proposals/blob/54255851f642f84a4f1aaf7bc063eebe3d76752b/proposals/2732-olm-fallback-keys.md
|
|
|
|
"""
|
|
|
|
test_device_id = "TESTDEVICE"
|
|
|
|
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request an initial sync
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET", self.sync_endpoint, access_token=alice_access_token
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check for those one time key counts
|
|
|
|
self.assertListEqual(
|
|
|
|
channel.json_body["device_unused_fallback_key_types"],
|
|
|
|
[],
|
|
|
|
channel.json_body["device_unused_fallback_key_types"],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_returns_device_one_time_keys(self) -> None:
|
|
|
|
"""
|
|
|
|
Tests that device unused fallback key type is returned correctly in the `/sync`
|
|
|
|
"""
|
|
|
|
test_device_id = "TESTDEVICE"
|
|
|
|
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(
|
|
|
|
alice_user_id, "correcthorse", device_id=test_device_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# We shouldn't have any unused fallback keys yet
|
|
|
|
res = self.get_success(
|
|
|
|
self.store.get_e2e_unused_fallback_key_types(alice_user_id, test_device_id)
|
|
|
|
)
|
|
|
|
self.assertEqual(res, [])
|
|
|
|
|
|
|
|
# Upload a fallback key for the user/device
|
|
|
|
fallback_key = {"alg1:k1": "fallback_key1"}
|
|
|
|
self.get_success(
|
|
|
|
self.e2e_keys_handler.upload_keys_for_user(
|
|
|
|
alice_user_id,
|
|
|
|
test_device_id,
|
|
|
|
{"fallback_keys": fallback_key},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# We should now have an unused alg1 key
|
|
|
|
fallback_res = self.get_success(
|
|
|
|
self.store.get_e2e_unused_fallback_key_types(alice_user_id, test_device_id)
|
|
|
|
)
|
|
|
|
self.assertEqual(fallback_res, ["alg1"], fallback_res)
|
|
|
|
|
|
|
|
# Request an initial sync
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET", self.sync_endpoint, access_token=alice_access_token
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# Check for the unused fallback key types
|
|
|
|
self.assertListEqual(
|
|
|
|
channel.json_body["device_unused_fallback_key_types"],
|
|
|
|
["alg1"],
|
|
|
|
channel.json_body["device_unused_fallback_key_types"],
|
2022-02-07 08:21:19 -05:00
|
|
|
)
|
2022-03-30 05:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ExcludeRoomTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def prepare(
|
|
|
|
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
|
|
|
|
) -> None:
|
|
|
|
self.user_id = self.register_user("user", "password")
|
|
|
|
self.tok = self.login("user", "password")
|
|
|
|
|
|
|
|
self.excluded_room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
|
|
|
|
self.included_room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
|
|
|
|
|
|
|
|
# We need to manually append the room ID, because we can't know the ID before
|
|
|
|
# creating the room, and we can't set the config after starting the homeserver.
|
2023-01-23 10:44:39 -05:00
|
|
|
self.hs.get_sync_handler().rooms_to_exclude_globally.append(
|
|
|
|
self.excluded_room_id
|
|
|
|
)
|
2022-03-30 05:43:04 -04:00
|
|
|
|
|
|
|
def test_join_leave(self) -> None:
|
|
|
|
"""Tests that rooms are correctly excluded from the 'join' and 'leave' sections of
|
|
|
|
sync responses.
|
|
|
|
"""
|
|
|
|
channel = self.make_request("GET", "/sync", access_token=self.tok)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["join"])
|
|
|
|
self.assertIn(self.included_room_id, channel.json_body["rooms"]["join"])
|
|
|
|
|
|
|
|
self.helper.leave(self.excluded_room_id, self.user_id, tok=self.tok)
|
|
|
|
self.helper.leave(self.included_room_id, self.user_id, tok=self.tok)
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
"/sync?since=" + channel.json_body["next_batch"],
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["leave"])
|
|
|
|
self.assertIn(self.included_room_id, channel.json_body["rooms"]["leave"])
|
|
|
|
|
|
|
|
def test_invite(self) -> None:
|
|
|
|
"""Tests that rooms are correctly excluded from the 'invite' section of sync
|
|
|
|
responses.
|
|
|
|
"""
|
|
|
|
invitee = self.register_user("invitee", "password")
|
|
|
|
invitee_tok = self.login("invitee", "password")
|
|
|
|
|
|
|
|
self.helper.invite(self.excluded_room_id, self.user_id, invitee, tok=self.tok)
|
|
|
|
self.helper.invite(self.included_room_id, self.user_id, invitee, tok=self.tok)
|
|
|
|
|
|
|
|
channel = self.make_request("GET", "/sync", access_token=invitee_tok)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["invite"])
|
|
|
|
self.assertIn(self.included_room_id, channel.json_body["rooms"]["invite"])
|
2022-08-04 05:02:29 -04:00
|
|
|
|
|
|
|
def test_incremental_sync(self) -> None:
|
|
|
|
"""Tests that activity in the room is properly filtered out of incremental
|
|
|
|
syncs.
|
|
|
|
"""
|
|
|
|
channel = self.make_request("GET", "/sync", access_token=self.tok)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
self.helper.send(self.excluded_room_id, tok=self.tok)
|
|
|
|
self.helper.send(self.included_room_id, tok=self.tok)
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/sync?since={next_batch}",
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200, channel.result)
|
|
|
|
|
|
|
|
self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["join"])
|
|
|
|
self.assertIn(self.included_room_id, channel.json_body["rooms"]["join"])
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SlidingSyncTestCase(unittest.HomeserverTestCase):
|
|
|
|
"""
|
|
|
|
Tests regarding MSC3575 Sliding Sync `/sync` endpoint.
|
|
|
|
"""
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
devices.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:
|
2024-06-13 14:56:58 -04:00
|
|
|
self.store = hs.get_datastores().main
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
self.sync_endpoint = "/_matrix/client/unstable/org.matrix.msc3575/sync"
|
|
|
|
self.store = hs.get_datastores().main
|
|
|
|
self.event_sources = hs.get_event_sources()
|
|
|
|
|
2024-06-13 14:56:58 -04:00
|
|
|
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
|
|
|
|
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
def test_sync_list(self) -> None:
|
|
|
|
"""
|
|
|
|
Test that room IDs show up in the Sliding Sync lists
|
|
|
|
"""
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(alice_user_id, "correcthorse")
|
|
|
|
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
alice_user_id, tok=alice_access_token, is_public=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make the Sliding Sync request
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.sync_endpoint,
|
|
|
|
{
|
|
|
|
"lists": {
|
|
|
|
"foo-list": {
|
|
|
|
"ranges": [[0, 99]],
|
|
|
|
"sort": ["by_notification_level", "by_recency", "by_name"],
|
|
|
|
"required_state": [
|
|
|
|
["m.room.join_rules", ""],
|
|
|
|
["m.room.history_visibility", ""],
|
|
|
|
["m.space.child", "*"],
|
|
|
|
],
|
|
|
|
"timeline_limit": 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
access_token=alice_access_token,
|
|
|
|
)
|
|
|
|
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()),
|
|
|
|
["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"]["foo-list"]["ops"]),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"op": "SYNC",
|
|
|
|
"range": [0, 99],
|
|
|
|
"room_ids": [room_id],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
channel.json_body["lists"]["foo-list"],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_wait_for_sync_token(self) -> None:
|
|
|
|
"""
|
|
|
|
Test that worker will wait until it catches up to the given token
|
|
|
|
"""
|
|
|
|
alice_user_id = self.register_user("alice", "correcthorse")
|
|
|
|
alice_access_token = self.login(alice_user_id, "correcthorse")
|
|
|
|
|
|
|
|
# Create a future token that will cause us to wait. Since we never send a new
|
|
|
|
# event to reach that future stream_ordering, the worker will wait until the
|
|
|
|
# full timeout.
|
|
|
|
current_token = self.event_sources.get_current_token()
|
|
|
|
future_position_token = current_token.copy_and_replace(
|
|
|
|
StreamKeyType.ROOM,
|
|
|
|
RoomStreamToken(stream=current_token.room_key.stream + 1),
|
|
|
|
)
|
|
|
|
|
|
|
|
future_position_token_serialized = self.get_success(
|
|
|
|
future_position_token.to_string(self.store)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make the Sliding Sync request
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.sync_endpoint + f"?pos={future_position_token_serialized}",
|
|
|
|
{
|
|
|
|
"lists": {
|
|
|
|
"foo-list": {
|
|
|
|
"ranges": [[0, 99]],
|
|
|
|
"sort": ["by_notification_level", "by_recency", "by_name"],
|
|
|
|
"required_state": [
|
|
|
|
["m.room.join_rules", ""],
|
|
|
|
["m.room.history_visibility", ""],
|
|
|
|
["m.space.child", "*"],
|
|
|
|
],
|
|
|
|
"timeline_limit": 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
access_token=alice_access_token,
|
|
|
|
await_result=False,
|
|
|
|
)
|
|
|
|
# Block for 10 seconds to make `notifier.wait_for_stream_token(from_token)`
|
|
|
|
# timeout
|
|
|
|
with self.assertRaises(TimedOutException):
|
|
|
|
channel.await_result(timeout_ms=9900)
|
|
|
|
channel.await_result(timeout_ms=200)
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
# We expect the `next_pos` in the result to be the same as what we requested
|
|
|
|
# with because we weren't able to find anything new yet.
|
|
|
|
self.assertEqual(
|
|
|
|
channel.json_body["next_pos"], future_position_token_serialized
|
|
|
|
)
|
2024-06-13 14:56:58 -04:00
|
|
|
|
|
|
|
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"]),
|
|
|
|
)
|