2019-05-14 09:59:21 -06:00
|
|
|
# Copyright 2019 New Vector Ltd
|
2021-11-22 10:01:47 -07:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
2019-05-14 09:59:21 -06: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-05-14 09:59:21 -06:00
|
|
|
import itertools
|
2021-10-14 07:19:35 -06:00
|
|
|
import urllib.parse
|
2022-03-02 09:34:14 -07:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2021-12-09 11:16:01 -07:00
|
|
|
from unittest.mock import patch
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2019-05-14 09:59:21 -06:00
|
|
|
from synapse.api.constants import EventTypes, RelationTypes
|
2019-05-17 10:45:51 -06:00
|
|
|
from synapse.rest import admin
|
2021-12-06 08:51:15 -07:00
|
|
|
from synapse.rest.client import login, register, relations, room, sync
|
2022-02-28 10:47:37 -07:00
|
|
|
from synapse.server import HomeServer
|
2022-03-04 05:10:10 -07:00
|
|
|
from synapse.types import JsonDict
|
2022-02-28 10:47:37 -07:00
|
|
|
from synapse.util import Clock
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
from tests import unittest
|
2021-10-14 07:19:35 -06:00
|
|
|
from tests.server import FakeChannel
|
2021-12-09 11:16:01 -07:00
|
|
|
from tests.test_utils import make_awaitable
|
|
|
|
from tests.test_utils.event_injection import inject_event
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
|
2022-03-02 04:56:16 -07:00
|
|
|
class BaseRelationsTestCase(unittest.HomeserverTestCase):
|
2019-05-14 09:59:21 -06:00
|
|
|
servlets = [
|
|
|
|
relations.register_servlets,
|
|
|
|
room.register_servlets,
|
2021-12-06 08:51:15 -07:00
|
|
|
sync.register_servlets,
|
2019-05-14 09:59:21 -06:00
|
|
|
login.register_servlets,
|
2019-05-17 10:45:51 -06:00
|
|
|
register.register_servlets,
|
|
|
|
admin.register_servlets_for_client_rest_resource,
|
2019-05-14 09:59:21 -06:00
|
|
|
]
|
2019-05-17 10:45:51 -06:00
|
|
|
hijack_auth = False
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-03-02 09:34:14 -07:00
|
|
|
def default_config(self) -> Dict[str, Any]:
|
2019-05-14 09:59:21 -06:00
|
|
|
# We need to enable msc1849 support for aggregations
|
2021-10-14 07:19:35 -06:00
|
|
|
config = super().default_config()
|
2021-03-17 10:51:55 -06:00
|
|
|
|
|
|
|
# We enable frozen dicts as relations/edits change event contents, so we
|
|
|
|
# want to test that we don't modify the events in the caches.
|
|
|
|
config["use_frozen_dicts"] = True
|
|
|
|
|
2021-10-14 07:19:35 -06:00
|
|
|
return config
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-11-22 10:01:47 -07:00
|
|
|
|
2019-05-17 10:45:51 -06:00
|
|
|
self.user_id, self.user_token = self._create_user("alice")
|
|
|
|
self.user2_id, self.user2_token = self._create_user("bob")
|
|
|
|
|
|
|
|
self.room = self.helper.create_room_as(self.user_id, tok=self.user_token)
|
|
|
|
self.helper.join(self.room, user=self.user2_id, tok=self.user2_token)
|
|
|
|
res = self.helper.send(self.room, body="Hi!", tok=self.user_token)
|
2019-05-14 09:59:21 -06:00
|
|
|
self.parent_id = res["event_id"]
|
|
|
|
|
2022-03-02 04:56:16 -07:00
|
|
|
def _create_user(self, localpart: str) -> Tuple[str, str]:
|
|
|
|
user_id = self.register_user(localpart, "abc123")
|
|
|
|
access_token = self.login(localpart, "abc123")
|
|
|
|
|
|
|
|
return user_id, access_token
|
|
|
|
|
|
|
|
def _send_relation(
|
|
|
|
self,
|
|
|
|
relation_type: str,
|
|
|
|
event_type: str,
|
|
|
|
key: Optional[str] = None,
|
|
|
|
content: Optional[dict] = None,
|
|
|
|
access_token: Optional[str] = None,
|
|
|
|
parent_id: Optional[str] = None,
|
|
|
|
) -> FakeChannel:
|
|
|
|
"""Helper function to send a relation pointing at `self.parent_id`
|
|
|
|
|
|
|
|
Args:
|
|
|
|
relation_type: One of `RelationTypes`
|
|
|
|
event_type: The type of the event to create
|
|
|
|
key: The aggregation key used for m.annotation relation type.
|
|
|
|
content: The content of the created event. Will be modified to configure
|
|
|
|
the m.relates_to key based on the other provided parameters.
|
|
|
|
access_token: The access token used to send the relation, defaults
|
|
|
|
to `self.user_token`
|
|
|
|
parent_id: The event_id this relation relates to. If None, then self.parent_id
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
FakeChannel
|
2019-05-14 09:59:21 -06:00
|
|
|
"""
|
2022-03-02 04:56:16 -07:00
|
|
|
if not access_token:
|
|
|
|
access_token = self.user_token
|
|
|
|
|
|
|
|
original_id = parent_id if parent_id else self.parent_id
|
|
|
|
|
|
|
|
if content is None:
|
|
|
|
content = {}
|
|
|
|
content["m.relates_to"] = {
|
|
|
|
"event_id": original_id,
|
|
|
|
"rel_type": relation_type,
|
|
|
|
}
|
|
|
|
if key is not None:
|
|
|
|
content["m.relates_to"]["key"] = key
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
f"/_matrix/client/v3/rooms/{self.room}/send/{event_type}",
|
|
|
|
content,
|
|
|
|
access_token=access_token,
|
|
|
|
)
|
|
|
|
return channel
|
|
|
|
|
|
|
|
|
|
|
|
class RelationsTestCase(BaseRelationsTestCase):
|
|
|
|
def test_send_relation(self) -> None:
|
|
|
|
"""Tests that sending a relation works."""
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2019-05-14 09:59:21 -06:00
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
event_id = channel.json_body["event_id"]
|
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-17 10:45:51 -06:00
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/rooms/{self.room}/event/{event_id}",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
self.assert_dict(
|
|
|
|
{
|
|
|
|
"type": "m.reaction",
|
|
|
|
"sender": self.user_id,
|
|
|
|
"content": {
|
|
|
|
"m.relates_to": {
|
|
|
|
"event_id": self.parent_id,
|
|
|
|
"key": "👍",
|
|
|
|
"rel_type": RelationTypes.ANNOTATION,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
channel.json_body,
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_deny_invalid_event(self) -> None:
|
2021-11-18 06:43:09 -07:00
|
|
|
"""Test that we deny relations on non-existant events"""
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION,
|
|
|
|
EventTypes.Message,
|
|
|
|
parent_id="foo",
|
|
|
|
content={"body": "foo", "msgtype": "m.text"},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(400, channel.code, channel.json_body)
|
2021-11-18 06:43:09 -07:00
|
|
|
|
|
|
|
# Unless that event is referenced from another event!
|
|
|
|
self.get_success(
|
2022-02-23 04:04:02 -07:00
|
|
|
self.hs.get_datastores().main.db_pool.simple_insert(
|
2021-11-18 06:43:09 -07:00
|
|
|
table="event_relations",
|
|
|
|
values={
|
|
|
|
"event_id": "bar",
|
|
|
|
"relates_to_id": "foo",
|
|
|
|
"relation_type": RelationTypes.THREAD,
|
|
|
|
},
|
|
|
|
desc="test_deny_invalid_event",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
EventTypes.Message,
|
|
|
|
parent_id="foo",
|
|
|
|
content={"body": "foo", "msgtype": "m.text"},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-18 06:43:09 -07:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_deny_invalid_room(self) -> None:
|
2021-11-18 06:43:09 -07:00
|
|
|
"""Test that we deny relations on non-existant events"""
|
|
|
|
# Create another room and send a message in it.
|
|
|
|
room2 = self.helper.create_room_as(self.user_id, tok=self.user_token)
|
|
|
|
res = self.helper.send(room2, body="Hi!", tok=self.user_token)
|
|
|
|
parent_id = res["event_id"]
|
|
|
|
|
|
|
|
# Attempt to send an annotation to that event.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", parent_id=parent_id, key="A"
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(400, channel.code, channel.json_body)
|
2021-11-18 06:43:09 -07:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_deny_double_react(self) -> None:
|
2019-05-20 10:39:05 -06:00
|
|
|
"""Test that we deny relations on membership events"""
|
2019-07-18 07:41:42 -06:00
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-20 10:39:05 -06:00
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(400, channel.code, channel.json_body)
|
2019-05-20 10:39:05 -06:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_deny_forked_thread(self) -> None:
|
2021-11-18 06:43:09 -07:00
|
|
|
"""It is invalid to start a thread off a thread."""
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "foo"},
|
|
|
|
parent_id=self.parent_id,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-18 06:43:09 -07:00
|
|
|
parent_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "foo"},
|
|
|
|
parent_id=parent_id,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(400, channel.code, channel.json_body)
|
2021-11-18 06:43:09 -07:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_basic_paginate_relations(self) -> None:
|
2020-07-09 07:52:58 -06:00
|
|
|
"""Tests that calling pagination API correctly the latest relations."""
|
2021-10-21 12:39:16 -06:00
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-11 09:20:27 -07:00
|
|
|
first_annotation_id = channel.json_body["event_id"]
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2021-10-21 12:39:16 -06:00
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-11 09:20:27 -07:00
|
|
|
second_annotation_id = channel.json_body["event_id"]
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-14 09:59:21 -06:00
|
|
|
"GET",
|
2022-02-11 09:20:27 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-11 09:20:27 -07:00
|
|
|
# We expect to get back a single pagination result, which is the latest
|
|
|
|
# full relation event we sent above.
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
self.assert_dict(
|
2022-02-11 09:20:27 -07:00
|
|
|
{
|
|
|
|
"event_id": second_annotation_id,
|
|
|
|
"sender": self.user_id,
|
|
|
|
"type": "m.reaction",
|
|
|
|
},
|
2019-05-14 09:59:21 -06:00
|
|
|
channel.json_body["chunk"][0],
|
|
|
|
)
|
|
|
|
|
2019-07-09 06:43:08 -06:00
|
|
|
# We also expect to get the original event (the id of which is self.parent_id)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2019-07-09 06:43:08 -06:00
|
|
|
channel.json_body["original_event"]["event_id"], self.parent_id
|
|
|
|
)
|
|
|
|
|
2019-05-14 09:59:21 -06:00
|
|
|
# Make sure next_batch has something in it that looks like it could be a
|
|
|
|
# valid token.
|
|
|
|
self.assertIsInstance(
|
2020-06-16 06:51:47 -06:00
|
|
|
channel.json_body.get("next_batch"), str, channel.json_body
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
|
|
|
|
2022-02-11 09:20:27 -07:00
|
|
|
# Request the relations again, but with a different direction.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations"
|
|
|
|
f"/{self.parent_id}?limit=1&org.matrix.msc3715.dir=f",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-11 09:20:27 -07:00
|
|
|
|
|
|
|
# We expect to get back a single pagination result, which is the earliest
|
|
|
|
# full relation event we sent above.
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
|
2022-02-11 09:20:27 -07:00
|
|
|
self.assert_dict(
|
|
|
|
{
|
|
|
|
"event_id": first_annotation_id,
|
|
|
|
"sender": self.user_id,
|
|
|
|
"type": "m.reaction",
|
|
|
|
},
|
|
|
|
channel.json_body["chunk"][0],
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_repeated_paginate_relations(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that if we paginate using a limit and tokens then we get the
|
|
|
|
expected events.
|
|
|
|
"""
|
|
|
|
|
|
|
|
expected_event_ids = []
|
2021-10-21 12:39:16 -06:00
|
|
|
for idx in range(10):
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", chr(ord("a") + idx)
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
expected_event_ids.append(channel.json_body["event_id"])
|
|
|
|
|
2022-02-10 08:52:48 -07:00
|
|
|
prev_token = ""
|
2021-10-14 07:19:35 -06:00
|
|
|
found_event_ids: List[str] = []
|
2019-05-14 09:59:21 -06:00
|
|
|
for _ in range(20):
|
|
|
|
from_token = ""
|
|
|
|
if prev_token:
|
|
|
|
from_token = "&from=" + prev_token
|
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-14 09:59:21 -06:00
|
|
|
"GET",
|
2022-02-10 08:52:48 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1{from_token}",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
found_event_ids.extend(e["event_id"] for e in channel.json_body["chunk"])
|
|
|
|
next_batch = channel.json_body.get("next_batch")
|
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertNotEqual(prev_token, next_batch)
|
2019-05-14 09:59:21 -06:00
|
|
|
prev_token = next_batch
|
|
|
|
|
|
|
|
if not prev_token:
|
|
|
|
break
|
|
|
|
|
|
|
|
# We paginated backwards, so reverse
|
|
|
|
found_event_ids.reverse()
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(found_event_ids, expected_event_ids)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_pagination_from_sync_and_messages(self) -> None:
|
2022-02-10 08:52:48 -07:00
|
|
|
"""Pagination tokens from /sync and /messages can be used to paginate /relations."""
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "A")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-10 08:52:48 -07:00
|
|
|
annotation_id = channel.json_body["event_id"]
|
|
|
|
# Send an event after the relation events.
|
|
|
|
self.helper.send(self.room, body="Latest event", tok=self.user_token)
|
|
|
|
|
|
|
|
# Request /sync, limiting it such that only the latest event is returned
|
|
|
|
# (and not the relation).
|
2022-03-02 04:56:16 -07:00
|
|
|
filter = urllib.parse.quote_plus(b'{"room": {"timeline": {"limit": 1}}}')
|
2022-02-10 08:52:48 -07:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET", f"/sync?filter={filter}", access_token=self.user_token
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-10 08:52:48 -07:00
|
|
|
room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
|
|
|
|
sync_prev_batch = room_timeline["prev_batch"]
|
|
|
|
self.assertIsNotNone(sync_prev_batch)
|
|
|
|
# Ensure the relation event is not in the batch returned from /sync.
|
|
|
|
self.assertNotIn(
|
|
|
|
annotation_id, [ev["event_id"] for ev in room_timeline["events"]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request /messages, limiting it such that only the latest event is
|
|
|
|
# returned (and not the relation).
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/messages?dir=b&limit=1",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-10 08:52:48 -07:00
|
|
|
messages_end = channel.json_body["end"]
|
|
|
|
self.assertIsNotNone(messages_end)
|
|
|
|
# Ensure the relation event is not in the chunk returned from /messages.
|
|
|
|
self.assertNotIn(
|
|
|
|
annotation_id, [ev["event_id"] for ev in channel.json_body["chunk"]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Request /relations with the pagination tokens received from both the
|
|
|
|
# /sync and /messages responses above, in turn.
|
|
|
|
#
|
|
|
|
# This is a tiny bit silly since the client wouldn't know the parent ID
|
|
|
|
# from the requests above; consider the parent ID to be known from a
|
|
|
|
# previous /sync.
|
|
|
|
for from_token in (sync_prev_batch, messages_end):
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?from={from_token}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-10 08:52:48 -07:00
|
|
|
|
|
|
|
# The relation should be in the returned chunk.
|
|
|
|
self.assertIn(
|
|
|
|
annotation_id, [ev["event_id"] for ev in channel.json_body["chunk"]]
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_aggregation_pagination_groups(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that we can paginate annotation groups correctly."""
|
|
|
|
|
2019-05-17 10:45:51 -06:00
|
|
|
# We need to create ten separate users to send each reaction.
|
|
|
|
access_tokens = [self.user_token, self.user2_token]
|
|
|
|
idx = 0
|
|
|
|
while len(access_tokens) < 10:
|
|
|
|
user_id, token = self._create_user("test" + str(idx))
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
self.helper.join(self.room, user=user_id, tok=token)
|
|
|
|
access_tokens.append(token)
|
|
|
|
|
|
|
|
idx = 0
|
2019-05-14 09:59:21 -06:00
|
|
|
sent_groups = {"👍": 10, "a": 7, "b": 5, "c": 3, "d": 2, "e": 1}
|
|
|
|
for key in itertools.chain.from_iterable(
|
|
|
|
itertools.repeat(key, num) for key, num in sent_groups.items()
|
|
|
|
):
|
|
|
|
channel = self._send_relation(
|
2019-05-17 10:45:51 -06:00
|
|
|
RelationTypes.ANNOTATION,
|
|
|
|
"m.reaction",
|
|
|
|
key=key,
|
|
|
|
access_token=access_tokens[idx],
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2019-05-17 10:45:51 -06:00
|
|
|
idx += 1
|
|
|
|
idx %= len(access_tokens)
|
|
|
|
|
2021-10-14 07:19:35 -06:00
|
|
|
prev_token: Optional[str] = None
|
|
|
|
found_groups: Dict[str, int] = {}
|
2019-05-14 09:59:21 -06:00
|
|
|
for _ in range(20):
|
|
|
|
from_token = ""
|
|
|
|
if prev_token:
|
|
|
|
from_token = "&from=" + prev_token
|
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-14 09:59:21 -06:00
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/aggregations/{self.parent_id}?limit=1{from_token}",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
|
|
|
|
|
|
|
|
for groups in channel.json_body["chunk"]:
|
|
|
|
# We only expect reactions
|
|
|
|
self.assertEqual(groups["type"], "m.reaction", channel.json_body)
|
|
|
|
|
|
|
|
# We should only see each key once
|
|
|
|
self.assertNotIn(groups["key"], found_groups, channel.json_body)
|
|
|
|
|
|
|
|
found_groups[groups["key"]] = groups["count"]
|
|
|
|
|
|
|
|
next_batch = channel.json_body.get("next_batch")
|
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertNotEqual(prev_token, next_batch)
|
2019-05-14 09:59:21 -06:00
|
|
|
prev_token = next_batch
|
|
|
|
|
|
|
|
if not prev_token:
|
|
|
|
break
|
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(sent_groups, found_groups)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_aggregation_pagination_within_group(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that we can paginate within an annotation group."""
|
|
|
|
|
2019-05-20 10:39:05 -06:00
|
|
|
# We need to create ten separate users to send each reaction.
|
|
|
|
access_tokens = [self.user_token, self.user2_token]
|
|
|
|
idx = 0
|
|
|
|
while len(access_tokens) < 10:
|
|
|
|
user_id, token = self._create_user("test" + str(idx))
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
self.helper.join(self.room, user=user_id, tok=token)
|
|
|
|
access_tokens.append(token)
|
|
|
|
|
|
|
|
idx = 0
|
2019-05-14 09:59:21 -06:00
|
|
|
expected_event_ids = []
|
|
|
|
for _ in range(10):
|
|
|
|
channel = self._send_relation(
|
2019-05-20 10:39:05 -06:00
|
|
|
RelationTypes.ANNOTATION,
|
|
|
|
"m.reaction",
|
|
|
|
key="👍",
|
|
|
|
access_token=access_tokens[idx],
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
expected_event_ids.append(channel.json_body["event_id"])
|
|
|
|
|
2019-05-20 10:39:05 -06:00
|
|
|
idx += 1
|
|
|
|
|
2019-05-14 09:59:21 -06:00
|
|
|
# Also send a different type of reaction so that we test we don't see it
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-10 08:52:48 -07:00
|
|
|
prev_token = ""
|
2021-10-14 07:19:35 -06:00
|
|
|
found_event_ids: List[str] = []
|
2021-07-13 04:43:15 -06:00
|
|
|
encoded_key = urllib.parse.quote_plus("👍".encode())
|
2019-05-14 09:59:21 -06:00
|
|
|
for _ in range(20):
|
|
|
|
from_token = ""
|
|
|
|
if prev_token:
|
|
|
|
from_token = "&from=" + prev_token
|
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-14 09:59:21 -06:00
|
|
|
"GET",
|
2022-02-10 08:52:48 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}"
|
|
|
|
f"/aggregations/{self.parent_id}/{RelationTypes.ANNOTATION}"
|
|
|
|
f"/m.reaction/{encoded_key}?limit=1{from_token}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-10 08:52:48 -07:00
|
|
|
|
|
|
|
self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
|
|
|
|
|
|
|
|
found_event_ids.extend(e["event_id"] for e in channel.json_body["chunk"])
|
|
|
|
|
|
|
|
next_batch = channel.json_body.get("next_batch")
|
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertNotEqual(prev_token, next_batch)
|
2022-02-10 08:52:48 -07:00
|
|
|
prev_token = next_batch
|
|
|
|
|
|
|
|
if not prev_token:
|
|
|
|
break
|
|
|
|
|
|
|
|
# We paginated backwards, so reverse
|
|
|
|
found_event_ids.reverse()
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(found_event_ids, expected_event_ids)
|
2022-02-10 08:52:48 -07:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_aggregation(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that annotations get correctly aggregated."""
|
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2019-05-17 10:45:51 -06:00
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", "a", access_token=self.user2_token
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-14 09:59:21 -06:00
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/aggregations/{self.parent_id}",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2019-05-14 09:59:21 -06:00
|
|
|
channel.json_body,
|
|
|
|
{
|
|
|
|
"chunk": [
|
|
|
|
{"type": "m.reaction", "key": "a", "count": 2},
|
|
|
|
{"type": "m.reaction", "key": "b", "count": 1},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_aggregation_must_be_annotation(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that aggregations must be annotations."""
|
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-14 09:59:21 -06:00
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/aggregations"
|
|
|
|
f"/{self.parent_id}/{RelationTypes.REPLACE}?limit=1",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(400, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-08 07:21:20 -07:00
|
|
|
@unittest.override_config(
|
|
|
|
{"experimental_features": {"msc3440_enabled": True, "msc3666_enabled": True}}
|
|
|
|
)
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_bundled_aggregations(self) -> None:
|
2022-01-21 03:31:31 -07:00
|
|
|
"""
|
|
|
|
Test that annotations, references, and threads get correctly bundled.
|
|
|
|
|
|
|
|
Note that this doesn't test against /relations since only thread relations
|
|
|
|
get bundled via that API. See test_aggregation_get_event_for_thread.
|
|
|
|
|
|
|
|
See test_edit for a similar test for edits.
|
|
|
|
"""
|
2021-12-06 08:51:15 -07:00
|
|
|
# Setup by sending a variety of relations.
|
2019-05-14 09:59:21 -06:00
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2019-05-17 10:45:51 -06:00
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", "a", access_token=self.user2_token
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2019-05-20 07:31:19 -06:00
|
|
|
channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
reply_1 = channel.json_body["event_id"]
|
|
|
|
|
2019-05-20 07:31:19 -06:00
|
|
|
channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
reply_2 = channel.json_body["event_id"]
|
|
|
|
|
2021-10-21 12:39:16 -06:00
|
|
|
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-10-21 12:39:16 -06:00
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-10-21 12:39:16 -06:00
|
|
|
thread_2 = channel.json_body["event_id"]
|
|
|
|
|
2022-01-21 03:31:31 -07:00
|
|
|
def assert_bundle(event_json: JsonDict) -> None:
|
2021-12-06 08:51:15 -07:00
|
|
|
"""Assert the expected values of the bundled aggregations."""
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict = event_json["unsigned"].get("m.relations")
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2021-12-06 08:51:15 -07:00
|
|
|
# Ensure the fields are as expected.
|
|
|
|
self.assertCountEqual(
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict.keys(),
|
2021-12-06 08:51:15 -07:00
|
|
|
(
|
|
|
|
RelationTypes.ANNOTATION,
|
|
|
|
RelationTypes.REFERENCE,
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check the values of each field.
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2021-12-06 08:51:15 -07:00
|
|
|
{
|
2019-05-14 09:59:21 -06:00
|
|
|
"chunk": [
|
|
|
|
{"type": "m.reaction", "key": "a", "count": 2},
|
|
|
|
{"type": "m.reaction", "key": "b", "count": 1},
|
|
|
|
]
|
|
|
|
},
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict[RelationTypes.ANNOTATION],
|
2021-12-06 08:51:15 -07:00
|
|
|
)
|
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2021-12-06 08:51:15 -07:00
|
|
|
{"chunk": [{"event_id": reply_1}, {"event_id": reply_2}]},
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict[RelationTypes.REFERENCE],
|
2021-12-06 08:51:15 -07:00
|
|
|
)
|
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2021-12-06 08:51:15 -07:00
|
|
|
2,
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict[RelationTypes.THREAD].get("count"),
|
2021-12-06 08:51:15 -07:00
|
|
|
)
|
2022-01-18 09:38:57 -07:00
|
|
|
self.assertTrue(
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict[RelationTypes.THREAD].get("current_user_participated")
|
2022-01-18 09:38:57 -07:00
|
|
|
)
|
2021-12-06 08:51:15 -07:00
|
|
|
# The latest thread event has some fields that don't matter.
|
|
|
|
self.assert_dict(
|
|
|
|
{
|
|
|
|
"content": {
|
|
|
|
"m.relates_to": {
|
|
|
|
"event_id": self.parent_id,
|
|
|
|
"rel_type": RelationTypes.THREAD,
|
|
|
|
}
|
2021-10-21 12:39:16 -06:00
|
|
|
},
|
2021-12-06 08:51:15 -07:00
|
|
|
"event_id": thread_2,
|
|
|
|
"sender": self.user_id,
|
|
|
|
"type": "m.room.test",
|
2021-10-21 12:39:16 -06:00
|
|
|
},
|
2022-01-21 03:31:31 -07:00
|
|
|
relations_dict[RelationTypes.THREAD].get("latest_event"),
|
2021-12-06 08:51:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
# Request the event directly.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/event/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
assert_bundle(channel.json_body)
|
2021-12-06 08:51:15 -07:00
|
|
|
|
|
|
|
# Request the room messages.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/messages?dir=b",
|
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
assert_bundle(self._find_event_in_chunk(channel.json_body["chunk"]))
|
2021-12-06 08:51:15 -07:00
|
|
|
|
|
|
|
# Request the room context.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/context/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
assert_bundle(channel.json_body["event"])
|
2021-12-06 08:51:15 -07:00
|
|
|
|
|
|
|
# Request sync.
|
2022-01-13 08:45:28 -07:00
|
|
|
channel = self.make_request("GET", "/sync", access_token=self.user_token)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-13 08:45:28 -07:00
|
|
|
room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
|
|
|
|
self.assertTrue(room_timeline["limited"])
|
2022-01-26 06:27:04 -07:00
|
|
|
assert_bundle(self._find_event_in_chunk(room_timeline["events"]))
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-08 07:21:20 -07:00
|
|
|
# Request search.
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/search",
|
|
|
|
# Search term matches the parent message.
|
|
|
|
content={"search_categories": {"room_events": {"search_term": "Hi"}}},
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-08 07:21:20 -07:00
|
|
|
chunk = [
|
|
|
|
result["result"]
|
|
|
|
for result in channel.json_body["search_categories"]["room_events"][
|
|
|
|
"results"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
assert_bundle(self._find_event_in_chunk(chunk))
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_aggregation_get_event_for_annotation(self) -> None:
|
2021-12-06 08:51:15 -07:00
|
|
|
"""Test that annotations do not get bundled aggregations included
|
2021-11-30 09:33:33 -07:00
|
|
|
when directly requested.
|
|
|
|
"""
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
annotation_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
# Annotate the annotation.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=annotation_id
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/event/{annotation_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
self.assertIsNone(channel.json_body["unsigned"].get("m.relations"))
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_aggregation_get_event_for_thread(self) -> None:
|
2021-12-06 08:51:15 -07:00
|
|
|
"""Test that threads get bundled aggregations included when directly requested."""
|
2021-11-30 09:33:33 -07:00
|
|
|
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
thread_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
# Annotate the annotation.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=thread_id
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/event/{thread_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
self.assertEqual(
|
2021-11-30 09:33:33 -07:00
|
|
|
channel.json_body["unsigned"].get("m.relations"),
|
|
|
|
{
|
|
|
|
RelationTypes.ANNOTATION: {
|
|
|
|
"chunk": [{"count": 1, "key": "a", "type": "m.reaction"}]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
# It should also be included when the entire thread is requested.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
self.assertEqual(len(channel.json_body["chunk"]), 1)
|
|
|
|
|
|
|
|
thread_message = channel.json_body["chunk"][0]
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2021-11-30 09:33:33 -07:00
|
|
|
thread_message["unsigned"].get("m.relations"),
|
|
|
|
{
|
|
|
|
RelationTypes.ANNOTATION: {
|
|
|
|
"chunk": [{"count": 1, "key": "a", "type": "m.reaction"}]
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-12-09 11:16:01 -07:00
|
|
|
@unittest.override_config({"experimental_features": {"msc3440_enabled": True}})
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_ignore_invalid_room(self) -> None:
|
2021-12-09 11:16:01 -07:00
|
|
|
"""Test that we ignore invalid relations over federation."""
|
|
|
|
# Create another room and send a message in it.
|
|
|
|
room2 = self.helper.create_room_as(self.user_id, tok=self.user_token)
|
|
|
|
res = self.helper.send(room2, body="Hi!", tok=self.user_token)
|
|
|
|
parent_id = res["event_id"]
|
|
|
|
|
|
|
|
# Disable the validation to pretend this came over federation.
|
|
|
|
with patch(
|
|
|
|
"synapse.handlers.message.EventCreationHandler._validate_event_relation",
|
|
|
|
new=lambda self, event: make_awaitable(None),
|
|
|
|
):
|
|
|
|
# Generate a various relations from a different room.
|
|
|
|
self.get_success(
|
|
|
|
inject_event(
|
|
|
|
self.hs,
|
|
|
|
room_id=self.room,
|
|
|
|
type="m.reaction",
|
|
|
|
sender=self.user_id,
|
|
|
|
content={
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationTypes.ANNOTATION,
|
|
|
|
"event_id": parent_id,
|
|
|
|
"key": "A",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
inject_event(
|
|
|
|
self.hs,
|
|
|
|
room_id=self.room,
|
|
|
|
type="m.room.message",
|
|
|
|
sender=self.user_id,
|
|
|
|
content={
|
|
|
|
"body": "foo",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationTypes.REFERENCE,
|
|
|
|
"event_id": parent_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
inject_event(
|
|
|
|
self.hs,
|
|
|
|
room_id=self.room,
|
|
|
|
type="m.room.message",
|
|
|
|
sender=self.user_id,
|
|
|
|
content={
|
|
|
|
"body": "foo",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationTypes.THREAD,
|
|
|
|
"event_id": parent_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
inject_event(
|
|
|
|
self.hs,
|
|
|
|
room_id=self.room,
|
|
|
|
type="m.room.message",
|
|
|
|
sender=self.user_id,
|
|
|
|
content={
|
|
|
|
"body": "foo",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"new_content": {
|
|
|
|
"body": "new content",
|
|
|
|
"msgtype": "m.text",
|
|
|
|
},
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": RelationTypes.REPLACE,
|
|
|
|
"event_id": parent_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# They should be ignored when fetching relations.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{room2}/relations/{parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-12-09 11:16:01 -07:00
|
|
|
self.assertEqual(channel.json_body["chunk"], [])
|
|
|
|
|
|
|
|
# And when fetching aggregations.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{room2}/aggregations/{parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-12-09 11:16:01 -07:00
|
|
|
self.assertEqual(channel.json_body["chunk"], [])
|
|
|
|
|
|
|
|
# And for bundled aggregations.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{room2}/event/{parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-12-09 11:16:01 -07:00
|
|
|
self.assertNotIn("m.relations", channel.json_body["unsigned"])
|
|
|
|
|
2022-02-08 07:21:20 -07:00
|
|
|
@unittest.override_config({"experimental_features": {"msc3666_enabled": True}})
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_edit(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that a simple edit works."""
|
|
|
|
|
|
|
|
new_body = {"msgtype": "m.text", "body": "I've been edited!"}
|
|
|
|
channel = self._send_relation(
|
2019-05-20 07:31:19 -06:00
|
|
|
RelationTypes.REPLACE,
|
2019-05-14 09:59:21 -06:00
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
edit_event_id = channel.json_body["event_id"]
|
|
|
|
|
2022-01-21 03:31:31 -07:00
|
|
|
def assert_bundle(event_json: JsonDict) -> None:
|
|
|
|
"""Assert the expected values of the bundled aggregations."""
|
|
|
|
relations_dict = event_json["unsigned"].get("m.relations")
|
|
|
|
self.assertIn(RelationTypes.REPLACE, relations_dict)
|
|
|
|
|
|
|
|
m_replace_dict = relations_dict[RelationTypes.REPLACE]
|
|
|
|
for key in ["event_id", "sender", "origin_server_ts"]:
|
|
|
|
self.assertIn(key, m_replace_dict)
|
|
|
|
|
|
|
|
self.assert_dict(
|
|
|
|
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
|
|
|
|
)
|
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-17 10:45:51 -06:00
|
|
|
"GET",
|
2022-01-21 03:31:31 -07:00
|
|
|
f"/rooms/{self.room}/event/{self.parent_id}",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
self.assertEqual(channel.json_body["content"], new_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
assert_bundle(channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-01-21 03:31:31 -07:00
|
|
|
# Request the room messages.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/messages?dir=b",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
assert_bundle(self._find_event_in_chunk(channel.json_body["chunk"]))
|
2019-07-05 10:20:02 -06:00
|
|
|
|
2022-01-21 03:31:31 -07:00
|
|
|
# Request the room context.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/context/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
assert_bundle(channel.json_body["event"])
|
2019-07-05 10:20:02 -06:00
|
|
|
|
2022-01-21 03:31:31 -07:00
|
|
|
# Request sync, but limit the timeline so it becomes limited (and includes
|
|
|
|
# bundled aggregations).
|
2022-03-02 04:56:16 -07:00
|
|
|
filter = urllib.parse.quote_plus(b'{"room": {"timeline": {"limit": 2}}}')
|
2022-01-21 03:31:31 -07:00
|
|
|
channel = self.make_request(
|
|
|
|
"GET", f"/sync?filter={filter}", access_token=self.user_token
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-01-21 03:31:31 -07:00
|
|
|
room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
|
|
|
|
self.assertTrue(room_timeline["limited"])
|
|
|
|
assert_bundle(self._find_event_in_chunk(room_timeline["events"]))
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-08 07:21:20 -07:00
|
|
|
# Request search.
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
"/search",
|
|
|
|
# Search term matches the parent message.
|
|
|
|
content={"search_categories": {"room_events": {"search_term": "Hi"}}},
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-08 07:21:20 -07:00
|
|
|
chunk = [
|
|
|
|
result["result"]
|
|
|
|
for result in channel.json_body["search_categories"]["room_events"][
|
|
|
|
"results"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
assert_bundle(self._find_event_in_chunk(chunk))
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_multi_edit(self) -> None:
|
2019-05-14 09:59:21 -06:00
|
|
|
"""Test that multiple edits, including attempts by people who
|
|
|
|
shouldn't be allowed, are correctly handled.
|
|
|
|
"""
|
|
|
|
|
|
|
|
channel = self._send_relation(
|
2019-05-20 07:31:19 -06:00
|
|
|
RelationTypes.REPLACE,
|
2019-05-14 09:59:21 -06:00
|
|
|
"m.room.message",
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "Wibble",
|
|
|
|
"m.new_content": {"msgtype": "m.text", "body": "First edit"},
|
|
|
|
},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
new_body = {"msgtype": "m.text", "body": "I've been edited!"}
|
|
|
|
channel = self._send_relation(
|
2019-05-20 07:31:19 -06:00
|
|
|
RelationTypes.REPLACE,
|
2019-05-14 09:59:21 -06:00
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
|
|
|
edit_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self._send_relation(
|
2019-05-20 07:31:19 -06:00
|
|
|
RelationTypes.REPLACE,
|
2019-05-14 09:59:21 -06:00
|
|
|
"m.room.message.WRONG_TYPE",
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "Wibble",
|
|
|
|
"m.new_content": {"msgtype": "m.text", "body": "Edit, but wrong type"},
|
|
|
|
},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2020-12-15 07:44:04 -07:00
|
|
|
channel = self.make_request(
|
2019-05-17 10:45:51 -06:00
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/rooms/{self.room}/event/{self.parent_id}",
|
2019-05-17 10:45:51 -06:00
|
|
|
access_token=self.user_token,
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(channel.json_body["content"], new_body)
|
2019-05-14 09:59:21 -06:00
|
|
|
|
2019-07-05 10:20:02 -06:00
|
|
|
relations_dict = channel.json_body["unsigned"].get("m.relations")
|
|
|
|
self.assertIn(RelationTypes.REPLACE, relations_dict)
|
|
|
|
|
|
|
|
m_replace_dict = relations_dict[RelationTypes.REPLACE]
|
|
|
|
for key in ["event_id", "sender", "origin_server_ts"]:
|
|
|
|
self.assertIn(key, m_replace_dict)
|
|
|
|
|
|
|
|
self.assert_dict(
|
|
|
|
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
|
2019-05-14 09:59:21 -06:00
|
|
|
)
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_edit_reply(self) -> None:
|
2021-03-17 10:51:55 -06:00
|
|
|
"""Test that editing a reply works."""
|
|
|
|
|
|
|
|
# Create a reply to edit.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.REFERENCE,
|
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "A reply!"},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-03-17 10:51:55 -06:00
|
|
|
reply = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
new_body = {"msgtype": "m.text", "body": "I've been edited!"}
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.REPLACE,
|
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
|
|
|
|
parent_id=reply,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-03-17 10:51:55 -06:00
|
|
|
|
|
|
|
edit_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/rooms/{self.room}/event/{reply}",
|
2021-03-17 10:51:55 -06:00
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-03-17 10:51:55 -06:00
|
|
|
|
|
|
|
# We expect to see the new body in the dict, as well as the reference
|
|
|
|
# metadata sill intact.
|
|
|
|
self.assertDictContainsSubset(new_body, channel.json_body["content"])
|
|
|
|
self.assertDictContainsSubset(
|
|
|
|
{
|
|
|
|
"m.relates_to": {
|
|
|
|
"event_id": self.parent_id,
|
|
|
|
"rel_type": "m.reference",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
channel.json_body["content"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# We expect that the edit relation appears in the unsigned relations
|
|
|
|
# section.
|
|
|
|
relations_dict = channel.json_body["unsigned"].get("m.relations")
|
|
|
|
self.assertIn(RelationTypes.REPLACE, relations_dict)
|
|
|
|
|
|
|
|
m_replace_dict = relations_dict[RelationTypes.REPLACE]
|
|
|
|
for key in ["event_id", "sender", "origin_server_ts"]:
|
|
|
|
self.assertIn(key, m_replace_dict)
|
|
|
|
|
|
|
|
self.assert_dict(
|
|
|
|
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
|
|
|
|
)
|
|
|
|
|
2022-02-15 06:26:57 -07:00
|
|
|
@unittest.override_config({"experimental_features": {"msc3440_enabled": True}})
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_edit_thread(self) -> None:
|
2022-02-15 06:26:57 -07:00
|
|
|
"""Test that editing a thread works."""
|
|
|
|
|
|
|
|
# Create a thread and edit the last event.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "A threaded reply!"},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-15 06:26:57 -07:00
|
|
|
threaded_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
new_body = {"msgtype": "m.text", "body": "I've been edited!"}
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.REPLACE,
|
|
|
|
"m.room.message",
|
|
|
|
content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
|
|
|
|
parent_id=threaded_event_id,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-15 06:26:57 -07:00
|
|
|
|
|
|
|
# Fetch the thread root, to get the bundled aggregation for the thread.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/rooms/{self.room}/event/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-02-15 06:26:57 -07:00
|
|
|
|
|
|
|
# We expect that the edit message appears in the thread summary in the
|
|
|
|
# unsigned relations section.
|
|
|
|
relations_dict = channel.json_body["unsigned"].get("m.relations")
|
|
|
|
self.assertIn(RelationTypes.THREAD, relations_dict)
|
|
|
|
|
|
|
|
thread_summary = relations_dict[RelationTypes.THREAD]
|
|
|
|
self.assertIn("latest_event", thread_summary)
|
|
|
|
latest_event_in_thread = thread_summary["latest_event"]
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(latest_event_in_thread["content"]["body"], "I've been edited!")
|
2022-02-15 06:26:57 -07:00
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_edit_edit(self) -> None:
|
2021-11-30 09:33:33 -07:00
|
|
|
"""Test that an edit cannot be edited."""
|
|
|
|
new_body = {"msgtype": "m.text", "body": "Initial edit"}
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.REPLACE,
|
|
|
|
"m.room.message",
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "Wibble",
|
|
|
|
"m.new_content": new_body,
|
|
|
|
},
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
edit_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
# Edit the edit event.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.REPLACE,
|
|
|
|
"m.room.message",
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "foo",
|
|
|
|
"m.new_content": {"msgtype": "m.text", "body": "Ignored edit"},
|
|
|
|
},
|
|
|
|
parent_id=edit_event_id,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
|
|
|
|
# Request the original event.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/rooms/{self.room}/event/{self.parent_id}",
|
2021-11-30 09:33:33 -07:00
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
# The edit to the edit should be ignored.
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(channel.json_body["content"], new_body)
|
2021-11-30 09:33:33 -07:00
|
|
|
|
|
|
|
# The relations information should not include the edit to the edit.
|
|
|
|
relations_dict = channel.json_body["unsigned"].get("m.relations")
|
|
|
|
self.assertIn(RelationTypes.REPLACE, relations_dict)
|
|
|
|
|
|
|
|
m_replace_dict = relations_dict[RelationTypes.REPLACE]
|
|
|
|
for key in ["event_id", "sender", "origin_server_ts"]:
|
|
|
|
self.assertIn(key, m_replace_dict)
|
|
|
|
|
|
|
|
self.assert_dict(
|
|
|
|
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_unknown_relations(self) -> None:
|
2021-11-22 10:01:47 -07:00
|
|
|
"""Unknown relations should be accepted."""
|
|
|
|
channel = self._send_relation("m.relation.test", "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1",
|
2021-11-22 10:01:47 -07:00
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
|
|
|
|
# We expect to get back a single pagination result, which is the full
|
|
|
|
# relation event we sent above.
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
self.assert_dict(
|
|
|
|
{"event_id": event_id, "sender": self.user_id, "type": "m.room.test"},
|
|
|
|
channel.json_body["chunk"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
# We also expect to get the original event (the id of which is self.parent_id)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(
|
2021-11-22 10:01:47 -07:00
|
|
|
channel.json_body["original_event"]["event_id"], self.parent_id
|
|
|
|
)
|
|
|
|
|
|
|
|
# When bundling the unknown relation is not included.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/rooms/{self.room}/event/{self.parent_id}",
|
2021-11-22 10:01:47 -07:00
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
self.assertNotIn("m.relations", channel.json_body["unsigned"])
|
|
|
|
|
|
|
|
# But unknown relations can be directly queried.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
2022-03-02 04:56:16 -07:00
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/aggregations/{self.parent_id}?limit=1",
|
2021-11-22 10:01:47 -07:00
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
self.assertEqual(channel.json_body["chunk"], [])
|
2021-11-22 10:01:47 -07:00
|
|
|
|
2022-01-21 03:31:31 -07:00
|
|
|
def _find_event_in_chunk(self, events: List[JsonDict]) -> JsonDict:
|
|
|
|
"""
|
|
|
|
Find the parent event in a chunk of events and assert that it has the proper bundled aggregations.
|
|
|
|
"""
|
|
|
|
for event in events:
|
|
|
|
if event["event_id"] == self.parent_id:
|
|
|
|
return event
|
|
|
|
|
|
|
|
raise AssertionError(f"Event {self.parent_id} not found in chunk")
|
|
|
|
|
2022-02-28 10:47:37 -07:00
|
|
|
def test_background_update(self) -> None:
|
2021-11-22 10:01:47 -07:00
|
|
|
"""Test the event_arbitrary_relations background update."""
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
annotation_event_id_good = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="A")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
annotation_event_id_bad = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
thread_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
# Clean-up the table as if the inserts did not happen during event creation.
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_delete_many(
|
|
|
|
table="event_relations",
|
|
|
|
column="event_id",
|
|
|
|
iterable=(annotation_event_id_bad, thread_event_id),
|
|
|
|
keyvalues={},
|
|
|
|
desc="RelationsTestCase.test_background_update",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Only the "good" annotation should be found.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=10",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
self.assertEqual(
|
2021-11-22 10:01:47 -07:00
|
|
|
[ev["event_id"] for ev in channel.json_body["chunk"]],
|
|
|
|
[annotation_event_id_good],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Insert and run the background update.
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_insert(
|
|
|
|
"background_updates",
|
|
|
|
{"update_name": "event_arbitrary_relations", "progress_json": "{}"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ugh, have to reset this flag
|
|
|
|
self.store.db_pool.updates._all_done = False
|
|
|
|
self.wait_for_background_updates()
|
|
|
|
|
|
|
|
# The "good" annotation and the thread should be found, but not the "bad"
|
|
|
|
# annotation.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=10",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
2022-02-28 05:12:29 -07:00
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2021-11-22 10:01:47 -07:00
|
|
|
self.assertCountEqual(
|
|
|
|
[ev["event_id"] for ev in channel.json_body["chunk"]],
|
|
|
|
[annotation_event_id_good, thread_event_id],
|
|
|
|
)
|
2022-03-02 04:56:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
class RelationRedactionTestCase(BaseRelationsTestCase):
|
2022-03-07 07:00:05 -07:00
|
|
|
"""
|
|
|
|
Test the behaviour of relations when the parent or child event is redacted.
|
|
|
|
|
|
|
|
The behaviour of each relation type is subtly different which causes the tests
|
|
|
|
to be a bit repetitive, they follow a naming scheme of:
|
|
|
|
|
|
|
|
test_redact_(relation|parent)_{relation_type}
|
|
|
|
|
|
|
|
The first bit of "relation" means that the event with the relation defined
|
|
|
|
on it (the child event) is to be redacted. A "parent" means that the target
|
|
|
|
of the relation (the parent event) is to be redacted.
|
|
|
|
|
|
|
|
The relation_type describes which type of relation is under test (i.e. it is
|
|
|
|
related to the value of rel_type in the event content).
|
|
|
|
"""
|
2022-03-02 04:56:16 -07:00
|
|
|
|
|
|
|
def _redact(self, event_id: str) -> None:
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
f"/_matrix/client/r0/rooms/{self.room}/redact/{event_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
content={},
|
|
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
def _make_relation_requests(self) -> Tuple[List[str], JsonDict]:
|
|
|
|
"""
|
|
|
|
Makes requests and ensures they result in a 200 response, returns a
|
|
|
|
tuple of results:
|
|
|
|
|
|
|
|
1. `/relations` -> Returns a list of event IDs.
|
|
|
|
2. `/event` -> Returns the response's m.relations field (from unsigned),
|
|
|
|
if it exists.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Request the relations of the event.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.json_body)
|
|
|
|
event_ids = [ev["event_id"] for ev in channel.json_body["chunk"]]
|
|
|
|
|
|
|
|
# Fetch the bundled aggregations of the event.
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/event/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.json_body)
|
|
|
|
bundled_relations = channel.json_body["unsigned"].get("m.relations", {})
|
|
|
|
|
|
|
|
return event_ids, bundled_relations
|
|
|
|
|
|
|
|
def _get_aggregations(self) -> List[JsonDict]:
|
|
|
|
"""Request /aggregations on the parent ID and includes the returned chunk."""
|
|
|
|
channel = self.make_request(
|
|
|
|
"GET",
|
|
|
|
f"/_matrix/client/unstable/rooms/{self.room}/aggregations/{self.parent_id}",
|
|
|
|
access_token=self.user_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
return channel.json_body["chunk"]
|
|
|
|
|
2022-03-02 04:56:16 -07:00
|
|
|
def test_redact_relation_annotation(self) -> None:
|
2022-03-07 07:00:05 -07:00
|
|
|
"""
|
|
|
|
Test that annotations of an event are properly handled after the
|
2022-03-02 04:56:16 -07:00
|
|
|
annotation is redacted.
|
2022-03-07 07:00:05 -07:00
|
|
|
|
|
|
|
The redacted relation should not be included in bundled aggregations or
|
|
|
|
the response to relations.
|
2022-03-02 04:56:16 -07:00
|
|
|
"""
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
to_redact_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.ANNOTATION, "m.reaction", "a", access_token=self.user2_token
|
|
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-03-07 07:00:05 -07:00
|
|
|
unredacted_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
# Both relations should exist.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertCountEqual(event_ids, [to_redact_event_id, unredacted_event_id])
|
|
|
|
self.assertEquals(
|
|
|
|
relations["m.annotation"],
|
|
|
|
{"chunk": [{"type": "m.reaction", "key": "a", "count": 2}]},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Both relations appear in the aggregation.
|
|
|
|
chunk = self._get_aggregations()
|
|
|
|
self.assertEqual(chunk, [{"type": "m.reaction", "key": "a", "count": 2}])
|
2022-03-02 04:56:16 -07:00
|
|
|
|
|
|
|
# Redact one of the reactions.
|
|
|
|
self._redact(to_redact_event_id)
|
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# The unredacted relation should still exist.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEquals(event_ids, [unredacted_event_id])
|
|
|
|
self.assertEquals(
|
|
|
|
relations["m.annotation"],
|
|
|
|
{"chunk": [{"type": "m.reaction", "key": "a", "count": 1}]},
|
|
|
|
)
|
|
|
|
|
|
|
|
# The unredacted aggregation should still exist.
|
|
|
|
chunk = self._get_aggregations()
|
|
|
|
self.assertEqual(chunk, [{"type": "m.reaction", "key": "a", "count": 1}])
|
|
|
|
|
|
|
|
@unittest.override_config({"experimental_features": {"msc3440_enabled": True}})
|
|
|
|
def test_redact_relation_thread(self) -> None:
|
|
|
|
"""
|
|
|
|
Test that thread replies are properly handled after the thread reply redacted.
|
|
|
|
|
|
|
|
The redacted event should not be included in bundled aggregations or
|
|
|
|
the response to relations.
|
|
|
|
"""
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
EventTypes.Message,
|
|
|
|
content={"body": "reply 1", "msgtype": "m.text"},
|
2022-03-02 04:56:16 -07:00
|
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
2022-03-07 07:00:05 -07:00
|
|
|
unredacted_event_id = channel.json_body["event_id"]
|
2022-03-02 04:56:16 -07:00
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# Note that the *last* event in the thread is redacted, as that gets
|
|
|
|
# included in the bundled aggregation.
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.THREAD,
|
|
|
|
EventTypes.Message,
|
|
|
|
content={"body": "reply 2", "msgtype": "m.text"},
|
|
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
to_redact_event_id = channel.json_body["event_id"]
|
|
|
|
|
|
|
|
# Both relations exist.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEquals(event_ids, [to_redact_event_id, unredacted_event_id])
|
|
|
|
self.assertDictContainsSubset(
|
|
|
|
{
|
|
|
|
"count": 2,
|
|
|
|
"current_user_participated": True,
|
|
|
|
},
|
|
|
|
relations[RelationTypes.THREAD],
|
|
|
|
)
|
|
|
|
# And the latest event returned is the event that will be redacted.
|
2022-03-02 04:56:16 -07:00
|
|
|
self.assertEqual(
|
2022-03-07 07:00:05 -07:00
|
|
|
relations[RelationTypes.THREAD]["latest_event"]["event_id"],
|
|
|
|
to_redact_event_id,
|
2022-03-02 04:56:16 -07:00
|
|
|
)
|
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# Redact one of the reactions.
|
|
|
|
self._redact(to_redact_event_id)
|
|
|
|
|
|
|
|
# The unredacted relation should still exist.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEquals(event_ids, [unredacted_event_id])
|
|
|
|
self.assertDictContainsSubset(
|
|
|
|
{
|
|
|
|
"count": 1,
|
|
|
|
"current_user_participated": True,
|
|
|
|
},
|
|
|
|
relations[RelationTypes.THREAD],
|
|
|
|
)
|
|
|
|
# And the latest event is now the unredacted event.
|
|
|
|
self.assertEqual(
|
|
|
|
relations[RelationTypes.THREAD]["latest_event"]["event_id"],
|
|
|
|
unredacted_event_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_redact_parent_edit(self) -> None:
|
2022-03-02 04:56:16 -07:00
|
|
|
"""Test that edits of an event are redacted when the original event
|
|
|
|
is redacted.
|
|
|
|
"""
|
|
|
|
# Add a relation
|
|
|
|
channel = self._send_relation(
|
|
|
|
RelationTypes.REPLACE,
|
|
|
|
"m.room.message",
|
|
|
|
parent_id=self.parent_id,
|
|
|
|
content={
|
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "Wibble",
|
|
|
|
"m.new_content": {"msgtype": "m.text", "body": "First edit"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
|
|
|
|
# Check the relation is returned
|
2022-03-07 07:00:05 -07:00
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEqual(len(event_ids), 1)
|
|
|
|
self.assertIn(RelationTypes.REPLACE, relations)
|
2022-03-02 04:56:16 -07:00
|
|
|
|
|
|
|
# Redact the original event
|
|
|
|
self._redact(self.parent_id)
|
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# The relations are not returned.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEqual(len(event_ids), 0)
|
|
|
|
self.assertEqual(relations, {})
|
2022-03-02 04:56:16 -07:00
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
def test_redact_parent_annotation(self) -> None:
|
2022-03-02 04:56:16 -07:00
|
|
|
"""Test that annotations of an event are redacted when the original event
|
|
|
|
is redacted.
|
|
|
|
"""
|
|
|
|
# Add a relation
|
|
|
|
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
|
|
|
|
self.assertEqual(200, channel.code, channel.json_body)
|
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# The relations should exist.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEqual(len(event_ids), 1)
|
|
|
|
self.assertIn(RelationTypes.ANNOTATION, relations)
|
|
|
|
|
|
|
|
# The aggregation should exist.
|
|
|
|
chunk = self._get_aggregations()
|
|
|
|
self.assertEqual(chunk, [{"type": "m.reaction", "key": "👍", "count": 1}])
|
|
|
|
|
2022-03-02 04:56:16 -07:00
|
|
|
# Redact the original event.
|
|
|
|
self._redact(self.parent_id)
|
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# The relations are not returned.
|
|
|
|
event_ids, relations = self._make_relation_requests()
|
|
|
|
self.assertEqual(event_ids, [])
|
|
|
|
self.assertEqual(relations, {})
|
2022-03-02 04:56:16 -07:00
|
|
|
|
2022-03-07 07:00:05 -07:00
|
|
|
# There's nothing to aggregate.
|
|
|
|
chunk = self._get_aggregations()
|
|
|
|
self.assertEqual(chunk, [])
|