You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1949 lines
74 KiB

  1. # Copyright 2019 New Vector Ltd
  2. # Copyright 2021 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import urllib.parse
  16. from typing import Any, Callable, Dict, List, Optional, Tuple
  17. from unittest.mock import patch
  18. from twisted.test.proto_helpers import MemoryReactor
  19. from synapse.api.constants import AccountDataTypes, EventTypes, RelationTypes
  20. from synapse.rest import admin
  21. from synapse.rest.client import login, register, relations, room, sync
  22. from synapse.server import HomeServer
  23. from synapse.types import JsonDict
  24. from synapse.util import Clock
  25. from tests import unittest
  26. from tests.server import FakeChannel
  27. from tests.test_utils import make_awaitable
  28. from tests.test_utils.event_injection import inject_event
  29. from tests.unittest import override_config
  30. class BaseRelationsTestCase(unittest.HomeserverTestCase):
  31. servlets = [
  32. relations.register_servlets,
  33. room.register_servlets,
  34. sync.register_servlets,
  35. login.register_servlets,
  36. register.register_servlets,
  37. admin.register_servlets_for_client_rest_resource,
  38. ]
  39. hijack_auth = False
  40. def default_config(self) -> Dict[str, Any]:
  41. # We need to enable msc1849 support for aggregations
  42. config = super().default_config()
  43. # We enable frozen dicts as relations/edits change event contents, so we
  44. # want to test that we don't modify the events in the caches.
  45. config["use_frozen_dicts"] = True
  46. return config
  47. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  48. self.store = hs.get_datastores().main
  49. self.user_id, self.user_token = self._create_user("alice")
  50. self.user2_id, self.user2_token = self._create_user("bob")
  51. self.room = self.helper.create_room_as(self.user_id, tok=self.user_token)
  52. self.helper.join(self.room, user=self.user2_id, tok=self.user2_token)
  53. res = self.helper.send(self.room, body="Hi!", tok=self.user_token)
  54. self.parent_id = res["event_id"]
  55. def _create_user(self, localpart: str) -> Tuple[str, str]:
  56. user_id = self.register_user(localpart, "abc123")
  57. access_token = self.login(localpart, "abc123")
  58. return user_id, access_token
  59. def _send_relation(
  60. self,
  61. relation_type: str,
  62. event_type: str,
  63. key: Optional[str] = None,
  64. content: Optional[dict] = None,
  65. access_token: Optional[str] = None,
  66. parent_id: Optional[str] = None,
  67. expected_response_code: int = 200,
  68. ) -> FakeChannel:
  69. """Helper function to send a relation pointing at `self.parent_id`
  70. Args:
  71. relation_type: One of `RelationTypes`
  72. event_type: The type of the event to create
  73. key: The aggregation key used for m.annotation relation type.
  74. content: The content of the created event. Will be modified to configure
  75. the m.relates_to key based on the other provided parameters.
  76. access_token: The access token used to send the relation, defaults
  77. to `self.user_token`
  78. parent_id: The event_id this relation relates to. If None, then self.parent_id
  79. Returns:
  80. FakeChannel
  81. """
  82. if not access_token:
  83. access_token = self.user_token
  84. original_id = parent_id if parent_id else self.parent_id
  85. if content is None:
  86. content = {}
  87. content["m.relates_to"] = {
  88. "event_id": original_id,
  89. "rel_type": relation_type,
  90. }
  91. if key is not None:
  92. content["m.relates_to"]["key"] = key
  93. channel = self.make_request(
  94. "POST",
  95. f"/_matrix/client/v3/rooms/{self.room}/send/{event_type}",
  96. content,
  97. access_token=access_token,
  98. )
  99. self.assertEqual(expected_response_code, channel.code, channel.json_body)
  100. return channel
  101. def _get_related_events(self) -> List[str]:
  102. """
  103. Requests /relations on the parent ID and returns a list of event IDs.
  104. """
  105. # Request the relations of the event.
  106. channel = self.make_request(
  107. "GET",
  108. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}",
  109. access_token=self.user_token,
  110. )
  111. self.assertEqual(200, channel.code, channel.json_body)
  112. return [ev["event_id"] for ev in channel.json_body["chunk"]]
  113. def _get_bundled_aggregations(self) -> JsonDict:
  114. """
  115. Requests /event on the parent ID and returns the m.relations field (from unsigned), if it exists.
  116. """
  117. # Fetch the bundled aggregations of the event.
  118. channel = self.make_request(
  119. "GET",
  120. f"/_matrix/client/v3/rooms/{self.room}/event/{self.parent_id}",
  121. access_token=self.user_token,
  122. )
  123. self.assertEqual(200, channel.code, channel.json_body)
  124. return channel.json_body["unsigned"].get("m.relations", {})
  125. def _find_event_in_chunk(self, events: List[JsonDict]) -> JsonDict:
  126. """
  127. Find the parent event in a chunk of events and assert that it has the proper bundled aggregations.
  128. """
  129. for event in events:
  130. if event["event_id"] == self.parent_id:
  131. return event
  132. raise AssertionError(f"Event {self.parent_id} not found in chunk")
  133. class RelationsTestCase(BaseRelationsTestCase):
  134. def test_send_relation(self) -> None:
  135. """Tests that sending a relation works."""
  136. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
  137. event_id = channel.json_body["event_id"]
  138. channel = self.make_request(
  139. "GET",
  140. f"/rooms/{self.room}/event/{event_id}",
  141. access_token=self.user_token,
  142. )
  143. self.assertEqual(200, channel.code, channel.json_body)
  144. self.assert_dict(
  145. {
  146. "type": "m.reaction",
  147. "sender": self.user_id,
  148. "content": {
  149. "m.relates_to": {
  150. "event_id": self.parent_id,
  151. "key": "👍",
  152. "rel_type": RelationTypes.ANNOTATION,
  153. }
  154. },
  155. },
  156. channel.json_body,
  157. )
  158. def test_deny_invalid_event(self) -> None:
  159. """Test that we deny relations on non-existant events"""
  160. self._send_relation(
  161. RelationTypes.ANNOTATION,
  162. EventTypes.Message,
  163. parent_id="foo",
  164. content={"body": "foo", "msgtype": "m.text"},
  165. expected_response_code=400,
  166. )
  167. # Unless that event is referenced from another event!
  168. self.get_success(
  169. self.hs.get_datastores().main.db_pool.simple_insert(
  170. table="event_relations",
  171. values={
  172. "event_id": "bar",
  173. "relates_to_id": "foo",
  174. "relation_type": RelationTypes.THREAD,
  175. },
  176. desc="test_deny_invalid_event",
  177. )
  178. )
  179. self._send_relation(
  180. RelationTypes.THREAD,
  181. EventTypes.Message,
  182. parent_id="foo",
  183. content={"body": "foo", "msgtype": "m.text"},
  184. )
  185. def test_deny_invalid_room(self) -> None:
  186. """Test that we deny relations on non-existant events"""
  187. # Create another room and send a message in it.
  188. room2 = self.helper.create_room_as(self.user_id, tok=self.user_token)
  189. res = self.helper.send(room2, body="Hi!", tok=self.user_token)
  190. parent_id = res["event_id"]
  191. # Attempt to send an annotation to that event.
  192. self._send_relation(
  193. RelationTypes.ANNOTATION,
  194. "m.reaction",
  195. parent_id=parent_id,
  196. key="A",
  197. expected_response_code=400,
  198. )
  199. def test_deny_double_react(self) -> None:
  200. """Test that we deny relations on membership events"""
  201. self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
  202. self._send_relation(
  203. RelationTypes.ANNOTATION, "m.reaction", "a", expected_response_code=400
  204. )
  205. def test_deny_forked_thread(self) -> None:
  206. """It is invalid to start a thread off a thread."""
  207. channel = self._send_relation(
  208. RelationTypes.THREAD,
  209. "m.room.message",
  210. content={"msgtype": "m.text", "body": "foo"},
  211. parent_id=self.parent_id,
  212. )
  213. parent_id = channel.json_body["event_id"]
  214. self._send_relation(
  215. RelationTypes.THREAD,
  216. "m.room.message",
  217. content={"msgtype": "m.text", "body": "foo"},
  218. parent_id=parent_id,
  219. expected_response_code=400,
  220. )
  221. def test_ignore_invalid_room(self) -> None:
  222. """Test that we ignore invalid relations over federation."""
  223. # Create another room and send a message in it.
  224. room2 = self.helper.create_room_as(self.user_id, tok=self.user_token)
  225. res = self.helper.send(room2, body="Hi!", tok=self.user_token)
  226. parent_id = res["event_id"]
  227. # Disable the validation to pretend this came over federation.
  228. with patch(
  229. "synapse.handlers.message.EventCreationHandler._validate_event_relation",
  230. new=lambda self, event: make_awaitable(None),
  231. ):
  232. # Generate a various relations from a different room.
  233. self.get_success(
  234. inject_event(
  235. self.hs,
  236. room_id=self.room,
  237. type="m.reaction",
  238. sender=self.user_id,
  239. content={
  240. "m.relates_to": {
  241. "rel_type": RelationTypes.ANNOTATION,
  242. "event_id": parent_id,
  243. "key": "A",
  244. }
  245. },
  246. )
  247. )
  248. self.get_success(
  249. inject_event(
  250. self.hs,
  251. room_id=self.room,
  252. type="m.room.message",
  253. sender=self.user_id,
  254. content={
  255. "body": "foo",
  256. "msgtype": "m.text",
  257. "m.relates_to": {
  258. "rel_type": RelationTypes.REFERENCE,
  259. "event_id": parent_id,
  260. },
  261. },
  262. )
  263. )
  264. self.get_success(
  265. inject_event(
  266. self.hs,
  267. room_id=self.room,
  268. type="m.room.message",
  269. sender=self.user_id,
  270. content={
  271. "body": "foo",
  272. "msgtype": "m.text",
  273. "m.relates_to": {
  274. "rel_type": RelationTypes.THREAD,
  275. "event_id": parent_id,
  276. },
  277. },
  278. )
  279. )
  280. self.get_success(
  281. inject_event(
  282. self.hs,
  283. room_id=self.room,
  284. type="m.room.message",
  285. sender=self.user_id,
  286. content={
  287. "body": "foo",
  288. "msgtype": "m.text",
  289. "new_content": {
  290. "body": "new content",
  291. "msgtype": "m.text",
  292. },
  293. "m.relates_to": {
  294. "rel_type": RelationTypes.REPLACE,
  295. "event_id": parent_id,
  296. },
  297. },
  298. )
  299. )
  300. # They should be ignored when fetching relations.
  301. channel = self.make_request(
  302. "GET",
  303. f"/_matrix/client/v1/rooms/{room2}/relations/{parent_id}",
  304. access_token=self.user_token,
  305. )
  306. self.assertEqual(200, channel.code, channel.json_body)
  307. self.assertEqual(channel.json_body["chunk"], [])
  308. # And for bundled aggregations.
  309. channel = self.make_request(
  310. "GET",
  311. f"/rooms/{room2}/event/{parent_id}",
  312. access_token=self.user_token,
  313. )
  314. self.assertEqual(200, channel.code, channel.json_body)
  315. self.assertNotIn("m.relations", channel.json_body["unsigned"])
  316. def _assert_edit_bundle(
  317. self, event_json: JsonDict, edit_event_id: str, edit_event_content: JsonDict
  318. ) -> None:
  319. """
  320. Assert that the given event has a correctly-serialised edit event in its
  321. bundled aggregations
  322. Args:
  323. event_json: the serialised event to be checked
  324. edit_event_id: the ID of the edit event that we expect to be bundled
  325. edit_event_content: the content of that event, excluding the 'm.relates_to`
  326. property
  327. """
  328. relations_dict = event_json["unsigned"].get("m.relations")
  329. self.assertIn(RelationTypes.REPLACE, relations_dict)
  330. m_replace_dict = relations_dict[RelationTypes.REPLACE]
  331. for key in [
  332. "event_id",
  333. "sender",
  334. "origin_server_ts",
  335. "content",
  336. "type",
  337. "unsigned",
  338. ]:
  339. self.assertIn(key, m_replace_dict)
  340. expected_edit_content = {
  341. "m.relates_to": {
  342. "event_id": event_json["event_id"],
  343. "rel_type": "m.replace",
  344. }
  345. }
  346. expected_edit_content.update(edit_event_content)
  347. self.assert_dict(
  348. {
  349. "event_id": edit_event_id,
  350. "sender": self.user_id,
  351. "content": expected_edit_content,
  352. "type": "m.room.message",
  353. },
  354. m_replace_dict,
  355. )
  356. def test_edit(self) -> None:
  357. """Test that a simple edit works."""
  358. orig_body = {"body": "Hi!", "msgtype": "m.text"}
  359. new_body = {"msgtype": "m.text", "body": "I've been edited!"}
  360. edit_event_content = {
  361. "msgtype": "m.text",
  362. "body": "foo",
  363. "m.new_content": new_body,
  364. }
  365. channel = self._send_relation(
  366. RelationTypes.REPLACE,
  367. "m.room.message",
  368. content=edit_event_content,
  369. )
  370. edit_event_id = channel.json_body["event_id"]
  371. # /event should return the *original* event
  372. channel = self.make_request(
  373. "GET",
  374. f"/rooms/{self.room}/event/{self.parent_id}",
  375. access_token=self.user_token,
  376. )
  377. self.assertEqual(200, channel.code, channel.json_body)
  378. self.assertEqual(channel.json_body["content"], orig_body)
  379. self._assert_edit_bundle(channel.json_body, edit_event_id, edit_event_content)
  380. # Request the room messages.
  381. channel = self.make_request(
  382. "GET",
  383. f"/rooms/{self.room}/messages?dir=b",
  384. access_token=self.user_token,
  385. )
  386. self.assertEqual(200, channel.code, channel.json_body)
  387. self._assert_edit_bundle(
  388. self._find_event_in_chunk(channel.json_body["chunk"]),
  389. edit_event_id,
  390. edit_event_content,
  391. )
  392. # Request the room context.
  393. # /context should return the event.
  394. channel = self.make_request(
  395. "GET",
  396. f"/rooms/{self.room}/context/{self.parent_id}",
  397. access_token=self.user_token,
  398. )
  399. self.assertEqual(200, channel.code, channel.json_body)
  400. self._assert_edit_bundle(
  401. channel.json_body["event"], edit_event_id, edit_event_content
  402. )
  403. self.assertEqual(channel.json_body["event"]["content"], orig_body)
  404. # Request sync, but limit the timeline so it becomes limited (and includes
  405. # bundled aggregations).
  406. filter = urllib.parse.quote_plus(b'{"room": {"timeline": {"limit": 2}}}')
  407. channel = self.make_request(
  408. "GET", f"/sync?filter={filter}", access_token=self.user_token
  409. )
  410. self.assertEqual(200, channel.code, channel.json_body)
  411. room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
  412. self.assertTrue(room_timeline["limited"])
  413. self._assert_edit_bundle(
  414. self._find_event_in_chunk(room_timeline["events"]),
  415. edit_event_id,
  416. edit_event_content,
  417. )
  418. # Request search.
  419. channel = self.make_request(
  420. "POST",
  421. "/search",
  422. # Search term matches the parent message.
  423. content={"search_categories": {"room_events": {"search_term": "Hi"}}},
  424. access_token=self.user_token,
  425. )
  426. self.assertEqual(200, channel.code, channel.json_body)
  427. chunk = [
  428. result["result"]
  429. for result in channel.json_body["search_categories"]["room_events"][
  430. "results"
  431. ]
  432. ]
  433. self._assert_edit_bundle(
  434. self._find_event_in_chunk(chunk),
  435. edit_event_id,
  436. edit_event_content,
  437. )
  438. def test_multi_edit(self) -> None:
  439. """Test that multiple edits, including attempts by people who
  440. shouldn't be allowed, are correctly handled.
  441. """
  442. orig_body = orig_body = {"body": "Hi!", "msgtype": "m.text"}
  443. self._send_relation(
  444. RelationTypes.REPLACE,
  445. "m.room.message",
  446. content={
  447. "msgtype": "m.text",
  448. "body": "Wibble",
  449. "m.new_content": {"msgtype": "m.text", "body": "First edit"},
  450. },
  451. )
  452. new_body = {"msgtype": "m.text", "body": "I've been edited!"}
  453. edit_event_content = {
  454. "msgtype": "m.text",
  455. "body": "foo",
  456. "m.new_content": new_body,
  457. }
  458. channel = self._send_relation(
  459. RelationTypes.REPLACE,
  460. "m.room.message",
  461. content=edit_event_content,
  462. )
  463. edit_event_id = channel.json_body["event_id"]
  464. self._send_relation(
  465. RelationTypes.REPLACE,
  466. "m.room.message.WRONG_TYPE",
  467. content={
  468. "msgtype": "m.text",
  469. "body": "Wibble",
  470. "m.new_content": {"msgtype": "m.text", "body": "Edit, but wrong type"},
  471. },
  472. )
  473. channel = self.make_request(
  474. "GET",
  475. f"/rooms/{self.room}/context/{self.parent_id}",
  476. access_token=self.user_token,
  477. )
  478. self.assertEqual(200, channel.code, channel.json_body)
  479. self.assertEqual(channel.json_body["event"]["content"], orig_body)
  480. self._assert_edit_bundle(
  481. channel.json_body["event"], edit_event_id, edit_event_content
  482. )
  483. def test_edit_reply(self) -> None:
  484. """Test that editing a reply works."""
  485. # Create a reply to edit.
  486. original_body = {"msgtype": "m.text", "body": "A reply!"}
  487. channel = self._send_relation(
  488. RelationTypes.REFERENCE, "m.room.message", content=original_body
  489. )
  490. reply = channel.json_body["event_id"]
  491. edit_event_content = {
  492. "msgtype": "m.text",
  493. "body": "foo",
  494. "m.new_content": {"msgtype": "m.text", "body": "I've been edited!"},
  495. }
  496. channel = self._send_relation(
  497. RelationTypes.REPLACE,
  498. "m.room.message",
  499. content=edit_event_content,
  500. parent_id=reply,
  501. )
  502. edit_event_id = channel.json_body["event_id"]
  503. # /event returns the original event
  504. channel = self.make_request(
  505. "GET",
  506. f"/rooms/{self.room}/event/{reply}",
  507. access_token=self.user_token,
  508. )
  509. self.assertEqual(200, channel.code, channel.json_body)
  510. event_result = channel.json_body
  511. self.assertDictContainsSubset(original_body, event_result["content"])
  512. # also check /context, which returns the *edited* event
  513. channel = self.make_request(
  514. "GET",
  515. f"/rooms/{self.room}/context/{reply}",
  516. access_token=self.user_token,
  517. )
  518. self.assertEqual(200, channel.code, channel.json_body)
  519. context_result = channel.json_body["event"]
  520. # check that the relations are correct for both APIs
  521. for result_event_dict, desc in (
  522. (event_result, "/event"),
  523. (context_result, "/context"),
  524. ):
  525. # The reference metadata should still be intact.
  526. self.assertDictContainsSubset(
  527. {
  528. "m.relates_to": {
  529. "event_id": self.parent_id,
  530. "rel_type": "m.reference",
  531. }
  532. },
  533. result_event_dict["content"],
  534. desc,
  535. )
  536. # We expect that the edit relation appears in the unsigned relations
  537. # section.
  538. self._assert_edit_bundle(
  539. result_event_dict, edit_event_id, edit_event_content
  540. )
  541. def test_edit_edit(self) -> None:
  542. """Test that an edit cannot be edited."""
  543. orig_body = {"body": "Hi!", "msgtype": "m.text"}
  544. new_body = {"msgtype": "m.text", "body": "Initial edit"}
  545. edit_event_content = {
  546. "msgtype": "m.text",
  547. "body": "Wibble",
  548. "m.new_content": new_body,
  549. }
  550. channel = self._send_relation(
  551. RelationTypes.REPLACE,
  552. "m.room.message",
  553. content=edit_event_content,
  554. )
  555. edit_event_id = channel.json_body["event_id"]
  556. # Edit the edit event.
  557. self._send_relation(
  558. RelationTypes.REPLACE,
  559. "m.room.message",
  560. content={
  561. "msgtype": "m.text",
  562. "body": "foo",
  563. "m.new_content": {"msgtype": "m.text", "body": "Ignored edit"},
  564. },
  565. parent_id=edit_event_id,
  566. )
  567. # Request the original event.
  568. # /event should return the original event.
  569. channel = self.make_request(
  570. "GET",
  571. f"/rooms/{self.room}/event/{self.parent_id}",
  572. access_token=self.user_token,
  573. )
  574. self.assertEqual(200, channel.code, channel.json_body)
  575. self.assertEqual(channel.json_body["content"], orig_body)
  576. # The relations information should not include the edit to the edit.
  577. self._assert_edit_bundle(channel.json_body, edit_event_id, edit_event_content)
  578. # /context should return the bundled edit for the *first* edit
  579. # (The edit to the edit should be ignored.)
  580. channel = self.make_request(
  581. "GET",
  582. f"/rooms/{self.room}/context/{self.parent_id}",
  583. access_token=self.user_token,
  584. )
  585. self.assertEqual(200, channel.code, channel.json_body)
  586. self.assertEqual(channel.json_body["event"]["content"], orig_body)
  587. self._assert_edit_bundle(
  588. channel.json_body["event"], edit_event_id, edit_event_content
  589. )
  590. # Directly requesting the edit should not have the edit to the edit applied.
  591. channel = self.make_request(
  592. "GET",
  593. f"/rooms/{self.room}/event/{edit_event_id}",
  594. access_token=self.user_token,
  595. )
  596. self.assertEqual(200, channel.code, channel.json_body)
  597. self.assertEqual("Wibble", channel.json_body["content"]["body"])
  598. self.assertIn("m.new_content", channel.json_body["content"])
  599. # The relations information should not include the edit to the edit.
  600. self.assertNotIn("m.relations", channel.json_body["unsigned"])
  601. def test_unknown_relations(self) -> None:
  602. """Unknown relations should be accepted."""
  603. channel = self._send_relation("m.relation.test", "m.room.test")
  604. event_id = channel.json_body["event_id"]
  605. channel = self.make_request(
  606. "GET",
  607. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=1",
  608. access_token=self.user_token,
  609. )
  610. self.assertEqual(200, channel.code, channel.json_body)
  611. # We expect to get back a single pagination result, which is the full
  612. # relation event we sent above.
  613. self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
  614. self.assert_dict(
  615. {"event_id": event_id, "sender": self.user_id, "type": "m.room.test"},
  616. channel.json_body["chunk"][0],
  617. )
  618. # We also expect to get the original event (the id of which is self.parent_id)
  619. # when requesting the unstable endpoint.
  620. self.assertNotIn("original_event", channel.json_body)
  621. channel = self.make_request(
  622. "GET",
  623. f"/_matrix/client/unstable/rooms/{self.room}/relations/{self.parent_id}?limit=1",
  624. access_token=self.user_token,
  625. )
  626. self.assertEqual(200, channel.code, channel.json_body)
  627. self.assertEqual(
  628. channel.json_body["original_event"]["event_id"], self.parent_id
  629. )
  630. # When bundling the unknown relation is not included.
  631. channel = self.make_request(
  632. "GET",
  633. f"/rooms/{self.room}/event/{self.parent_id}",
  634. access_token=self.user_token,
  635. )
  636. self.assertEqual(200, channel.code, channel.json_body)
  637. self.assertNotIn("m.relations", channel.json_body["unsigned"])
  638. def test_background_update(self) -> None:
  639. """Test the event_arbitrary_relations background update."""
  640. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
  641. annotation_event_id_good = channel.json_body["event_id"]
  642. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="A")
  643. annotation_event_id_bad = channel.json_body["event_id"]
  644. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  645. thread_event_id = channel.json_body["event_id"]
  646. # Clean-up the table as if the inserts did not happen during event creation.
  647. self.get_success(
  648. self.store.db_pool.simple_delete_many(
  649. table="event_relations",
  650. column="event_id",
  651. iterable=(annotation_event_id_bad, thread_event_id),
  652. keyvalues={},
  653. desc="RelationsTestCase.test_background_update",
  654. )
  655. )
  656. # Only the "good" annotation should be found.
  657. channel = self.make_request(
  658. "GET",
  659. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=10",
  660. access_token=self.user_token,
  661. )
  662. self.assertEqual(200, channel.code, channel.json_body)
  663. self.assertEqual(
  664. [ev["event_id"] for ev in channel.json_body["chunk"]],
  665. [annotation_event_id_good],
  666. )
  667. # Insert and run the background update.
  668. self.get_success(
  669. self.store.db_pool.simple_insert(
  670. "background_updates",
  671. {"update_name": "event_arbitrary_relations", "progress_json": "{}"},
  672. )
  673. )
  674. # Ugh, have to reset this flag
  675. self.store.db_pool.updates._all_done = False
  676. self.wait_for_background_updates()
  677. # The "good" annotation and the thread should be found, but not the "bad"
  678. # annotation.
  679. channel = self.make_request(
  680. "GET",
  681. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=10",
  682. access_token=self.user_token,
  683. )
  684. self.assertEqual(200, channel.code, channel.json_body)
  685. self.assertCountEqual(
  686. [ev["event_id"] for ev in channel.json_body["chunk"]],
  687. [annotation_event_id_good, thread_event_id],
  688. )
  689. class RelationPaginationTestCase(BaseRelationsTestCase):
  690. def test_basic_paginate_relations(self) -> None:
  691. """Tests that calling pagination API correctly the latest relations."""
  692. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  693. first_annotation_id = channel.json_body["event_id"]
  694. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
  695. second_annotation_id = channel.json_body["event_id"]
  696. channel = self.make_request(
  697. "GET",
  698. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=1",
  699. access_token=self.user_token,
  700. )
  701. self.assertEqual(200, channel.code, channel.json_body)
  702. # We expect to get back a single pagination result, which is the latest
  703. # full relation event we sent above.
  704. self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
  705. self.assert_dict(
  706. {
  707. "event_id": second_annotation_id,
  708. "sender": self.user_id,
  709. "type": "m.reaction",
  710. },
  711. channel.json_body["chunk"][0],
  712. )
  713. # Make sure next_batch has something in it that looks like it could be a
  714. # valid token.
  715. self.assertIsInstance(
  716. channel.json_body.get("next_batch"), str, channel.json_body
  717. )
  718. # Request the relations again, but with a different direction.
  719. channel = self.make_request(
  720. "GET",
  721. f"/_matrix/client/v1/rooms/{self.room}/relations"
  722. f"/{self.parent_id}?limit=1&dir=f",
  723. access_token=self.user_token,
  724. )
  725. self.assertEqual(200, channel.code, channel.json_body)
  726. # We expect to get back a single pagination result, which is the earliest
  727. # full relation event we sent above.
  728. self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
  729. self.assert_dict(
  730. {
  731. "event_id": first_annotation_id,
  732. "sender": self.user_id,
  733. "type": "m.reaction",
  734. },
  735. channel.json_body["chunk"][0],
  736. )
  737. def test_repeated_paginate_relations(self) -> None:
  738. """Test that if we paginate using a limit and tokens then we get the
  739. expected events.
  740. """
  741. expected_event_ids = []
  742. for idx in range(10):
  743. channel = self._send_relation(
  744. RelationTypes.ANNOTATION, "m.reaction", chr(ord("a") + idx)
  745. )
  746. expected_event_ids.append(channel.json_body["event_id"])
  747. prev_token: Optional[str] = ""
  748. found_event_ids: List[str] = []
  749. for _ in range(20):
  750. from_token = ""
  751. if prev_token:
  752. from_token = "&from=" + prev_token
  753. channel = self.make_request(
  754. "GET",
  755. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=3{from_token}",
  756. access_token=self.user_token,
  757. )
  758. self.assertEqual(200, channel.code, channel.json_body)
  759. found_event_ids.extend(e["event_id"] for e in channel.json_body["chunk"])
  760. next_batch = channel.json_body.get("next_batch")
  761. self.assertNotEqual(prev_token, next_batch)
  762. prev_token = next_batch
  763. if not prev_token:
  764. break
  765. # We paginated backwards, so reverse
  766. found_event_ids.reverse()
  767. self.assertEqual(found_event_ids, expected_event_ids)
  768. # Test forward pagination.
  769. prev_token = ""
  770. found_event_ids = []
  771. for _ in range(20):
  772. from_token = ""
  773. if prev_token:
  774. from_token = "&from=" + prev_token
  775. channel = self.make_request(
  776. "GET",
  777. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?dir=f&limit=3{from_token}",
  778. access_token=self.user_token,
  779. )
  780. self.assertEqual(200, channel.code, channel.json_body)
  781. found_event_ids.extend(e["event_id"] for e in channel.json_body["chunk"])
  782. next_batch = channel.json_body.get("next_batch")
  783. self.assertNotEqual(prev_token, next_batch)
  784. prev_token = next_batch
  785. if not prev_token:
  786. break
  787. self.assertEqual(found_event_ids, expected_event_ids)
  788. def test_pagination_from_sync_and_messages(self) -> None:
  789. """Pagination tokens from /sync and /messages can be used to paginate /relations."""
  790. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "A")
  791. annotation_id = channel.json_body["event_id"]
  792. # Send an event after the relation events.
  793. self.helper.send(self.room, body="Latest event", tok=self.user_token)
  794. # Request /sync, limiting it such that only the latest event is returned
  795. # (and not the relation).
  796. filter = urllib.parse.quote_plus(b'{"room": {"timeline": {"limit": 1}}}')
  797. channel = self.make_request(
  798. "GET", f"/sync?filter={filter}", access_token=self.user_token
  799. )
  800. self.assertEqual(200, channel.code, channel.json_body)
  801. room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
  802. sync_prev_batch = room_timeline["prev_batch"]
  803. self.assertIsNotNone(sync_prev_batch)
  804. # Ensure the relation event is not in the batch returned from /sync.
  805. self.assertNotIn(
  806. annotation_id, [ev["event_id"] for ev in room_timeline["events"]]
  807. )
  808. # Request /messages, limiting it such that only the latest event is
  809. # returned (and not the relation).
  810. channel = self.make_request(
  811. "GET",
  812. f"/rooms/{self.room}/messages?dir=b&limit=1",
  813. access_token=self.user_token,
  814. )
  815. self.assertEqual(200, channel.code, channel.json_body)
  816. messages_end = channel.json_body["end"]
  817. self.assertIsNotNone(messages_end)
  818. # Ensure the relation event is not in the chunk returned from /messages.
  819. self.assertNotIn(
  820. annotation_id, [ev["event_id"] for ev in channel.json_body["chunk"]]
  821. )
  822. # Request /relations with the pagination tokens received from both the
  823. # /sync and /messages responses above, in turn.
  824. #
  825. # This is a tiny bit silly since the client wouldn't know the parent ID
  826. # from the requests above; consider the parent ID to be known from a
  827. # previous /sync.
  828. for from_token in (sync_prev_batch, messages_end):
  829. channel = self.make_request(
  830. "GET",
  831. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?from={from_token}",
  832. access_token=self.user_token,
  833. )
  834. self.assertEqual(200, channel.code, channel.json_body)
  835. # The relation should be in the returned chunk.
  836. self.assertIn(
  837. annotation_id, [ev["event_id"] for ev in channel.json_body["chunk"]]
  838. )
  839. class RecursiveRelationTestCase(BaseRelationsTestCase):
  840. @override_config({"experimental_features": {"msc3981_recurse_relations": True}})
  841. def test_recursive_relations(self) -> None:
  842. """Generate a complex, multi-level relationship tree and query it."""
  843. # Create a thread with a few messages in it.
  844. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  845. thread_1 = channel.json_body["event_id"]
  846. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  847. thread_2 = channel.json_body["event_id"]
  848. # Add annotations.
  849. channel = self._send_relation(
  850. RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=thread_2
  851. )
  852. annotation_1 = channel.json_body["event_id"]
  853. channel = self._send_relation(
  854. RelationTypes.ANNOTATION, "m.reaction", "b", parent_id=thread_1
  855. )
  856. annotation_2 = channel.json_body["event_id"]
  857. # Add a reference to part of the thread, then edit the reference and annotate it.
  858. channel = self._send_relation(
  859. RelationTypes.REFERENCE, "m.room.test", parent_id=thread_2
  860. )
  861. reference_1 = channel.json_body["event_id"]
  862. channel = self._send_relation(
  863. RelationTypes.ANNOTATION, "m.reaction", "c", parent_id=reference_1
  864. )
  865. annotation_3 = channel.json_body["event_id"]
  866. channel = self._send_relation(
  867. RelationTypes.REPLACE,
  868. "m.room.test",
  869. parent_id=reference_1,
  870. )
  871. edit = channel.json_body["event_id"]
  872. # Also more events off the root.
  873. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "d")
  874. annotation_4 = channel.json_body["event_id"]
  875. channel = self.make_request(
  876. "GET",
  877. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}"
  878. "?dir=f&limit=20&org.matrix.msc3981.recurse=true",
  879. access_token=self.user_token,
  880. )
  881. self.assertEqual(200, channel.code, channel.json_body)
  882. # The above events should be returned in creation order.
  883. event_ids = [ev["event_id"] for ev in channel.json_body["chunk"]]
  884. self.assertEqual(
  885. event_ids,
  886. [
  887. thread_1,
  888. thread_2,
  889. annotation_1,
  890. annotation_2,
  891. reference_1,
  892. annotation_3,
  893. edit,
  894. annotation_4,
  895. ],
  896. )
  897. @override_config({"experimental_features": {"msc3981_recurse_relations": True}})
  898. def test_recursive_relations_with_filter(self) -> None:
  899. """The event_type and rel_type still apply."""
  900. # Create a thread with a few messages in it.
  901. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  902. thread_1 = channel.json_body["event_id"]
  903. # Add annotations.
  904. channel = self._send_relation(
  905. RelationTypes.ANNOTATION, "m.reaction", "b", parent_id=thread_1
  906. )
  907. annotation_1 = channel.json_body["event_id"]
  908. # Add a reference to part of the thread, then edit the reference and annotate it.
  909. channel = self._send_relation(
  910. RelationTypes.REFERENCE, "m.room.test", parent_id=thread_1
  911. )
  912. reference_1 = channel.json_body["event_id"]
  913. channel = self._send_relation(
  914. RelationTypes.ANNOTATION, "org.matrix.reaction", "c", parent_id=reference_1
  915. )
  916. annotation_2 = channel.json_body["event_id"]
  917. # Fetch only annotations, but recursively.
  918. channel = self.make_request(
  919. "GET",
  920. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}/{RelationTypes.ANNOTATION}"
  921. "?dir=f&limit=20&org.matrix.msc3981.recurse=true",
  922. access_token=self.user_token,
  923. )
  924. self.assertEqual(200, channel.code, channel.json_body)
  925. # The above events should be returned in creation order.
  926. event_ids = [ev["event_id"] for ev in channel.json_body["chunk"]]
  927. self.assertEqual(event_ids, [annotation_1, annotation_2])
  928. # Fetch only m.reactions, but recursively.
  929. channel = self.make_request(
  930. "GET",
  931. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}/{RelationTypes.ANNOTATION}/m.reaction"
  932. "?dir=f&limit=20&org.matrix.msc3981.recurse=true",
  933. access_token=self.user_token,
  934. )
  935. self.assertEqual(200, channel.code, channel.json_body)
  936. # The above events should be returned in creation order.
  937. event_ids = [ev["event_id"] for ev in channel.json_body["chunk"]]
  938. self.assertEqual(event_ids, [annotation_1])
  939. class BundledAggregationsTestCase(BaseRelationsTestCase):
  940. """
  941. See RelationsTestCase.test_edit for a similar test for edits.
  942. Note that this doesn't test against /relations since only thread relations
  943. get bundled via that API. See test_aggregation_get_event_for_thread.
  944. """
  945. def _test_bundled_aggregations(
  946. self,
  947. relation_type: str,
  948. assertion_callable: Callable[[JsonDict], None],
  949. expected_db_txn_for_event: int,
  950. access_token: Optional[str] = None,
  951. ) -> None:
  952. """
  953. Makes requests to various endpoints which should include bundled aggregations
  954. and then calls an assertion function on the bundled aggregations.
  955. Args:
  956. relation_type: The field to search for in the `m.relations` field in unsigned.
  957. assertion_callable: Called with the contents of unsigned["m.relations"][relation_type]
  958. for relation-specific assertions.
  959. expected_db_txn_for_event: The number of database transactions which
  960. are expected for a call to /event/.
  961. access_token: The access token to user, defaults to self.user_token.
  962. """
  963. access_token = access_token or self.user_token
  964. def assert_bundle(event_json: JsonDict) -> None:
  965. """Assert the expected values of the bundled aggregations."""
  966. relations_dict = event_json["unsigned"].get("m.relations")
  967. # Ensure the fields are as expected.
  968. self.assertCountEqual(relations_dict.keys(), (relation_type,))
  969. assertion_callable(relations_dict[relation_type])
  970. # Request the event directly.
  971. channel = self.make_request(
  972. "GET",
  973. f"/rooms/{self.room}/event/{self.parent_id}",
  974. access_token=access_token,
  975. )
  976. self.assertEqual(200, channel.code, channel.json_body)
  977. assert_bundle(channel.json_body)
  978. assert channel.resource_usage is not None
  979. self.assertEqual(channel.resource_usage.db_txn_count, expected_db_txn_for_event)
  980. # Request the room messages.
  981. channel = self.make_request(
  982. "GET",
  983. f"/rooms/{self.room}/messages?dir=b",
  984. access_token=access_token,
  985. )
  986. self.assertEqual(200, channel.code, channel.json_body)
  987. assert_bundle(self._find_event_in_chunk(channel.json_body["chunk"]))
  988. # Request the room context.
  989. channel = self.make_request(
  990. "GET",
  991. f"/rooms/{self.room}/context/{self.parent_id}",
  992. access_token=access_token,
  993. )
  994. self.assertEqual(200, channel.code, channel.json_body)
  995. assert_bundle(channel.json_body["event"])
  996. # Request sync.
  997. filter = urllib.parse.quote_plus(b'{"room": {"timeline": {"limit": 4}}}')
  998. channel = self.make_request(
  999. "GET", f"/sync?filter={filter}", access_token=access_token
  1000. )
  1001. self.assertEqual(200, channel.code, channel.json_body)
  1002. room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
  1003. self.assertTrue(room_timeline["limited"])
  1004. assert_bundle(self._find_event_in_chunk(room_timeline["events"]))
  1005. # Request search.
  1006. channel = self.make_request(
  1007. "POST",
  1008. "/search",
  1009. # Search term matches the parent message.
  1010. content={"search_categories": {"room_events": {"search_term": "Hi"}}},
  1011. access_token=access_token,
  1012. )
  1013. self.assertEqual(200, channel.code, channel.json_body)
  1014. chunk = [
  1015. result["result"]
  1016. for result in channel.json_body["search_categories"]["room_events"][
  1017. "results"
  1018. ]
  1019. ]
  1020. assert_bundle(self._find_event_in_chunk(chunk))
  1021. def test_reference(self) -> None:
  1022. """
  1023. Test that references get correctly bundled.
  1024. """
  1025. channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
  1026. reply_1 = channel.json_body["event_id"]
  1027. channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
  1028. reply_2 = channel.json_body["event_id"]
  1029. def assert_annotations(bundled_aggregations: JsonDict) -> None:
  1030. self.assertEqual(
  1031. {"chunk": [{"event_id": reply_1}, {"event_id": reply_2}]},
  1032. bundled_aggregations,
  1033. )
  1034. self._test_bundled_aggregations(RelationTypes.REFERENCE, assert_annotations, 6)
  1035. def test_thread(self) -> None:
  1036. """
  1037. Test that threads get correctly bundled.
  1038. """
  1039. # The root message is from "user", send replies as "user2".
  1040. self._send_relation(
  1041. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1042. )
  1043. channel = self._send_relation(
  1044. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1045. )
  1046. thread_2 = channel.json_body["event_id"]
  1047. # This needs two assertion functions which are identical except for whether
  1048. # the current_user_participated flag is True, create a factory for the
  1049. # two versions.
  1050. def _gen_assert(participated: bool) -> Callable[[JsonDict], None]:
  1051. def assert_thread(bundled_aggregations: JsonDict) -> None:
  1052. self.assertEqual(2, bundled_aggregations.get("count"))
  1053. self.assertEqual(
  1054. participated, bundled_aggregations.get("current_user_participated")
  1055. )
  1056. # The latest thread event has some fields that don't matter.
  1057. self.assertIn("latest_event", bundled_aggregations)
  1058. self.assert_dict(
  1059. {
  1060. "content": {
  1061. "m.relates_to": {
  1062. "event_id": self.parent_id,
  1063. "rel_type": RelationTypes.THREAD,
  1064. }
  1065. },
  1066. "event_id": thread_2,
  1067. "sender": self.user2_id,
  1068. "type": "m.room.test",
  1069. },
  1070. bundled_aggregations["latest_event"],
  1071. )
  1072. return assert_thread
  1073. # The "user" sent the root event and is making queries for the bundled
  1074. # aggregations: they have participated.
  1075. self._test_bundled_aggregations(RelationTypes.THREAD, _gen_assert(True), 6)
  1076. # The "user2" sent replies in the thread and is making queries for the
  1077. # bundled aggregations: they have participated.
  1078. #
  1079. # Note that this re-uses some cached values, so the total number of
  1080. # queries is much smaller.
  1081. self._test_bundled_aggregations(
  1082. RelationTypes.THREAD, _gen_assert(True), 3, access_token=self.user2_token
  1083. )
  1084. # A user with no interactions with the thread: they have not participated.
  1085. user3_id, user3_token = self._create_user("charlie")
  1086. self.helper.join(self.room, user=user3_id, tok=user3_token)
  1087. self._test_bundled_aggregations(
  1088. RelationTypes.THREAD, _gen_assert(False), 3, access_token=user3_token
  1089. )
  1090. def test_thread_with_bundled_aggregations_for_latest(self) -> None:
  1091. """
  1092. Bundled aggregations should get applied to the latest thread event.
  1093. """
  1094. self._send_relation(RelationTypes.THREAD, "m.room.test")
  1095. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1096. thread_2 = channel.json_body["event_id"]
  1097. channel = self._send_relation(
  1098. RelationTypes.REFERENCE, "org.matrix.test", parent_id=thread_2
  1099. )
  1100. reference_event_id = channel.json_body["event_id"]
  1101. def assert_thread(bundled_aggregations: JsonDict) -> None:
  1102. self.assertEqual(2, bundled_aggregations.get("count"))
  1103. self.assertTrue(bundled_aggregations.get("current_user_participated"))
  1104. # The latest thread event has some fields that don't matter.
  1105. self.assertIn("latest_event", bundled_aggregations)
  1106. self.assert_dict(
  1107. {
  1108. "content": {
  1109. "m.relates_to": {
  1110. "event_id": self.parent_id,
  1111. "rel_type": RelationTypes.THREAD,
  1112. }
  1113. },
  1114. "event_id": thread_2,
  1115. "sender": self.user_id,
  1116. "type": "m.room.test",
  1117. },
  1118. bundled_aggregations["latest_event"],
  1119. )
  1120. # Check the unsigned field on the latest event.
  1121. self.assert_dict(
  1122. {
  1123. "m.relations": {
  1124. RelationTypes.REFERENCE: {
  1125. "chunk": [{"event_id": reference_event_id}]
  1126. },
  1127. }
  1128. },
  1129. bundled_aggregations["latest_event"].get("unsigned"),
  1130. )
  1131. self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 6)
  1132. def test_nested_thread(self) -> None:
  1133. """
  1134. Ensure that a nested thread gets ignored by bundled aggregations, as
  1135. those are forbidden.
  1136. """
  1137. # Start a thread.
  1138. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1139. reply_event_id = channel.json_body["event_id"]
  1140. # Disable the validation to pretend this came over federation, since it is
  1141. # not an event the Client-Server API will allow..
  1142. with patch(
  1143. "synapse.handlers.message.EventCreationHandler._validate_event_relation",
  1144. new=lambda self, event: make_awaitable(None),
  1145. ):
  1146. # Create a sub-thread off the thread, which is not allowed.
  1147. self._send_relation(
  1148. RelationTypes.THREAD, "m.room.test", parent_id=reply_event_id
  1149. )
  1150. # Fetch the thread root, to get the bundled aggregation for the thread.
  1151. relations_from_event = self._get_bundled_aggregations()
  1152. # Ensure that requesting the room messages also does not return the sub-thread.
  1153. channel = self.make_request(
  1154. "GET",
  1155. f"/rooms/{self.room}/messages?dir=b",
  1156. access_token=self.user_token,
  1157. )
  1158. self.assertEqual(200, channel.code, channel.json_body)
  1159. event = self._find_event_in_chunk(channel.json_body["chunk"])
  1160. relations_from_messages = event["unsigned"]["m.relations"]
  1161. # Check the bundled aggregations from each point.
  1162. for aggregations, desc in (
  1163. (relations_from_event, "/event"),
  1164. (relations_from_messages, "/messages"),
  1165. ):
  1166. # The latest event should have bundled aggregations.
  1167. self.assertIn(RelationTypes.THREAD, aggregations, desc)
  1168. thread_summary = aggregations[RelationTypes.THREAD]
  1169. self.assertIn("latest_event", thread_summary, desc)
  1170. self.assertEqual(
  1171. thread_summary["latest_event"]["event_id"], reply_event_id, desc
  1172. )
  1173. # The latest event should not have any bundled aggregations (since the
  1174. # only relation to it is another thread, which is invalid).
  1175. self.assertNotIn(
  1176. "m.relations", thread_summary["latest_event"]["unsigned"], desc
  1177. )
  1178. def test_thread_edit_latest_event(self) -> None:
  1179. """Test that editing the latest event in a thread works."""
  1180. # Create a thread and edit the last event.
  1181. channel = self._send_relation(
  1182. RelationTypes.THREAD,
  1183. "m.room.message",
  1184. content={"msgtype": "m.text", "body": "A threaded reply!"},
  1185. )
  1186. threaded_event_id = channel.json_body["event_id"]
  1187. new_body = {"msgtype": "m.text", "body": "I've been edited!"}
  1188. channel = self._send_relation(
  1189. RelationTypes.REPLACE,
  1190. "m.room.message",
  1191. content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
  1192. parent_id=threaded_event_id,
  1193. )
  1194. edit_event_id = channel.json_body["event_id"]
  1195. # Fetch the thread root, to get the bundled aggregation for the thread.
  1196. relations_dict = self._get_bundled_aggregations()
  1197. # We expect that the edit message appears in the thread summary in the
  1198. # unsigned relations section.
  1199. self.assertIn(RelationTypes.THREAD, relations_dict)
  1200. thread_summary = relations_dict[RelationTypes.THREAD]
  1201. self.assertIn("latest_event", thread_summary)
  1202. latest_event_in_thread = thread_summary["latest_event"]
  1203. # The latest event in the thread should have the edit appear under the
  1204. # bundled aggregations.
  1205. self.assertDictContainsSubset(
  1206. {"event_id": edit_event_id, "sender": "@alice:test"},
  1207. latest_event_in_thread["unsigned"]["m.relations"][RelationTypes.REPLACE],
  1208. )
  1209. def test_aggregation_get_event_for_annotation(self) -> None:
  1210. """Test that annotations do not get bundled aggregations included
  1211. when directly requested.
  1212. """
  1213. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  1214. annotation_id = channel.json_body["event_id"]
  1215. # Annotate the annotation.
  1216. self._send_relation(
  1217. RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=annotation_id
  1218. )
  1219. channel = self.make_request(
  1220. "GET",
  1221. f"/rooms/{self.room}/event/{annotation_id}",
  1222. access_token=self.user_token,
  1223. )
  1224. self.assertEqual(200, channel.code, channel.json_body)
  1225. self.assertIsNone(channel.json_body["unsigned"].get("m.relations"))
  1226. def test_aggregation_get_event_for_thread(self) -> None:
  1227. """Test that threads get bundled aggregations included when directly requested."""
  1228. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1229. thread_id = channel.json_body["event_id"]
  1230. # Make a reference to the thread.
  1231. channel = self._send_relation(
  1232. RelationTypes.REFERENCE, "org.matrix.test", parent_id=thread_id
  1233. )
  1234. reference_event_id = channel.json_body["event_id"]
  1235. channel = self.make_request(
  1236. "GET",
  1237. f"/rooms/{self.room}/event/{thread_id}",
  1238. access_token=self.user_token,
  1239. )
  1240. self.assertEqual(200, channel.code, channel.json_body)
  1241. self.assertEqual(
  1242. channel.json_body["unsigned"].get("m.relations"),
  1243. {
  1244. RelationTypes.REFERENCE: {"chunk": [{"event_id": reference_event_id}]},
  1245. },
  1246. )
  1247. # It should also be included when the entire thread is requested.
  1248. channel = self.make_request(
  1249. "GET",
  1250. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=1",
  1251. access_token=self.user_token,
  1252. )
  1253. self.assertEqual(200, channel.code, channel.json_body)
  1254. self.assertEqual(len(channel.json_body["chunk"]), 1)
  1255. thread_message = channel.json_body["chunk"][0]
  1256. self.assertEqual(
  1257. thread_message["unsigned"].get("m.relations"),
  1258. {
  1259. RelationTypes.REFERENCE: {"chunk": [{"event_id": reference_event_id}]},
  1260. },
  1261. )
  1262. def test_bundled_aggregations_with_filter(self) -> None:
  1263. """
  1264. If "unsigned" is an omitted field (due to filtering), adding the bundled
  1265. aggregations should not break.
  1266. Note that the spec allows for a server to return additional fields beyond
  1267. what is specified.
  1268. """
  1269. channel = self._send_relation(RelationTypes.REFERENCE, "org.matrix.test")
  1270. reference_event_id = channel.json_body["event_id"]
  1271. # Note that the sync filter does not include "unsigned" as a field.
  1272. filter = urllib.parse.quote_plus(
  1273. b'{"event_fields": ["content", "event_id"], "room": {"timeline": {"limit": 3}}}'
  1274. )
  1275. channel = self.make_request(
  1276. "GET", f"/sync?filter={filter}", access_token=self.user_token
  1277. )
  1278. self.assertEqual(200, channel.code, channel.json_body)
  1279. # Ensure the timeline is limited, find the parent event.
  1280. room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
  1281. self.assertTrue(room_timeline["limited"])
  1282. parent_event = self._find_event_in_chunk(room_timeline["events"])
  1283. # Ensure there's bundled aggregations on it.
  1284. self.assertIn("unsigned", parent_event)
  1285. self.assertEqual(
  1286. parent_event["unsigned"].get("m.relations"),
  1287. {
  1288. RelationTypes.REFERENCE: {"chunk": [{"event_id": reference_event_id}]},
  1289. },
  1290. )
  1291. class RelationIgnoredUserTestCase(BaseRelationsTestCase):
  1292. """Relations sent from an ignored user should be ignored."""
  1293. def _test_ignored_user(
  1294. self,
  1295. relation_type: str,
  1296. allowed_event_ids: List[str],
  1297. ignored_event_ids: List[str],
  1298. ) -> Tuple[JsonDict, JsonDict]:
  1299. """
  1300. Fetch the relations and ensure they're all there, then ignore user2, and
  1301. repeat.
  1302. Returns:
  1303. A tuple of two JSON dictionaries, each are bundled aggregations, the
  1304. first is from before the user is ignored, and the second is after.
  1305. """
  1306. # Get the relations.
  1307. event_ids = self._get_related_events()
  1308. self.assertCountEqual(event_ids, allowed_event_ids + ignored_event_ids)
  1309. # And the bundled aggregations.
  1310. before_aggregations = self._get_bundled_aggregations()
  1311. self.assertIn(relation_type, before_aggregations)
  1312. # Ignore user2 and re-do the requests.
  1313. self.get_success(
  1314. self.store.add_account_data_for_user(
  1315. self.user_id,
  1316. AccountDataTypes.IGNORED_USER_LIST,
  1317. {"ignored_users": {self.user2_id: {}}},
  1318. )
  1319. )
  1320. # Get the relations.
  1321. event_ids = self._get_related_events()
  1322. self.assertCountEqual(event_ids, allowed_event_ids)
  1323. # And the bundled aggregations.
  1324. after_aggregations = self._get_bundled_aggregations()
  1325. self.assertIn(relation_type, after_aggregations)
  1326. return before_aggregations[relation_type], after_aggregations[relation_type]
  1327. def test_reference(self) -> None:
  1328. """Aggregations should exclude reference relations from ignored users"""
  1329. channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
  1330. allowed_event_ids = [channel.json_body["event_id"]]
  1331. channel = self._send_relation(
  1332. RelationTypes.REFERENCE, "m.room.test", access_token=self.user2_token
  1333. )
  1334. ignored_event_ids = [channel.json_body["event_id"]]
  1335. before_aggregations, after_aggregations = self._test_ignored_user(
  1336. RelationTypes.REFERENCE, allowed_event_ids, ignored_event_ids
  1337. )
  1338. self.assertCountEqual(
  1339. [e["event_id"] for e in before_aggregations["chunk"]],
  1340. allowed_event_ids + ignored_event_ids,
  1341. )
  1342. self.assertCountEqual(
  1343. [e["event_id"] for e in after_aggregations["chunk"]], allowed_event_ids
  1344. )
  1345. def test_thread(self) -> None:
  1346. """Aggregations should exclude thread releations from ignored users"""
  1347. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1348. allowed_event_ids = [channel.json_body["event_id"]]
  1349. channel = self._send_relation(
  1350. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1351. )
  1352. ignored_event_ids = [channel.json_body["event_id"]]
  1353. before_aggregations, after_aggregations = self._test_ignored_user(
  1354. RelationTypes.THREAD, allowed_event_ids, ignored_event_ids
  1355. )
  1356. self.assertEqual(before_aggregations["count"], 2)
  1357. self.assertTrue(before_aggregations["current_user_participated"])
  1358. # The latest thread event has some fields that don't matter.
  1359. self.assertEqual(
  1360. before_aggregations["latest_event"]["event_id"], ignored_event_ids[0]
  1361. )
  1362. self.assertEqual(after_aggregations["count"], 1)
  1363. self.assertTrue(after_aggregations["current_user_participated"])
  1364. # The latest thread event has some fields that don't matter.
  1365. self.assertEqual(
  1366. after_aggregations["latest_event"]["event_id"], allowed_event_ids[0]
  1367. )
  1368. class RelationRedactionTestCase(BaseRelationsTestCase):
  1369. """
  1370. Test the behaviour of relations when the parent or child event is redacted.
  1371. The behaviour of each relation type is subtly different which causes the tests
  1372. to be a bit repetitive, they follow a naming scheme of:
  1373. test_redact_(relation|parent)_{relation_type}
  1374. The first bit of "relation" means that the event with the relation defined
  1375. on it (the child event) is to be redacted. A "parent" means that the target
  1376. of the relation (the parent event) is to be redacted.
  1377. The relation_type describes which type of relation is under test (i.e. it is
  1378. related to the value of rel_type in the event content).
  1379. """
  1380. def _redact(self, event_id: str) -> None:
  1381. channel = self.make_request(
  1382. "POST",
  1383. f"/_matrix/client/r0/rooms/{self.room}/redact/{event_id}",
  1384. access_token=self.user_token,
  1385. content={},
  1386. )
  1387. self.assertEqual(200, channel.code, channel.json_body)
  1388. def _get_threads(self) -> List[Tuple[str, str]]:
  1389. """Request the threads in the room and returns a list of thread ID and latest event ID."""
  1390. # Request the threads in the room.
  1391. channel = self.make_request(
  1392. "GET",
  1393. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1394. access_token=self.user_token,
  1395. )
  1396. self.assertEqual(200, channel.code, channel.json_body)
  1397. threads = channel.json_body["chunk"]
  1398. return [
  1399. (
  1400. t["event_id"],
  1401. t["unsigned"]["m.relations"][RelationTypes.THREAD]["latest_event"][
  1402. "event_id"
  1403. ],
  1404. )
  1405. for t in threads
  1406. ]
  1407. def test_redact_relation_thread(self) -> None:
  1408. """
  1409. Test that thread replies are properly handled after the thread reply redacted.
  1410. The redacted event should not be included in bundled aggregations or
  1411. the response to relations.
  1412. """
  1413. # Create a thread with a few events in it.
  1414. thread_replies = []
  1415. for i in range(3):
  1416. channel = self._send_relation(
  1417. RelationTypes.THREAD,
  1418. EventTypes.Message,
  1419. content={"body": f"reply {i}", "msgtype": "m.text"},
  1420. )
  1421. thread_replies.append(channel.json_body["event_id"])
  1422. ##################################################
  1423. # Check the test data is configured as expected. #
  1424. ##################################################
  1425. self.assertEqual(self._get_related_events(), list(reversed(thread_replies)))
  1426. relations = self._get_bundled_aggregations()
  1427. self.assertDictContainsSubset(
  1428. {"count": 3, "current_user_participated": True},
  1429. relations[RelationTypes.THREAD],
  1430. )
  1431. # The latest event is the last sent event.
  1432. self.assertEqual(
  1433. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1434. thread_replies[-1],
  1435. )
  1436. # There should be one thread, the latest event is the event that will be redacted.
  1437. self.assertEqual(self._get_threads(), [(self.parent_id, thread_replies[-1])])
  1438. ##########################
  1439. # Redact the last event. #
  1440. ##########################
  1441. self._redact(thread_replies.pop())
  1442. # The thread should still exist, but the latest event should be updated.
  1443. self.assertEqual(self._get_related_events(), list(reversed(thread_replies)))
  1444. relations = self._get_bundled_aggregations()
  1445. self.assertDictContainsSubset(
  1446. {"count": 2, "current_user_participated": True},
  1447. relations[RelationTypes.THREAD],
  1448. )
  1449. # And the latest event is the last unredacted event.
  1450. self.assertEqual(
  1451. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1452. thread_replies[-1],
  1453. )
  1454. self.assertEqual(self._get_threads(), [(self.parent_id, thread_replies[-1])])
  1455. ###########################################
  1456. # Redact the *first* event in the thread. #
  1457. ###########################################
  1458. self._redact(thread_replies.pop(0))
  1459. # Nothing should have changed (except the thread count).
  1460. self.assertEqual(self._get_related_events(), thread_replies)
  1461. relations = self._get_bundled_aggregations()
  1462. self.assertDictContainsSubset(
  1463. {"count": 1, "current_user_participated": True},
  1464. relations[RelationTypes.THREAD],
  1465. )
  1466. # And the latest event is the last unredacted event.
  1467. self.assertEqual(
  1468. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1469. thread_replies[-1],
  1470. )
  1471. self.assertEqual(self._get_threads(), [(self.parent_id, thread_replies[-1])])
  1472. ####################################
  1473. # Redact the last remaining event. #
  1474. ####################################
  1475. self._redact(thread_replies.pop(0))
  1476. self.assertEqual(thread_replies, [])
  1477. # The event should no longer be considered a thread.
  1478. self.assertEqual(self._get_related_events(), [])
  1479. self.assertEqual(self._get_bundled_aggregations(), {})
  1480. self.assertEqual(self._get_threads(), [])
  1481. def test_redact_parent_edit(self) -> None:
  1482. """Test that edits of an event are redacted when the original event
  1483. is redacted.
  1484. """
  1485. # Add a relation
  1486. self._send_relation(
  1487. RelationTypes.REPLACE,
  1488. "m.room.message",
  1489. parent_id=self.parent_id,
  1490. content={
  1491. "msgtype": "m.text",
  1492. "body": "Wibble",
  1493. "m.new_content": {"msgtype": "m.text", "body": "First edit"},
  1494. },
  1495. )
  1496. # Check the relation is returned
  1497. event_ids = self._get_related_events()
  1498. relations = self._get_bundled_aggregations()
  1499. self.assertEqual(len(event_ids), 1)
  1500. self.assertIn(RelationTypes.REPLACE, relations)
  1501. # Redact the original event
  1502. self._redact(self.parent_id)
  1503. # The relations are not returned.
  1504. event_ids = self._get_related_events()
  1505. relations = self._get_bundled_aggregations()
  1506. self.assertEqual(len(event_ids), 0)
  1507. self.assertEqual(relations, {})
  1508. def test_redact_parent_annotation(self) -> None:
  1509. """Test that annotations of an event are viewable when the original event
  1510. is redacted.
  1511. """
  1512. # Add a relation
  1513. channel = self._send_relation(RelationTypes.REFERENCE, "org.matrix.test")
  1514. related_event_id = channel.json_body["event_id"]
  1515. # The relations should exist.
  1516. event_ids = self._get_related_events()
  1517. relations = self._get_bundled_aggregations()
  1518. self.assertEqual(len(event_ids), 1)
  1519. self.assertIn(RelationTypes.REFERENCE, relations)
  1520. # Redact the original event.
  1521. self._redact(self.parent_id)
  1522. # The relations are returned.
  1523. event_ids = self._get_related_events()
  1524. relations = self._get_bundled_aggregations()
  1525. self.assertEqual(event_ids, [related_event_id])
  1526. self.assertEqual(
  1527. relations[RelationTypes.REFERENCE],
  1528. {"chunk": [{"event_id": related_event_id}]},
  1529. )
  1530. def test_redact_parent_thread(self) -> None:
  1531. """
  1532. Test that thread replies are still available when the root event is redacted.
  1533. """
  1534. channel = self._send_relation(
  1535. RelationTypes.THREAD,
  1536. EventTypes.Message,
  1537. content={"body": "reply 1", "msgtype": "m.text"},
  1538. )
  1539. related_event_id = channel.json_body["event_id"]
  1540. # Redact one of the reactions.
  1541. self._redact(self.parent_id)
  1542. # The unredacted relation should still exist.
  1543. event_ids = self._get_related_events()
  1544. relations = self._get_bundled_aggregations()
  1545. self.assertEqual(len(event_ids), 1)
  1546. self.assertDictContainsSubset(
  1547. {
  1548. "count": 1,
  1549. "current_user_participated": True,
  1550. },
  1551. relations[RelationTypes.THREAD],
  1552. )
  1553. self.assertEqual(
  1554. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1555. related_event_id,
  1556. )
  1557. class ThreadsTestCase(BaseRelationsTestCase):
  1558. def _get_threads(self, body: JsonDict) -> List[Tuple[str, str]]:
  1559. return [
  1560. (
  1561. ev["event_id"],
  1562. ev["unsigned"]["m.relations"]["m.thread"]["latest_event"]["event_id"],
  1563. )
  1564. for ev in body["chunk"]
  1565. ]
  1566. def test_threads(self) -> None:
  1567. """Create threads and ensure the ordering is due to their latest event."""
  1568. # Create 2 threads.
  1569. thread_1 = self.parent_id
  1570. res = self.helper.send(self.room, body="Thread Root!", tok=self.user_token)
  1571. thread_2 = res["event_id"]
  1572. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1573. reply_1 = channel.json_body["event_id"]
  1574. channel = self._send_relation(
  1575. RelationTypes.THREAD, "m.room.test", parent_id=thread_2
  1576. )
  1577. reply_2 = channel.json_body["event_id"]
  1578. # Request the threads in the room.
  1579. channel = self.make_request(
  1580. "GET",
  1581. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1582. access_token=self.user_token,
  1583. )
  1584. self.assertEqual(200, channel.code, channel.json_body)
  1585. threads = self._get_threads(channel.json_body)
  1586. self.assertEqual(threads, [(thread_2, reply_2), (thread_1, reply_1)])
  1587. # Update the first thread, the ordering should swap.
  1588. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1589. reply_3 = channel.json_body["event_id"]
  1590. channel = self.make_request(
  1591. "GET",
  1592. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1593. access_token=self.user_token,
  1594. )
  1595. self.assertEqual(200, channel.code, channel.json_body)
  1596. # Tuple of (thread ID, latest event ID) for each thread.
  1597. threads = self._get_threads(channel.json_body)
  1598. self.assertEqual(threads, [(thread_1, reply_3), (thread_2, reply_2)])
  1599. def test_pagination(self) -> None:
  1600. """Create threads and paginate through them."""
  1601. # Create 2 threads.
  1602. thread_1 = self.parent_id
  1603. res = self.helper.send(self.room, body="Thread Root!", tok=self.user_token)
  1604. thread_2 = res["event_id"]
  1605. self._send_relation(RelationTypes.THREAD, "m.room.test")
  1606. self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
  1607. # Request the threads in the room.
  1608. channel = self.make_request(
  1609. "GET",
  1610. f"/_matrix/client/v1/rooms/{self.room}/threads?limit=1",
  1611. access_token=self.user_token,
  1612. )
  1613. self.assertEqual(200, channel.code, channel.json_body)
  1614. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1615. self.assertEqual(thread_roots, [thread_2])
  1616. # Make sure next_batch has something in it that looks like it could be a
  1617. # valid token.
  1618. next_batch = channel.json_body.get("next_batch")
  1619. self.assertIsInstance(next_batch, str, channel.json_body)
  1620. channel = self.make_request(
  1621. "GET",
  1622. f"/_matrix/client/v1/rooms/{self.room}/threads?limit=1&from={next_batch}",
  1623. access_token=self.user_token,
  1624. )
  1625. self.assertEqual(200, channel.code, channel.json_body)
  1626. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1627. self.assertEqual(thread_roots, [thread_1], channel.json_body)
  1628. self.assertNotIn("next_batch", channel.json_body, channel.json_body)
  1629. def test_include(self) -> None:
  1630. """Filtering threads to all or participated in should work."""
  1631. # Thread 1 has the user as the root event.
  1632. thread_1 = self.parent_id
  1633. self._send_relation(
  1634. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1635. )
  1636. # Thread 2 has the user replying.
  1637. res = self.helper.send(self.room, body="Thread Root!", tok=self.user2_token)
  1638. thread_2 = res["event_id"]
  1639. self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
  1640. # Thread 3 has the user not participating in.
  1641. res = self.helper.send(self.room, body="Another thread!", tok=self.user2_token)
  1642. thread_3 = res["event_id"]
  1643. self._send_relation(
  1644. RelationTypes.THREAD,
  1645. "m.room.test",
  1646. access_token=self.user2_token,
  1647. parent_id=thread_3,
  1648. )
  1649. # All threads in the room.
  1650. channel = self.make_request(
  1651. "GET",
  1652. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1653. access_token=self.user_token,
  1654. )
  1655. self.assertEqual(200, channel.code, channel.json_body)
  1656. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1657. self.assertEqual(
  1658. thread_roots, [thread_3, thread_2, thread_1], channel.json_body
  1659. )
  1660. # Only participated threads.
  1661. channel = self.make_request(
  1662. "GET",
  1663. f"/_matrix/client/v1/rooms/{self.room}/threads?include=participated",
  1664. access_token=self.user_token,
  1665. )
  1666. self.assertEqual(200, channel.code, channel.json_body)
  1667. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1668. self.assertEqual(thread_roots, [thread_2, thread_1], channel.json_body)
  1669. def test_ignored_user(self) -> None:
  1670. """Events from ignored users should be ignored."""
  1671. # Thread 1 has a reply from an ignored user.
  1672. thread_1 = self.parent_id
  1673. self._send_relation(
  1674. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1675. )
  1676. # Thread 2 is created by an ignored user.
  1677. res = self.helper.send(self.room, body="Thread Root!", tok=self.user2_token)
  1678. thread_2 = res["event_id"]
  1679. self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
  1680. # Ignore user2.
  1681. self.get_success(
  1682. self.store.add_account_data_for_user(
  1683. self.user_id,
  1684. AccountDataTypes.IGNORED_USER_LIST,
  1685. {"ignored_users": {self.user2_id: {}}},
  1686. )
  1687. )
  1688. # Only thread 1 is returned.
  1689. channel = self.make_request(
  1690. "GET",
  1691. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1692. access_token=self.user_token,
  1693. )
  1694. self.assertEqual(200, channel.code, channel.json_body)
  1695. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1696. self.assertEqual(thread_roots, [thread_1], channel.json_body)