test_relations.py 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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 AsyncMock, 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.event_injection import inject_event
  28. from tests.unittest import override_config
  29. class BaseRelationsTestCase(unittest.HomeserverTestCase):
  30. servlets = [
  31. relations.register_servlets,
  32. room.register_servlets,
  33. sync.register_servlets,
  34. login.register_servlets,
  35. register.register_servlets,
  36. admin.register_servlets_for_client_rest_resource,
  37. ]
  38. hijack_auth = False
  39. def default_config(self) -> Dict[str, Any]:
  40. # We need to enable msc1849 support for aggregations
  41. config = super().default_config()
  42. # We enable frozen dicts as relations/edits change event contents, so we
  43. # want to test that we don't modify the events in the caches.
  44. config["use_frozen_dicts"] = True
  45. return config
  46. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  47. self.store = hs.get_datastores().main
  48. self.user_id, self.user_token = self._create_user("alice")
  49. self.user2_id, self.user2_token = self._create_user("bob")
  50. self.room = self.helper.create_room_as(self.user_id, tok=self.user_token)
  51. self.helper.join(self.room, user=self.user2_id, tok=self.user2_token)
  52. res = self.helper.send(self.room, body="Hi!", tok=self.user_token)
  53. self.parent_id = res["event_id"]
  54. def _create_user(self, localpart: str) -> Tuple[str, str]:
  55. user_id = self.register_user(localpart, "abc123")
  56. access_token = self.login(localpart, "abc123")
  57. return user_id, access_token
  58. def _send_relation(
  59. self,
  60. relation_type: str,
  61. event_type: str,
  62. key: Optional[str] = None,
  63. content: Optional[dict] = None,
  64. access_token: Optional[str] = None,
  65. parent_id: Optional[str] = None,
  66. expected_response_code: int = 200,
  67. ) -> FakeChannel:
  68. """Helper function to send a relation pointing at `self.parent_id`
  69. Args:
  70. relation_type: One of `RelationTypes`
  71. event_type: The type of the event to create
  72. key: The aggregation key used for m.annotation relation type.
  73. content: The content of the created event. Will be modified to configure
  74. the m.relates_to key based on the other provided parameters.
  75. access_token: The access token used to send the relation, defaults
  76. to `self.user_token`
  77. parent_id: The event_id this relation relates to. If None, then self.parent_id
  78. Returns:
  79. FakeChannel
  80. """
  81. if not access_token:
  82. access_token = self.user_token
  83. original_id = parent_id if parent_id else self.parent_id
  84. if content is None:
  85. content = {}
  86. content["m.relates_to"] = {
  87. "event_id": original_id,
  88. "rel_type": relation_type,
  89. }
  90. if key is not None:
  91. content["m.relates_to"]["key"] = key
  92. channel = self.make_request(
  93. "POST",
  94. f"/_matrix/client/v3/rooms/{self.room}/send/{event_type}",
  95. content,
  96. access_token=access_token,
  97. )
  98. self.assertEqual(expected_response_code, channel.code, channel.json_body)
  99. return channel
  100. def _get_related_events(self) -> List[str]:
  101. """
  102. Requests /relations on the parent ID and returns a list of event IDs.
  103. """
  104. # Request the relations of the event.
  105. channel = self.make_request(
  106. "GET",
  107. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}",
  108. access_token=self.user_token,
  109. )
  110. self.assertEqual(200, channel.code, channel.json_body)
  111. return [ev["event_id"] for ev in channel.json_body["chunk"]]
  112. def _get_bundled_aggregations(self) -> JsonDict:
  113. """
  114. Requests /event on the parent ID and returns the m.relations field (from unsigned), if it exists.
  115. """
  116. # Fetch the bundled aggregations of the event.
  117. channel = self.make_request(
  118. "GET",
  119. f"/_matrix/client/v3/rooms/{self.room}/event/{self.parent_id}",
  120. access_token=self.user_token,
  121. )
  122. self.assertEqual(200, channel.code, channel.json_body)
  123. return channel.json_body["unsigned"].get("m.relations", {})
  124. def _find_event_in_chunk(self, events: List[JsonDict]) -> JsonDict:
  125. """
  126. Find the parent event in a chunk of events and assert that it has the proper bundled aggregations.
  127. """
  128. for event in events:
  129. if event["event_id"] == self.parent_id:
  130. return event
  131. raise AssertionError(f"Event {self.parent_id} not found in chunk")
  132. class RelationsTestCase(BaseRelationsTestCase):
  133. def test_send_relation(self) -> None:
  134. """Tests that sending a relation works."""
  135. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
  136. event_id = channel.json_body["event_id"]
  137. channel = self.make_request(
  138. "GET",
  139. f"/rooms/{self.room}/event/{event_id}",
  140. access_token=self.user_token,
  141. )
  142. self.assertEqual(200, channel.code, channel.json_body)
  143. self.assert_dict(
  144. {
  145. "type": "m.reaction",
  146. "sender": self.user_id,
  147. "content": {
  148. "m.relates_to": {
  149. "event_id": self.parent_id,
  150. "key": "👍",
  151. "rel_type": RelationTypes.ANNOTATION,
  152. }
  153. },
  154. },
  155. channel.json_body,
  156. )
  157. def test_deny_invalid_event(self) -> None:
  158. """Test that we deny relations on non-existant events"""
  159. self._send_relation(
  160. RelationTypes.ANNOTATION,
  161. EventTypes.Message,
  162. parent_id="foo",
  163. content={"body": "foo", "msgtype": "m.text"},
  164. expected_response_code=400,
  165. )
  166. # Unless that event is referenced from another event!
  167. self.get_success(
  168. self.hs.get_datastores().main.db_pool.simple_insert(
  169. table="event_relations",
  170. values={
  171. "event_id": "bar",
  172. "relates_to_id": "foo",
  173. "relation_type": RelationTypes.THREAD,
  174. },
  175. desc="test_deny_invalid_event",
  176. )
  177. )
  178. self._send_relation(
  179. RelationTypes.THREAD,
  180. EventTypes.Message,
  181. parent_id="foo",
  182. content={"body": "foo", "msgtype": "m.text"},
  183. )
  184. def test_deny_invalid_room(self) -> None:
  185. """Test that we deny relations on non-existant events"""
  186. # Create another room and send a message in it.
  187. room2 = self.helper.create_room_as(self.user_id, tok=self.user_token)
  188. res = self.helper.send(room2, body="Hi!", tok=self.user_token)
  189. parent_id = res["event_id"]
  190. # Attempt to send an annotation to that event.
  191. self._send_relation(
  192. RelationTypes.ANNOTATION,
  193. "m.reaction",
  194. parent_id=parent_id,
  195. key="A",
  196. expected_response_code=400,
  197. )
  198. def test_deny_double_react(self) -> None:
  199. """Test that we deny relations on membership events"""
  200. self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
  201. self._send_relation(
  202. RelationTypes.ANNOTATION, "m.reaction", "a", expected_response_code=400
  203. )
  204. def test_deny_forked_thread(self) -> None:
  205. """It is invalid to start a thread off a thread."""
  206. channel = self._send_relation(
  207. RelationTypes.THREAD,
  208. "m.room.message",
  209. content={"msgtype": "m.text", "body": "foo"},
  210. parent_id=self.parent_id,
  211. )
  212. parent_id = channel.json_body["event_id"]
  213. self._send_relation(
  214. RelationTypes.THREAD,
  215. "m.room.message",
  216. content={"msgtype": "m.text", "body": "foo"},
  217. parent_id=parent_id,
  218. expected_response_code=400,
  219. )
  220. def test_ignore_invalid_room(self) -> None:
  221. """Test that we ignore invalid relations over federation."""
  222. # Create another room and send a message in it.
  223. room2 = self.helper.create_room_as(self.user_id, tok=self.user_token)
  224. res = self.helper.send(room2, body="Hi!", tok=self.user_token)
  225. parent_id = res["event_id"]
  226. # Disable the validation to pretend this came over federation.
  227. with patch(
  228. "synapse.handlers.message.EventCreationHandler._validate_event_relation",
  229. new_callable=AsyncMock,
  230. return_value=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.assertLessEqual(original_body.items(), event_result["content"].items())
  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.assertLessEqual(
  527. {
  528. "m.relates_to": {
  529. "event_id": self.parent_id,
  530. "rel_type": "m.reference",
  531. }
  532. }.items(),
  533. result_event_dict["content"].items(),
  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_callable=AsyncMock,
  1145. return_value=None,
  1146. ):
  1147. # Create a sub-thread off the thread, which is not allowed.
  1148. self._send_relation(
  1149. RelationTypes.THREAD, "m.room.test", parent_id=reply_event_id
  1150. )
  1151. # Fetch the thread root, to get the bundled aggregation for the thread.
  1152. relations_from_event = self._get_bundled_aggregations()
  1153. # Ensure that requesting the room messages also does not return the sub-thread.
  1154. channel = self.make_request(
  1155. "GET",
  1156. f"/rooms/{self.room}/messages?dir=b",
  1157. access_token=self.user_token,
  1158. )
  1159. self.assertEqual(200, channel.code, channel.json_body)
  1160. event = self._find_event_in_chunk(channel.json_body["chunk"])
  1161. relations_from_messages = event["unsigned"]["m.relations"]
  1162. # Check the bundled aggregations from each point.
  1163. for aggregations, desc in (
  1164. (relations_from_event, "/event"),
  1165. (relations_from_messages, "/messages"),
  1166. ):
  1167. # The latest event should have bundled aggregations.
  1168. self.assertIn(RelationTypes.THREAD, aggregations, desc)
  1169. thread_summary = aggregations[RelationTypes.THREAD]
  1170. self.assertIn("latest_event", thread_summary, desc)
  1171. self.assertEqual(
  1172. thread_summary["latest_event"]["event_id"], reply_event_id, desc
  1173. )
  1174. # The latest event should not have any bundled aggregations (since the
  1175. # only relation to it is another thread, which is invalid).
  1176. self.assertNotIn(
  1177. "m.relations", thread_summary["latest_event"]["unsigned"], desc
  1178. )
  1179. def test_thread_edit_latest_event(self) -> None:
  1180. """Test that editing the latest event in a thread works."""
  1181. # Create a thread and edit the last event.
  1182. channel = self._send_relation(
  1183. RelationTypes.THREAD,
  1184. "m.room.message",
  1185. content={"msgtype": "m.text", "body": "A threaded reply!"},
  1186. )
  1187. threaded_event_id = channel.json_body["event_id"]
  1188. new_body = {"msgtype": "m.text", "body": "I've been edited!"}
  1189. channel = self._send_relation(
  1190. RelationTypes.REPLACE,
  1191. "m.room.message",
  1192. content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
  1193. parent_id=threaded_event_id,
  1194. )
  1195. edit_event_id = channel.json_body["event_id"]
  1196. # Fetch the thread root, to get the bundled aggregation for the thread.
  1197. relations_dict = self._get_bundled_aggregations()
  1198. # We expect that the edit message appears in the thread summary in the
  1199. # unsigned relations section.
  1200. self.assertIn(RelationTypes.THREAD, relations_dict)
  1201. thread_summary = relations_dict[RelationTypes.THREAD]
  1202. self.assertIn("latest_event", thread_summary)
  1203. latest_event_in_thread = thread_summary["latest_event"]
  1204. # The latest event in the thread should have the edit appear under the
  1205. # bundled aggregations.
  1206. self.assertLessEqual(
  1207. {"event_id": edit_event_id, "sender": "@alice:test"}.items(),
  1208. latest_event_in_thread["unsigned"]["m.relations"][
  1209. RelationTypes.REPLACE
  1210. ].items(),
  1211. )
  1212. def test_aggregation_get_event_for_annotation(self) -> None:
  1213. """Test that annotations do not get bundled aggregations included
  1214. when directly requested.
  1215. """
  1216. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  1217. annotation_id = channel.json_body["event_id"]
  1218. # Annotate the annotation.
  1219. self._send_relation(
  1220. RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=annotation_id
  1221. )
  1222. channel = self.make_request(
  1223. "GET",
  1224. f"/rooms/{self.room}/event/{annotation_id}",
  1225. access_token=self.user_token,
  1226. )
  1227. self.assertEqual(200, channel.code, channel.json_body)
  1228. self.assertIsNone(channel.json_body["unsigned"].get("m.relations"))
  1229. def test_aggregation_get_event_for_thread(self) -> None:
  1230. """Test that threads get bundled aggregations included when directly requested."""
  1231. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1232. thread_id = channel.json_body["event_id"]
  1233. # Make a reference to the thread.
  1234. channel = self._send_relation(
  1235. RelationTypes.REFERENCE, "org.matrix.test", parent_id=thread_id
  1236. )
  1237. reference_event_id = channel.json_body["event_id"]
  1238. channel = self.make_request(
  1239. "GET",
  1240. f"/rooms/{self.room}/event/{thread_id}",
  1241. access_token=self.user_token,
  1242. )
  1243. self.assertEqual(200, channel.code, channel.json_body)
  1244. self.assertEqual(
  1245. channel.json_body["unsigned"].get("m.relations"),
  1246. {
  1247. RelationTypes.REFERENCE: {"chunk": [{"event_id": reference_event_id}]},
  1248. },
  1249. )
  1250. # It should also be included when the entire thread is requested.
  1251. channel = self.make_request(
  1252. "GET",
  1253. f"/_matrix/client/v1/rooms/{self.room}/relations/{self.parent_id}?limit=1",
  1254. access_token=self.user_token,
  1255. )
  1256. self.assertEqual(200, channel.code, channel.json_body)
  1257. self.assertEqual(len(channel.json_body["chunk"]), 1)
  1258. thread_message = channel.json_body["chunk"][0]
  1259. self.assertEqual(
  1260. thread_message["unsigned"].get("m.relations"),
  1261. {
  1262. RelationTypes.REFERENCE: {"chunk": [{"event_id": reference_event_id}]},
  1263. },
  1264. )
  1265. def test_bundled_aggregations_with_filter(self) -> None:
  1266. """
  1267. If "unsigned" is an omitted field (due to filtering), adding the bundled
  1268. aggregations should not break.
  1269. Note that the spec allows for a server to return additional fields beyond
  1270. what is specified.
  1271. """
  1272. channel = self._send_relation(RelationTypes.REFERENCE, "org.matrix.test")
  1273. reference_event_id = channel.json_body["event_id"]
  1274. # Note that the sync filter does not include "unsigned" as a field.
  1275. filter = urllib.parse.quote_plus(
  1276. b'{"event_fields": ["content", "event_id"], "room": {"timeline": {"limit": 3}}}'
  1277. )
  1278. channel = self.make_request(
  1279. "GET", f"/sync?filter={filter}", access_token=self.user_token
  1280. )
  1281. self.assertEqual(200, channel.code, channel.json_body)
  1282. # Ensure the timeline is limited, find the parent event.
  1283. room_timeline = channel.json_body["rooms"]["join"][self.room]["timeline"]
  1284. self.assertTrue(room_timeline["limited"])
  1285. parent_event = self._find_event_in_chunk(room_timeline["events"])
  1286. # Ensure there's bundled aggregations on it.
  1287. self.assertIn("unsigned", parent_event)
  1288. self.assertEqual(
  1289. parent_event["unsigned"].get("m.relations"),
  1290. {
  1291. RelationTypes.REFERENCE: {"chunk": [{"event_id": reference_event_id}]},
  1292. },
  1293. )
  1294. class RelationIgnoredUserTestCase(BaseRelationsTestCase):
  1295. """Relations sent from an ignored user should be ignored."""
  1296. def _test_ignored_user(
  1297. self,
  1298. relation_type: str,
  1299. allowed_event_ids: List[str],
  1300. ignored_event_ids: List[str],
  1301. ) -> Tuple[JsonDict, JsonDict]:
  1302. """
  1303. Fetch the relations and ensure they're all there, then ignore user2, and
  1304. repeat.
  1305. Returns:
  1306. A tuple of two JSON dictionaries, each are bundled aggregations, the
  1307. first is from before the user is ignored, and the second is after.
  1308. """
  1309. # Get the relations.
  1310. event_ids = self._get_related_events()
  1311. self.assertCountEqual(event_ids, allowed_event_ids + ignored_event_ids)
  1312. # And the bundled aggregations.
  1313. before_aggregations = self._get_bundled_aggregations()
  1314. self.assertIn(relation_type, before_aggregations)
  1315. # Ignore user2 and re-do the requests.
  1316. self.get_success(
  1317. self.store.add_account_data_for_user(
  1318. self.user_id,
  1319. AccountDataTypes.IGNORED_USER_LIST,
  1320. {"ignored_users": {self.user2_id: {}}},
  1321. )
  1322. )
  1323. # Get the relations.
  1324. event_ids = self._get_related_events()
  1325. self.assertCountEqual(event_ids, allowed_event_ids)
  1326. # And the bundled aggregations.
  1327. after_aggregations = self._get_bundled_aggregations()
  1328. self.assertIn(relation_type, after_aggregations)
  1329. return before_aggregations[relation_type], after_aggregations[relation_type]
  1330. def test_reference(self) -> None:
  1331. """Aggregations should exclude reference relations from ignored users"""
  1332. channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
  1333. allowed_event_ids = [channel.json_body["event_id"]]
  1334. channel = self._send_relation(
  1335. RelationTypes.REFERENCE, "m.room.test", access_token=self.user2_token
  1336. )
  1337. ignored_event_ids = [channel.json_body["event_id"]]
  1338. before_aggregations, after_aggregations = self._test_ignored_user(
  1339. RelationTypes.REFERENCE, allowed_event_ids, ignored_event_ids
  1340. )
  1341. self.assertCountEqual(
  1342. [e["event_id"] for e in before_aggregations["chunk"]],
  1343. allowed_event_ids + ignored_event_ids,
  1344. )
  1345. self.assertCountEqual(
  1346. [e["event_id"] for e in after_aggregations["chunk"]], allowed_event_ids
  1347. )
  1348. def test_thread(self) -> None:
  1349. """Aggregations should exclude thread releations from ignored users"""
  1350. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1351. allowed_event_ids = [channel.json_body["event_id"]]
  1352. channel = self._send_relation(
  1353. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1354. )
  1355. ignored_event_ids = [channel.json_body["event_id"]]
  1356. before_aggregations, after_aggregations = self._test_ignored_user(
  1357. RelationTypes.THREAD, allowed_event_ids, ignored_event_ids
  1358. )
  1359. self.assertEqual(before_aggregations["count"], 2)
  1360. self.assertTrue(before_aggregations["current_user_participated"])
  1361. # The latest thread event has some fields that don't matter.
  1362. self.assertEqual(
  1363. before_aggregations["latest_event"]["event_id"], ignored_event_ids[0]
  1364. )
  1365. self.assertEqual(after_aggregations["count"], 1)
  1366. self.assertTrue(after_aggregations["current_user_participated"])
  1367. # The latest thread event has some fields that don't matter.
  1368. self.assertEqual(
  1369. after_aggregations["latest_event"]["event_id"], allowed_event_ids[0]
  1370. )
  1371. class RelationRedactionTestCase(BaseRelationsTestCase):
  1372. """
  1373. Test the behaviour of relations when the parent or child event is redacted.
  1374. The behaviour of each relation type is subtly different which causes the tests
  1375. to be a bit repetitive, they follow a naming scheme of:
  1376. test_redact_(relation|parent)_{relation_type}
  1377. The first bit of "relation" means that the event with the relation defined
  1378. on it (the child event) is to be redacted. A "parent" means that the target
  1379. of the relation (the parent event) is to be redacted.
  1380. The relation_type describes which type of relation is under test (i.e. it is
  1381. related to the value of rel_type in the event content).
  1382. """
  1383. def _redact(self, event_id: str) -> None:
  1384. channel = self.make_request(
  1385. "POST",
  1386. f"/_matrix/client/r0/rooms/{self.room}/redact/{event_id}",
  1387. access_token=self.user_token,
  1388. content={},
  1389. )
  1390. self.assertEqual(200, channel.code, channel.json_body)
  1391. def _get_threads(self) -> List[Tuple[str, str]]:
  1392. """Request the threads in the room and returns a list of thread ID and latest event ID."""
  1393. # Request the threads in the room.
  1394. channel = self.make_request(
  1395. "GET",
  1396. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1397. access_token=self.user_token,
  1398. )
  1399. self.assertEqual(200, channel.code, channel.json_body)
  1400. threads = channel.json_body["chunk"]
  1401. return [
  1402. (
  1403. t["event_id"],
  1404. t["unsigned"]["m.relations"][RelationTypes.THREAD]["latest_event"][
  1405. "event_id"
  1406. ],
  1407. )
  1408. for t in threads
  1409. ]
  1410. def test_redact_relation_thread(self) -> None:
  1411. """
  1412. Test that thread replies are properly handled after the thread reply redacted.
  1413. The redacted event should not be included in bundled aggregations or
  1414. the response to relations.
  1415. """
  1416. # Create a thread with a few events in it.
  1417. thread_replies = []
  1418. for i in range(3):
  1419. channel = self._send_relation(
  1420. RelationTypes.THREAD,
  1421. EventTypes.Message,
  1422. content={"body": f"reply {i}", "msgtype": "m.text"},
  1423. )
  1424. thread_replies.append(channel.json_body["event_id"])
  1425. ##################################################
  1426. # Check the test data is configured as expected. #
  1427. ##################################################
  1428. self.assertEqual(self._get_related_events(), list(reversed(thread_replies)))
  1429. relations = self._get_bundled_aggregations()
  1430. self.assertLessEqual(
  1431. {"count": 3, "current_user_participated": True}.items(),
  1432. relations[RelationTypes.THREAD].items(),
  1433. )
  1434. # The latest event is the last sent event.
  1435. self.assertEqual(
  1436. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1437. thread_replies[-1],
  1438. )
  1439. # There should be one thread, the latest event is the event that will be redacted.
  1440. self.assertEqual(self._get_threads(), [(self.parent_id, thread_replies[-1])])
  1441. ##########################
  1442. # Redact the last event. #
  1443. ##########################
  1444. self._redact(thread_replies.pop())
  1445. # The thread should still exist, but the latest event should be updated.
  1446. self.assertEqual(self._get_related_events(), list(reversed(thread_replies)))
  1447. relations = self._get_bundled_aggregations()
  1448. self.assertLessEqual(
  1449. {"count": 2, "current_user_participated": True}.items(),
  1450. relations[RelationTypes.THREAD].items(),
  1451. )
  1452. # And the latest event is the last unredacted event.
  1453. self.assertEqual(
  1454. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1455. thread_replies[-1],
  1456. )
  1457. self.assertEqual(self._get_threads(), [(self.parent_id, thread_replies[-1])])
  1458. ###########################################
  1459. # Redact the *first* event in the thread. #
  1460. ###########################################
  1461. self._redact(thread_replies.pop(0))
  1462. # Nothing should have changed (except the thread count).
  1463. self.assertEqual(self._get_related_events(), thread_replies)
  1464. relations = self._get_bundled_aggregations()
  1465. self.assertLessEqual(
  1466. {"count": 1, "current_user_participated": True}.items(),
  1467. relations[RelationTypes.THREAD].items(),
  1468. )
  1469. # And the latest event is the last unredacted event.
  1470. self.assertEqual(
  1471. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1472. thread_replies[-1],
  1473. )
  1474. self.assertEqual(self._get_threads(), [(self.parent_id, thread_replies[-1])])
  1475. ####################################
  1476. # Redact the last remaining event. #
  1477. ####################################
  1478. self._redact(thread_replies.pop(0))
  1479. self.assertEqual(thread_replies, [])
  1480. # The event should no longer be considered a thread.
  1481. self.assertEqual(self._get_related_events(), [])
  1482. self.assertEqual(self._get_bundled_aggregations(), {})
  1483. self.assertEqual(self._get_threads(), [])
  1484. def test_redact_parent_edit(self) -> None:
  1485. """Test that edits of an event are redacted when the original event
  1486. is redacted.
  1487. """
  1488. # Add a relation
  1489. self._send_relation(
  1490. RelationTypes.REPLACE,
  1491. "m.room.message",
  1492. parent_id=self.parent_id,
  1493. content={
  1494. "msgtype": "m.text",
  1495. "body": "Wibble",
  1496. "m.new_content": {"msgtype": "m.text", "body": "First edit"},
  1497. },
  1498. )
  1499. # Check the relation is returned
  1500. event_ids = self._get_related_events()
  1501. relations = self._get_bundled_aggregations()
  1502. self.assertEqual(len(event_ids), 1)
  1503. self.assertIn(RelationTypes.REPLACE, relations)
  1504. # Redact the original event
  1505. self._redact(self.parent_id)
  1506. # The relations are not returned.
  1507. event_ids = self._get_related_events()
  1508. relations = self._get_bundled_aggregations()
  1509. self.assertEqual(len(event_ids), 0)
  1510. self.assertEqual(relations, {})
  1511. def test_redact_parent_annotation(self) -> None:
  1512. """Test that annotations of an event are viewable when the original event
  1513. is redacted.
  1514. """
  1515. # Add a relation
  1516. channel = self._send_relation(RelationTypes.REFERENCE, "org.matrix.test")
  1517. related_event_id = channel.json_body["event_id"]
  1518. # The relations should exist.
  1519. event_ids = self._get_related_events()
  1520. relations = self._get_bundled_aggregations()
  1521. self.assertEqual(len(event_ids), 1)
  1522. self.assertIn(RelationTypes.REFERENCE, relations)
  1523. # Redact the original event.
  1524. self._redact(self.parent_id)
  1525. # The relations are returned.
  1526. event_ids = self._get_related_events()
  1527. relations = self._get_bundled_aggregations()
  1528. self.assertEqual(event_ids, [related_event_id])
  1529. self.assertEqual(
  1530. relations[RelationTypes.REFERENCE],
  1531. {"chunk": [{"event_id": related_event_id}]},
  1532. )
  1533. def test_redact_parent_thread(self) -> None:
  1534. """
  1535. Test that thread replies are still available when the root event is redacted.
  1536. """
  1537. channel = self._send_relation(
  1538. RelationTypes.THREAD,
  1539. EventTypes.Message,
  1540. content={"body": "reply 1", "msgtype": "m.text"},
  1541. )
  1542. related_event_id = channel.json_body["event_id"]
  1543. # Redact one of the reactions.
  1544. self._redact(self.parent_id)
  1545. # The unredacted relation should still exist.
  1546. event_ids = self._get_related_events()
  1547. relations = self._get_bundled_aggregations()
  1548. self.assertEqual(len(event_ids), 1)
  1549. self.assertLessEqual(
  1550. {
  1551. "count": 1,
  1552. "current_user_participated": True,
  1553. }.items(),
  1554. relations[RelationTypes.THREAD].items(),
  1555. )
  1556. self.assertEqual(
  1557. relations[RelationTypes.THREAD]["latest_event"]["event_id"],
  1558. related_event_id,
  1559. )
  1560. class ThreadsTestCase(BaseRelationsTestCase):
  1561. def _get_threads(self, body: JsonDict) -> List[Tuple[str, str]]:
  1562. return [
  1563. (
  1564. ev["event_id"],
  1565. ev["unsigned"]["m.relations"]["m.thread"]["latest_event"]["event_id"],
  1566. )
  1567. for ev in body["chunk"]
  1568. ]
  1569. def test_threads(self) -> None:
  1570. """Create threads and ensure the ordering is due to their latest event."""
  1571. # Create 2 threads.
  1572. thread_1 = self.parent_id
  1573. res = self.helper.send(self.room, body="Thread Root!", tok=self.user_token)
  1574. thread_2 = res["event_id"]
  1575. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1576. reply_1 = channel.json_body["event_id"]
  1577. channel = self._send_relation(
  1578. RelationTypes.THREAD, "m.room.test", parent_id=thread_2
  1579. )
  1580. reply_2 = channel.json_body["event_id"]
  1581. # Request the threads in the room.
  1582. channel = self.make_request(
  1583. "GET",
  1584. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1585. access_token=self.user_token,
  1586. )
  1587. self.assertEqual(200, channel.code, channel.json_body)
  1588. threads = self._get_threads(channel.json_body)
  1589. self.assertEqual(threads, [(thread_2, reply_2), (thread_1, reply_1)])
  1590. # Update the first thread, the ordering should swap.
  1591. channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
  1592. reply_3 = channel.json_body["event_id"]
  1593. channel = self.make_request(
  1594. "GET",
  1595. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1596. access_token=self.user_token,
  1597. )
  1598. self.assertEqual(200, channel.code, channel.json_body)
  1599. # Tuple of (thread ID, latest event ID) for each thread.
  1600. threads = self._get_threads(channel.json_body)
  1601. self.assertEqual(threads, [(thread_1, reply_3), (thread_2, reply_2)])
  1602. def test_pagination(self) -> None:
  1603. """Create threads and paginate through them."""
  1604. # Create 2 threads.
  1605. thread_1 = self.parent_id
  1606. res = self.helper.send(self.room, body="Thread Root!", tok=self.user_token)
  1607. thread_2 = res["event_id"]
  1608. self._send_relation(RelationTypes.THREAD, "m.room.test")
  1609. self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
  1610. # Request the threads in the room.
  1611. channel = self.make_request(
  1612. "GET",
  1613. f"/_matrix/client/v1/rooms/{self.room}/threads?limit=1",
  1614. access_token=self.user_token,
  1615. )
  1616. self.assertEqual(200, channel.code, channel.json_body)
  1617. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1618. self.assertEqual(thread_roots, [thread_2])
  1619. # Make sure next_batch has something in it that looks like it could be a
  1620. # valid token.
  1621. next_batch = channel.json_body.get("next_batch")
  1622. self.assertIsInstance(next_batch, str, channel.json_body)
  1623. channel = self.make_request(
  1624. "GET",
  1625. f"/_matrix/client/v1/rooms/{self.room}/threads?limit=1&from={next_batch}",
  1626. access_token=self.user_token,
  1627. )
  1628. self.assertEqual(200, channel.code, channel.json_body)
  1629. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1630. self.assertEqual(thread_roots, [thread_1], channel.json_body)
  1631. self.assertNotIn("next_batch", channel.json_body, channel.json_body)
  1632. def test_include(self) -> None:
  1633. """Filtering threads to all or participated in should work."""
  1634. # Thread 1 has the user as the root event.
  1635. thread_1 = self.parent_id
  1636. self._send_relation(
  1637. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1638. )
  1639. # Thread 2 has the user replying.
  1640. res = self.helper.send(self.room, body="Thread Root!", tok=self.user2_token)
  1641. thread_2 = res["event_id"]
  1642. self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
  1643. # Thread 3 has the user not participating in.
  1644. res = self.helper.send(self.room, body="Another thread!", tok=self.user2_token)
  1645. thread_3 = res["event_id"]
  1646. self._send_relation(
  1647. RelationTypes.THREAD,
  1648. "m.room.test",
  1649. access_token=self.user2_token,
  1650. parent_id=thread_3,
  1651. )
  1652. # All threads in the room.
  1653. channel = self.make_request(
  1654. "GET",
  1655. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1656. access_token=self.user_token,
  1657. )
  1658. self.assertEqual(200, channel.code, channel.json_body)
  1659. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1660. self.assertEqual(
  1661. thread_roots, [thread_3, thread_2, thread_1], channel.json_body
  1662. )
  1663. # Only participated threads.
  1664. channel = self.make_request(
  1665. "GET",
  1666. f"/_matrix/client/v1/rooms/{self.room}/threads?include=participated",
  1667. access_token=self.user_token,
  1668. )
  1669. self.assertEqual(200, channel.code, channel.json_body)
  1670. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1671. self.assertEqual(thread_roots, [thread_2, thread_1], channel.json_body)
  1672. def test_ignored_user(self) -> None:
  1673. """Events from ignored users should be ignored."""
  1674. # Thread 1 has a reply from an ignored user.
  1675. thread_1 = self.parent_id
  1676. self._send_relation(
  1677. RelationTypes.THREAD, "m.room.test", access_token=self.user2_token
  1678. )
  1679. # Thread 2 is created by an ignored user.
  1680. res = self.helper.send(self.room, body="Thread Root!", tok=self.user2_token)
  1681. thread_2 = res["event_id"]
  1682. self._send_relation(RelationTypes.THREAD, "m.room.test", parent_id=thread_2)
  1683. # Ignore user2.
  1684. self.get_success(
  1685. self.store.add_account_data_for_user(
  1686. self.user_id,
  1687. AccountDataTypes.IGNORED_USER_LIST,
  1688. {"ignored_users": {self.user2_id: {}}},
  1689. )
  1690. )
  1691. # Only thread 1 is returned.
  1692. channel = self.make_request(
  1693. "GET",
  1694. f"/_matrix/client/v1/rooms/{self.room}/threads",
  1695. access_token=self.user_token,
  1696. )
  1697. self.assertEqual(200, channel.code, channel.json_body)
  1698. thread_roots = [ev["event_id"] for ev in channel.json_body["chunk"]]
  1699. self.assertEqual(thread_roots, [thread_1], channel.json_body)