test_relations.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  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 itertools
  16. import json
  17. import six
  18. from synapse.api.constants import EventTypes, RelationTypes
  19. from synapse.rest import admin
  20. from synapse.rest.client.v1 import login, room
  21. from synapse.rest.client.v2_alpha import register, relations
  22. from tests import unittest
  23. class RelationsTestCase(unittest.HomeserverTestCase):
  24. servlets = [
  25. relations.register_servlets,
  26. room.register_servlets,
  27. login.register_servlets,
  28. register.register_servlets,
  29. admin.register_servlets_for_client_rest_resource,
  30. ]
  31. hijack_auth = False
  32. def make_homeserver(self, reactor, clock):
  33. # We need to enable msc1849 support for aggregations
  34. config = self.default_config()
  35. config["experimental_msc1849_support_enabled"] = True
  36. return self.setup_test_homeserver(config=config)
  37. def prepare(self, reactor, clock, hs):
  38. self.user_id, self.user_token = self._create_user("alice")
  39. self.user2_id, self.user2_token = self._create_user("bob")
  40. self.room = self.helper.create_room_as(self.user_id, tok=self.user_token)
  41. self.helper.join(self.room, user=self.user2_id, tok=self.user2_token)
  42. res = self.helper.send(self.room, body="Hi!", tok=self.user_token)
  43. self.parent_id = res["event_id"]
  44. def test_send_relation(self):
  45. """Tests that sending a relation using the new /send_relation works
  46. creates the right shape of event.
  47. """
  48. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="👍")
  49. self.assertEquals(200, channel.code, channel.json_body)
  50. event_id = channel.json_body["event_id"]
  51. request, channel = self.make_request(
  52. "GET",
  53. "/rooms/%s/event/%s" % (self.room, event_id),
  54. access_token=self.user_token,
  55. )
  56. self.render(request)
  57. self.assertEquals(200, channel.code, channel.json_body)
  58. self.assert_dict(
  59. {
  60. "type": "m.reaction",
  61. "sender": self.user_id,
  62. "content": {
  63. "m.relates_to": {
  64. "event_id": self.parent_id,
  65. "key": "👍",
  66. "rel_type": RelationTypes.ANNOTATION,
  67. }
  68. },
  69. },
  70. channel.json_body,
  71. )
  72. def test_deny_membership(self):
  73. """Test that we deny relations on membership events
  74. """
  75. channel = self._send_relation(RelationTypes.ANNOTATION, EventTypes.Member)
  76. self.assertEquals(400, channel.code, channel.json_body)
  77. def test_deny_double_react(self):
  78. """Test that we deny relations on membership events
  79. """
  80. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
  81. self.assertEquals(200, channel.code, channel.json_body)
  82. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  83. self.assertEquals(400, channel.code, channel.json_body)
  84. def test_basic_paginate_relations(self):
  85. """Tests that calling pagination API corectly the latest relations.
  86. """
  87. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction")
  88. self.assertEquals(200, channel.code, channel.json_body)
  89. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction")
  90. self.assertEquals(200, channel.code, channel.json_body)
  91. annotation_id = channel.json_body["event_id"]
  92. request, channel = self.make_request(
  93. "GET",
  94. "/_matrix/client/unstable/rooms/%s/relations/%s?limit=1"
  95. % (self.room, self.parent_id),
  96. access_token=self.user_token,
  97. )
  98. self.render(request)
  99. self.assertEquals(200, channel.code, channel.json_body)
  100. # We expect to get back a single pagination result, which is the full
  101. # relation event we sent above.
  102. self.assertEquals(len(channel.json_body["chunk"]), 1, channel.json_body)
  103. self.assert_dict(
  104. {"event_id": annotation_id, "sender": self.user_id, "type": "m.reaction"},
  105. channel.json_body["chunk"][0],
  106. )
  107. # We also expect to get the original event (the id of which is self.parent_id)
  108. self.assertEquals(
  109. channel.json_body["original_event"]["event_id"], self.parent_id
  110. )
  111. # Make sure next_batch has something in it that looks like it could be a
  112. # valid token.
  113. self.assertIsInstance(
  114. channel.json_body.get("next_batch"), six.string_types, channel.json_body
  115. )
  116. def test_repeated_paginate_relations(self):
  117. """Test that if we paginate using a limit and tokens then we get the
  118. expected events.
  119. """
  120. expected_event_ids = []
  121. for _ in range(10):
  122. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction")
  123. self.assertEquals(200, channel.code, channel.json_body)
  124. expected_event_ids.append(channel.json_body["event_id"])
  125. prev_token = None
  126. found_event_ids = []
  127. for _ in range(20):
  128. from_token = ""
  129. if prev_token:
  130. from_token = "&from=" + prev_token
  131. request, channel = self.make_request(
  132. "GET",
  133. "/_matrix/client/unstable/rooms/%s/relations/%s?limit=1%s"
  134. % (self.room, self.parent_id, from_token),
  135. access_token=self.user_token,
  136. )
  137. self.render(request)
  138. self.assertEquals(200, channel.code, channel.json_body)
  139. found_event_ids.extend(e["event_id"] for e in channel.json_body["chunk"])
  140. next_batch = channel.json_body.get("next_batch")
  141. self.assertNotEquals(prev_token, next_batch)
  142. prev_token = next_batch
  143. if not prev_token:
  144. break
  145. # We paginated backwards, so reverse
  146. found_event_ids.reverse()
  147. self.assertEquals(found_event_ids, expected_event_ids)
  148. def test_aggregation_pagination_groups(self):
  149. """Test that we can paginate annotation groups correctly.
  150. """
  151. # We need to create ten separate users to send each reaction.
  152. access_tokens = [self.user_token, self.user2_token]
  153. idx = 0
  154. while len(access_tokens) < 10:
  155. user_id, token = self._create_user("test" + str(idx))
  156. idx += 1
  157. self.helper.join(self.room, user=user_id, tok=token)
  158. access_tokens.append(token)
  159. idx = 0
  160. sent_groups = {"👍": 10, "a": 7, "b": 5, "c": 3, "d": 2, "e": 1}
  161. for key in itertools.chain.from_iterable(
  162. itertools.repeat(key, num) for key, num in sent_groups.items()
  163. ):
  164. channel = self._send_relation(
  165. RelationTypes.ANNOTATION,
  166. "m.reaction",
  167. key=key,
  168. access_token=access_tokens[idx],
  169. )
  170. self.assertEquals(200, channel.code, channel.json_body)
  171. idx += 1
  172. idx %= len(access_tokens)
  173. prev_token = None
  174. found_groups = {}
  175. for _ in range(20):
  176. from_token = ""
  177. if prev_token:
  178. from_token = "&from=" + prev_token
  179. request, channel = self.make_request(
  180. "GET",
  181. "/_matrix/client/unstable/rooms/%s/aggregations/%s?limit=1%s"
  182. % (self.room, self.parent_id, from_token),
  183. access_token=self.user_token,
  184. )
  185. self.render(request)
  186. self.assertEquals(200, channel.code, channel.json_body)
  187. self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
  188. for groups in channel.json_body["chunk"]:
  189. # We only expect reactions
  190. self.assertEqual(groups["type"], "m.reaction", channel.json_body)
  191. # We should only see each key once
  192. self.assertNotIn(groups["key"], found_groups, channel.json_body)
  193. found_groups[groups["key"]] = groups["count"]
  194. next_batch = channel.json_body.get("next_batch")
  195. self.assertNotEquals(prev_token, next_batch)
  196. prev_token = next_batch
  197. if not prev_token:
  198. break
  199. self.assertEquals(sent_groups, found_groups)
  200. def test_aggregation_pagination_within_group(self):
  201. """Test that we can paginate within an annotation group.
  202. """
  203. # We need to create ten separate users to send each reaction.
  204. access_tokens = [self.user_token, self.user2_token]
  205. idx = 0
  206. while len(access_tokens) < 10:
  207. user_id, token = self._create_user("test" + str(idx))
  208. idx += 1
  209. self.helper.join(self.room, user=user_id, tok=token)
  210. access_tokens.append(token)
  211. idx = 0
  212. expected_event_ids = []
  213. for _ in range(10):
  214. channel = self._send_relation(
  215. RelationTypes.ANNOTATION,
  216. "m.reaction",
  217. key="👍",
  218. access_token=access_tokens[idx],
  219. )
  220. self.assertEquals(200, channel.code, channel.json_body)
  221. expected_event_ids.append(channel.json_body["event_id"])
  222. idx += 1
  223. # Also send a different type of reaction so that we test we don't see it
  224. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", key="a")
  225. self.assertEquals(200, channel.code, channel.json_body)
  226. prev_token = None
  227. found_event_ids = []
  228. encoded_key = six.moves.urllib.parse.quote_plus("👍".encode("utf-8"))
  229. for _ in range(20):
  230. from_token = ""
  231. if prev_token:
  232. from_token = "&from=" + prev_token
  233. request, channel = self.make_request(
  234. "GET",
  235. "/_matrix/client/unstable/rooms/%s"
  236. "/aggregations/%s/%s/m.reaction/%s?limit=1%s"
  237. % (
  238. self.room,
  239. self.parent_id,
  240. RelationTypes.ANNOTATION,
  241. encoded_key,
  242. from_token,
  243. ),
  244. access_token=self.user_token,
  245. )
  246. self.render(request)
  247. self.assertEquals(200, channel.code, channel.json_body)
  248. self.assertEqual(len(channel.json_body["chunk"]), 1, channel.json_body)
  249. found_event_ids.extend(e["event_id"] for e in channel.json_body["chunk"])
  250. next_batch = channel.json_body.get("next_batch")
  251. self.assertNotEquals(prev_token, next_batch)
  252. prev_token = next_batch
  253. if not prev_token:
  254. break
  255. # We paginated backwards, so reverse
  256. found_event_ids.reverse()
  257. self.assertEquals(found_event_ids, expected_event_ids)
  258. def test_aggregation(self):
  259. """Test that annotations get correctly aggregated.
  260. """
  261. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  262. self.assertEquals(200, channel.code, channel.json_body)
  263. channel = self._send_relation(
  264. RelationTypes.ANNOTATION, "m.reaction", "a", access_token=self.user2_token
  265. )
  266. self.assertEquals(200, channel.code, channel.json_body)
  267. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
  268. self.assertEquals(200, channel.code, channel.json_body)
  269. request, channel = self.make_request(
  270. "GET",
  271. "/_matrix/client/unstable/rooms/%s/aggregations/%s"
  272. % (self.room, self.parent_id),
  273. access_token=self.user_token,
  274. )
  275. self.render(request)
  276. self.assertEquals(200, channel.code, channel.json_body)
  277. self.assertEquals(
  278. channel.json_body,
  279. {
  280. "chunk": [
  281. {"type": "m.reaction", "key": "a", "count": 2},
  282. {"type": "m.reaction", "key": "b", "count": 1},
  283. ]
  284. },
  285. )
  286. def test_aggregation_redactions(self):
  287. """Test that annotations get correctly aggregated after a redaction.
  288. """
  289. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  290. self.assertEquals(200, channel.code, channel.json_body)
  291. to_redact_event_id = channel.json_body["event_id"]
  292. channel = self._send_relation(
  293. RelationTypes.ANNOTATION, "m.reaction", "a", access_token=self.user2_token
  294. )
  295. self.assertEquals(200, channel.code, channel.json_body)
  296. # Now lets redact one of the 'a' reactions
  297. request, channel = self.make_request(
  298. "POST",
  299. "/_matrix/client/r0/rooms/%s/redact/%s" % (self.room, to_redact_event_id),
  300. access_token=self.user_token,
  301. content={},
  302. )
  303. self.render(request)
  304. self.assertEquals(200, channel.code, channel.json_body)
  305. request, channel = self.make_request(
  306. "GET",
  307. "/_matrix/client/unstable/rooms/%s/aggregations/%s"
  308. % (self.room, self.parent_id),
  309. access_token=self.user_token,
  310. )
  311. self.render(request)
  312. self.assertEquals(200, channel.code, channel.json_body)
  313. self.assertEquals(
  314. channel.json_body,
  315. {"chunk": [{"type": "m.reaction", "key": "a", "count": 1}]},
  316. )
  317. def test_aggregation_must_be_annotation(self):
  318. """Test that aggregations must be annotations.
  319. """
  320. request, channel = self.make_request(
  321. "GET",
  322. "/_matrix/client/unstable/rooms/%s/aggregations/%s/%s?limit=1"
  323. % (self.room, self.parent_id, RelationTypes.REPLACE),
  324. access_token=self.user_token,
  325. )
  326. self.render(request)
  327. self.assertEquals(400, channel.code, channel.json_body)
  328. def test_aggregation_get_event(self):
  329. """Test that annotations and references get correctly bundled when
  330. getting the parent event.
  331. """
  332. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
  333. self.assertEquals(200, channel.code, channel.json_body)
  334. channel = self._send_relation(
  335. RelationTypes.ANNOTATION, "m.reaction", "a", access_token=self.user2_token
  336. )
  337. self.assertEquals(200, channel.code, channel.json_body)
  338. channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "b")
  339. self.assertEquals(200, channel.code, channel.json_body)
  340. channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
  341. self.assertEquals(200, channel.code, channel.json_body)
  342. reply_1 = channel.json_body["event_id"]
  343. channel = self._send_relation(RelationTypes.REFERENCE, "m.room.test")
  344. self.assertEquals(200, channel.code, channel.json_body)
  345. reply_2 = channel.json_body["event_id"]
  346. request, channel = self.make_request(
  347. "GET",
  348. "/rooms/%s/event/%s" % (self.room, self.parent_id),
  349. access_token=self.user_token,
  350. )
  351. self.render(request)
  352. self.assertEquals(200, channel.code, channel.json_body)
  353. self.assertEquals(
  354. channel.json_body["unsigned"].get("m.relations"),
  355. {
  356. RelationTypes.ANNOTATION: {
  357. "chunk": [
  358. {"type": "m.reaction", "key": "a", "count": 2},
  359. {"type": "m.reaction", "key": "b", "count": 1},
  360. ]
  361. },
  362. RelationTypes.REFERENCE: {
  363. "chunk": [{"event_id": reply_1}, {"event_id": reply_2}]
  364. },
  365. },
  366. )
  367. def test_edit(self):
  368. """Test that a simple edit works.
  369. """
  370. new_body = {"msgtype": "m.text", "body": "I've been edited!"}
  371. channel = self._send_relation(
  372. RelationTypes.REPLACE,
  373. "m.room.message",
  374. content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
  375. )
  376. self.assertEquals(200, channel.code, channel.json_body)
  377. edit_event_id = channel.json_body["event_id"]
  378. request, channel = self.make_request(
  379. "GET",
  380. "/rooms/%s/event/%s" % (self.room, self.parent_id),
  381. access_token=self.user_token,
  382. )
  383. self.render(request)
  384. self.assertEquals(200, channel.code, channel.json_body)
  385. self.assertEquals(channel.json_body["content"], new_body)
  386. relations_dict = channel.json_body["unsigned"].get("m.relations")
  387. self.assertIn(RelationTypes.REPLACE, relations_dict)
  388. m_replace_dict = relations_dict[RelationTypes.REPLACE]
  389. for key in ["event_id", "sender", "origin_server_ts"]:
  390. self.assertIn(key, m_replace_dict)
  391. self.assert_dict(
  392. {"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
  393. )
  394. def test_multi_edit(self):
  395. """Test that multiple edits, including attempts by people who
  396. shouldn't be allowed, are correctly handled.
  397. """
  398. channel = self._send_relation(
  399. RelationTypes.REPLACE,
  400. "m.room.message",
  401. content={
  402. "msgtype": "m.text",
  403. "body": "Wibble",
  404. "m.new_content": {"msgtype": "m.text", "body": "First edit"},
  405. },
  406. )
  407. self.assertEquals(200, channel.code, channel.json_body)
  408. new_body = {"msgtype": "m.text", "body": "I've been edited!"}
  409. channel = self._send_relation(
  410. RelationTypes.REPLACE,
  411. "m.room.message",
  412. content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
  413. )
  414. self.assertEquals(200, channel.code, channel.json_body)
  415. edit_event_id = channel.json_body["event_id"]
  416. channel = self._send_relation(
  417. RelationTypes.REPLACE,
  418. "m.room.message.WRONG_TYPE",
  419. content={
  420. "msgtype": "m.text",
  421. "body": "Wibble",
  422. "m.new_content": {"msgtype": "m.text", "body": "Edit, but wrong type"},
  423. },
  424. )
  425. self.assertEquals(200, channel.code, channel.json_body)
  426. request, channel = self.make_request(
  427. "GET",
  428. "/rooms/%s/event/%s" % (self.room, self.parent_id),
  429. access_token=self.user_token,
  430. )
  431. self.render(request)
  432. self.assertEquals(200, channel.code, channel.json_body)
  433. self.assertEquals(channel.json_body["content"], new_body)
  434. relations_dict = channel.json_body["unsigned"].get("m.relations")
  435. self.assertIn(RelationTypes.REPLACE, relations_dict)
  436. m_replace_dict = relations_dict[RelationTypes.REPLACE]
  437. for key in ["event_id", "sender", "origin_server_ts"]:
  438. self.assertIn(key, m_replace_dict)
  439. self.assert_dict(
  440. {"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
  441. )
  442. def test_relations_redaction_redacts_edits(self):
  443. """Test that edits of an event are redacted when the original event
  444. is redacted.
  445. """
  446. # Send a new event
  447. res = self.helper.send(self.room, body="Heyo!", tok=self.user_token)
  448. original_event_id = res["event_id"]
  449. # Add a relation
  450. channel = self._send_relation(
  451. RelationTypes.REPLACE,
  452. "m.room.message",
  453. parent_id=original_event_id,
  454. content={
  455. "msgtype": "m.text",
  456. "body": "Wibble",
  457. "m.new_content": {"msgtype": "m.text", "body": "First edit"},
  458. },
  459. )
  460. self.assertEquals(200, channel.code, channel.json_body)
  461. # Check the relation is returned
  462. request, channel = self.make_request(
  463. "GET",
  464. "/_matrix/client/unstable/rooms/%s/relations/%s/m.replace/m.room.message"
  465. % (self.room, original_event_id),
  466. access_token=self.user_token,
  467. )
  468. self.render(request)
  469. self.assertEquals(200, channel.code, channel.json_body)
  470. self.assertIn("chunk", channel.json_body)
  471. self.assertEquals(len(channel.json_body["chunk"]), 1)
  472. # Redact the original event
  473. request, channel = self.make_request(
  474. "PUT",
  475. "/rooms/%s/redact/%s/%s"
  476. % (self.room, original_event_id, "test_relations_redaction_redacts_edits"),
  477. access_token=self.user_token,
  478. content="{}",
  479. )
  480. self.render(request)
  481. self.assertEquals(200, channel.code, channel.json_body)
  482. # Try to check for remaining m.replace relations
  483. request, channel = self.make_request(
  484. "GET",
  485. "/_matrix/client/unstable/rooms/%s/relations/%s/m.replace/m.room.message"
  486. % (self.room, original_event_id),
  487. access_token=self.user_token,
  488. )
  489. self.render(request)
  490. self.assertEquals(200, channel.code, channel.json_body)
  491. # Check that no relations are returned
  492. self.assertIn("chunk", channel.json_body)
  493. self.assertEquals(channel.json_body["chunk"], [])
  494. def test_aggregations_redaction_prevents_access_to_aggregations(self):
  495. """Test that annotations of an event are redacted when the original event
  496. is redacted.
  497. """
  498. # Send a new event
  499. res = self.helper.send(self.room, body="Hello!", tok=self.user_token)
  500. original_event_id = res["event_id"]
  501. # Add a relation
  502. channel = self._send_relation(
  503. RelationTypes.ANNOTATION, "m.reaction", key="👍", parent_id=original_event_id
  504. )
  505. self.assertEquals(200, channel.code, channel.json_body)
  506. # Redact the original
  507. request, channel = self.make_request(
  508. "PUT",
  509. "/rooms/%s/redact/%s/%s"
  510. % (
  511. self.room,
  512. original_event_id,
  513. "test_aggregations_redaction_prevents_access_to_aggregations",
  514. ),
  515. access_token=self.user_token,
  516. content="{}",
  517. )
  518. self.render(request)
  519. self.assertEquals(200, channel.code, channel.json_body)
  520. # Check that aggregations returns zero
  521. request, channel = self.make_request(
  522. "GET",
  523. "/_matrix/client/unstable/rooms/%s/aggregations/%s/m.annotation/m.reaction"
  524. % (self.room, original_event_id),
  525. access_token=self.user_token,
  526. )
  527. self.render(request)
  528. self.assertEquals(200, channel.code, channel.json_body)
  529. self.assertIn("chunk", channel.json_body)
  530. self.assertEquals(channel.json_body["chunk"], [])
  531. def _send_relation(
  532. self,
  533. relation_type,
  534. event_type,
  535. key=None,
  536. content={},
  537. access_token=None,
  538. parent_id=None,
  539. ):
  540. """Helper function to send a relation pointing at `self.parent_id`
  541. Args:
  542. relation_type (str): One of `RelationTypes`
  543. event_type (str): The type of the event to create
  544. parent_id (str): The event_id this relation relates to. If None, then self.parent_id
  545. key (str|None): The aggregation key used for m.annotation relation
  546. type.
  547. content(dict|None): The content of the created event.
  548. access_token (str|None): The access token used to send the relation,
  549. defaults to `self.user_token`
  550. Returns:
  551. FakeChannel
  552. """
  553. if not access_token:
  554. access_token = self.user_token
  555. query = ""
  556. if key:
  557. query = "?key=" + six.moves.urllib.parse.quote_plus(key.encode("utf-8"))
  558. original_id = parent_id if parent_id else self.parent_id
  559. request, channel = self.make_request(
  560. "POST",
  561. "/_matrix/client/unstable/rooms/%s/send_relation/%s/%s/%s%s"
  562. % (self.room, original_id, relation_type, event_type, query),
  563. json.dumps(content).encode("utf-8"),
  564. access_token=access_token,
  565. )
  566. self.render(request)
  567. return channel
  568. def _create_user(self, localpart):
  569. user_id = self.register_user(localpart, "abc123")
  570. access_token = self.login(localpart, "abc123")
  571. return user_id, access_token