2019-11-05 11:56:39 +01:00
|
|
|
# Copyright 2018-2019 New Vector Ltd
|
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
2018-07-17 12:43:18 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2019-10-30 17:47:37 +01:00
|
|
|
import json
|
2019-10-30 19:01:56 +01:00
|
|
|
|
2021-11-09 11:26:07 +01:00
|
|
|
from parameterized import parameterized
|
|
|
|
|
2019-05-01 16:32:38 +02:00
|
|
|
import synapse.rest.admin
|
2021-07-28 10:05:11 +02:00
|
|
|
from synapse.api.constants import (
|
|
|
|
EventContentFields,
|
|
|
|
EventTypes,
|
|
|
|
ReadReceiptEventFields,
|
|
|
|
RelationTypes,
|
|
|
|
)
|
2021-08-17 13:57:58 +02:00
|
|
|
from synapse.rest.client import knock, login, read_marker, receipts, room, sync
|
2018-07-17 12:43:18 +02:00
|
|
|
|
|
|
|
from tests import unittest
|
2021-06-09 20:39:51 +02:00
|
|
|
from tests.federation.transport.test_knocking import (
|
|
|
|
KnockingStrippedStateEventHelperMixin,
|
|
|
|
)
|
2018-11-02 14:19:23 +01:00
|
|
|
from tests.server import TimedOutException
|
2021-06-09 20:39:51 +02:00
|
|
|
from tests.unittest import override_config
|
2018-07-17 12:43:18 +02:00
|
|
|
|
|
|
|
|
2018-08-17 17:08:45 +02:00
|
|
|
class FilterTestCase(unittest.HomeserverTestCase):
|
2018-07-17 12:43:18 +02:00
|
|
|
|
2018-08-17 17:08:45 +02:00
|
|
|
user_id = "@apple:test"
|
2019-10-30 17:47:37 +01:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
2018-07-17 12:43:18 +02:00
|
|
|
|
2018-08-17 17:08:45 +02:00
|
|
|
def test_sync_argless(self):
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request("GET", "/sync")
|
2018-07-17 12:43:18 +02:00
|
|
|
|
2018-08-17 17:08:45 +02:00
|
|
|
self.assertEqual(channel.code, 200)
|
2021-06-23 16:57:41 +02:00
|
|
|
self.assertIn("next_batch", channel.json_body)
|
2018-11-02 14:19:23 +01:00
|
|
|
|
|
|
|
|
2019-10-30 17:47:37 +01:00
|
|
|
class SyncFilterTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_sync_filter_labels(self):
|
2019-10-30 18:28:41 +01:00
|
|
|
"""Test that we can filter by a label."""
|
2019-10-30 17:47:37 +01: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 18:28:41 +01: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 17:47:37 +01:00
|
|
|
|
|
|
|
def test_sync_filter_not_labels(self):
|
2019-10-30 18:28:41 +01:00
|
|
|
"""Test that we can filter by the absence of a label."""
|
2019-10-30 17:47:37 +01: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 18:28:41 +01:00
|
|
|
self.assertEqual(len(events), 3, [event["content"] for event in events])
|
2019-10-30 17:47:37 +01: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 19:01:56 +01:00
|
|
|
self.assertEqual(
|
|
|
|
events[2]["content"]["body"], "with two wrong labels", events[2]
|
|
|
|
)
|
2019-10-30 18:28:41 +01:00
|
|
|
|
|
|
|
def test_sync_filter_labels_not_labels(self):
|
|
|
|
"""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 17:47:37 +01:00
|
|
|
|
|
|
|
def _test_sync_filter_labels(self, sync_filter):
|
|
|
|
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 18:28:41 +01:00
|
|
|
"body": "with right label",
|
2019-11-01 17:22:44 +01:00
|
|
|
EventContentFields.LABELS: ["#fun"],
|
2019-10-30 17:47:37 +01:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
2019-10-30 19:01:56 +01:00
|
|
|
content={"msgtype": "m.text", "body": "without label"},
|
2019-10-30 17:47:37 +01: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 17:22:44 +01:00
|
|
|
EventContentFields.LABELS: ["#work"],
|
2019-10-30 17:47:37 +01:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper.send_event(
|
|
|
|
room_id=room_id,
|
|
|
|
type=EventTypes.Message,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
2019-10-30 18:28:41 +01:00
|
|
|
"body": "with two wrong labels",
|
2019-11-01 17:22:44 +01:00
|
|
|
EventContentFields.LABELS: ["#work", "#notfun"],
|
2019-10-30 18:28:41 +01: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 17:22:44 +01:00
|
|
|
EventContentFields.LABELS: ["#fun"],
|
2019-10-30 17:47:37 +01:00
|
|
|
},
|
|
|
|
tok=tok,
|
|
|
|
)
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2019-10-30 17:47:37 +01: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 14:19:23 +01:00
|
|
|
class SyncTypingTests(unittest.HomeserverTestCase):
|
|
|
|
|
|
|
|
servlets = [
|
2019-05-02 12:59:16 +02:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2018-11-02 14:19:23 +01:00
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
user_id = True
|
|
|
|
hijack_auth = False
|
|
|
|
|
|
|
|
def test_sync_backwards_typing(self):
|
|
|
|
"""
|
|
|
|
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 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2018-11-02 14:19:23 +01:00
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": true, "timeout": 30000}',
|
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request("GET", "/sync?access_token=%s" % (access_token,))
|
2018-11-02 14:19:23 +01:00
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Stop typing.
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2018-11-02 14:19:23 +01:00
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": false}',
|
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
|
|
|
|
# Start typing.
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2018-11-02 14:19:23 +01:00
|
|
|
"PUT",
|
|
|
|
typing_url % (room, other_user_id, other_access_token),
|
|
|
|
b'{"typing": true, "timeout": 30000}',
|
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
|
|
|
|
# Should return immediately
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request("GET", sync_url % (access_token, next_batch))
|
2018-11-02 14:19:23 +01:00
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
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 15:44:04 +01:00
|
|
|
channel = self.make_request("GET", sync_url % (access_token, next_batch))
|
2018-11-02 14:19:23 +01:00
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
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 15:44:04 +01:00
|
|
|
channel = self.make_request("GET", sync_url % (access_token, next_batch))
|
2018-11-02 14:19:23 +01:00
|
|
|
self.assertEquals(200, channel.code)
|
|
|
|
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 23:49:21 +01:00
|
|
|
with self.assertRaises(TimedOutException):
|
|
|
|
self.make_request("GET", sync_url % (access_token, next_batch))
|
2020-09-02 18:19:37 +02:00
|
|
|
|
|
|
|
|
2021-06-09 20:39:51 +02:00
|
|
|
class SyncKnockTestCase(
|
|
|
|
unittest.HomeserverTestCase, KnockingStrippedStateEventHelperMixin
|
|
|
|
):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
knock.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
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 13:45:14 +02:00
|
|
|
room_version="7",
|
2021-06-09 20:39:51 +02: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
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_config({"experimental_features": {"msc2403_enabled": True}})
|
|
|
|
def test_knock_room_state(self):
|
|
|
|
"""Tests that /sync returns state from a room after knocking on it."""
|
|
|
|
# Knock on a room
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
2021-06-15 13:45:14 +02:00
|
|
|
"/_matrix/client/r0/knock/%s" % (self.room_id,),
|
2021-06-09 20:39:51 +02:00
|
|
|
b"{}",
|
|
|
|
self.knocker_tok,
|
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.result)
|
|
|
|
|
|
|
|
# We expect to see the knock event in the stripped room state later
|
|
|
|
self.expected_room_state[EventTypes.Member] = {
|
2021-06-15 13:45:14 +02:00
|
|
|
"content": {"membership": "knock", "displayname": "knocker"},
|
2021-06-09 20:39:51 +02: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 13:45:14 +02:00
|
|
|
knock_entry = channel.json_body["rooms"]["knock"]
|
2021-06-09 20:39:51 +02: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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-28 10:05:11 +02:00
|
|
|
class ReadReceiptsTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
receipts.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
self.url = "/sync?since=%s"
|
|
|
|
self.next_batch = "s0"
|
|
|
|
|
|
|
|
# Register the first user
|
|
|
|
self.user_id = self.register_user("kermit", "monkey")
|
|
|
|
self.tok = self.login("kermit", "monkey")
|
|
|
|
|
|
|
|
# Create the room
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
|
|
|
|
|
|
|
|
# Register the second user
|
|
|
|
self.user2 = self.register_user("kermit2", "monkey")
|
|
|
|
self.tok2 = self.login("kermit2", "monkey")
|
|
|
|
|
|
|
|
# Join the second user
|
|
|
|
self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
|
|
|
|
|
|
|
|
@override_config({"experimental_features": {"msc2285_enabled": True}})
|
|
|
|
def test_hidden_read_receipts(self):
|
|
|
|
# Send a message as the first user
|
|
|
|
res = self.helper.send(self.room_id, body="hello", tok=self.tok)
|
|
|
|
|
|
|
|
# Send a read receipt to tell the server the first user's message was read
|
|
|
|
body = json.dumps({ReadReceiptEventFields.MSC2285_HIDDEN: True}).encode("utf8")
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/rooms/%s/receipt/m.read/%s" % (self.room_id, res["event_id"]),
|
|
|
|
body,
|
|
|
|
access_token=self.tok2,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
# Test that the first user can't see the other user's hidden read receipt
|
|
|
|
self.assertEqual(self._get_read_receipt(), None)
|
|
|
|
|
2021-11-09 11:26:07 +01:00
|
|
|
@parameterized.expand(
|
|
|
|
[
|
|
|
|
# Old Element version, expected to send an empty body
|
|
|
|
(
|
|
|
|
"agent1",
|
|
|
|
"Element/1.2.2 (Linux; U; Android 9; MatrixAndroidSDK_X 0.0.1)",
|
|
|
|
200,
|
|
|
|
),
|
|
|
|
# Old SchildiChat version, expected to send an empty body
|
|
|
|
("agent2", "SchildiChat/1.2.1 (Android 10)", 200),
|
|
|
|
# Expected 400: Denies empty body starting at version 1.3+
|
|
|
|
("agent3", "Element/1.3.6 (Android 10)", 400),
|
|
|
|
("agent4", "SchildiChat/1.3.6 (Android 11)", 400),
|
|
|
|
# Contains "Riot": Receipts with empty bodies expected
|
|
|
|
("agent5", "Element (Riot.im) (Android 9)", 200),
|
|
|
|
# Expected 400: Does not contain "Android"
|
|
|
|
("agent6", "Element/1.2.1", 400),
|
|
|
|
# Expected 400: Different format, missing "/" after Element; existing build that should allow empty bodies, but minimal ongoing usage
|
|
|
|
("agent7", "Element dbg/1.1.8-dev (Android)", 400),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_read_receipt_with_empty_body(
|
|
|
|
self, name, user_agent: str, expected_status_code: int
|
|
|
|
):
|
2021-08-04 16:13:24 +02:00
|
|
|
# Send a message as the first user
|
|
|
|
res = self.helper.send(self.room_id, body="hello", tok=self.tok)
|
|
|
|
|
|
|
|
# Send a read receipt for this message with an empty body
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/rooms/%s/receipt/m.read/%s" % (self.room_id, res["event_id"]),
|
|
|
|
access_token=self.tok2,
|
2021-11-09 11:26:07 +01:00
|
|
|
custom_headers=[("User-Agent", user_agent)],
|
2021-08-04 16:13:24 +02:00
|
|
|
)
|
2021-11-09 11:26:07 +01:00
|
|
|
self.assertEqual(channel.code, expected_status_code)
|
2021-08-04 16:13:24 +02:00
|
|
|
|
2021-07-28 10:05:11 +02:00
|
|
|
def _get_read_receipt(self):
|
|
|
|
"""Syncs and returns the read receipt."""
|
|
|
|
|
|
|
|
# Checks if event is a read receipt
|
|
|
|
def is_read_receipt(event):
|
|
|
|
return event["type"] == "m.receipt"
|
|
|
|
|
|
|
|
# Sync
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
self.url % self.next_batch,
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
# Store the next batch for the next request.
|
|
|
|
self.next_batch = channel.json_body["next_batch"]
|
|
|
|
|
|
|
|
# Return the read receipt
|
|
|
|
ephemeral_events = channel.json_body["rooms"]["join"][self.room_id][
|
|
|
|
"ephemeral"
|
|
|
|
]["events"]
|
|
|
|
return next(filter(is_read_receipt, ephemeral_events), None)
|
|
|
|
|
|
|
|
|
2020-09-02 18:19:37 +02: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 10:05:11 +02:00
|
|
|
receipts.register_servlets,
|
2020-09-02 18:19:37 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unread_counts(self):
|
|
|
|
"""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.
|
|
|
|
body = json.dumps({"m.read": res["event_id"]}).encode("utf8")
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-09-02 18:19:37 +02:00
|
|
|
"POST",
|
|
|
|
"/rooms/%s/read_markers" % self.room_id,
|
|
|
|
body,
|
|
|
|
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)
|
|
|
|
|
2021-07-28 10:05:11 +02:00
|
|
|
# Check that hidden read receipts don't break unread counts
|
|
|
|
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.
|
|
|
|
body = json.dumps({ReadReceiptEventFields.MSC2285_HIDDEN: True}).encode("utf8")
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/rooms/%s/receipt/m.read/%s" % (self.room_id, res["event_id"]),
|
|
|
|
body,
|
|
|
|
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 18:19:37 +02: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.
|
|
|
|
self.helper.send_event(
|
|
|
|
self.room_id,
|
|
|
|
"org.matrix.custom_type",
|
|
|
|
{"body": "hello"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
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",
|
|
|
|
"m.relates_to": {"rel_type": RelationTypes.REPLACE},
|
|
|
|
},
|
|
|
|
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.
|
|
|
|
self.helper.send_state(
|
|
|
|
self.room_id,
|
|
|
|
EventTypes.Tombstone,
|
|
|
|
{"replacement_room": "!someroom:test"},
|
|
|
|
tok=self.tok2,
|
|
|
|
)
|
|
|
|
self._check_unread_count(5)
|
|
|
|
|
2021-06-09 20:39:51 +02:00
|
|
|
def _check_unread_count(self, expected_count: int):
|
2020-09-02 18:19:37 +02:00
|
|
|
"""Syncs and compares the unread count with the expected value."""
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-09-02 18:19:37 +02:00
|
|
|
"GET",
|
|
|
|
self.url % self.next_batch,
|
|
|
|
access_token=self.tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(channel.code, 200, channel.json_body)
|
|
|
|
|
|
|
|
room_entry = channel.json_body["rooms"]["join"][self.room_id]
|
|
|
|
self.assertEqual(
|
|
|
|
room_entry["org.matrix.msc2654.unread_count"],
|
|
|
|
expected_count,
|
|
|
|
room_entry,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Store the next batch for the next request.
|
|
|
|
self.next_batch = channel.json_body["next_batch"]
|
2021-06-17 17:23:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SyncCacheTestCase(unittest.HomeserverTestCase):
|
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
login.register_servlets,
|
|
|
|
sync.register_servlets,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_noop_sync_does_not_tightloop(self):
|
|
|
|
"""If the sync times out, we shouldn't cache the result
|
|
|
|
|
|
|
|
Essentially a regression test for #8518.
|
|
|
|
"""
|
|
|
|
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)
|