Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ee68683

Browse files
committed
Do not bundle relations for annotations and edits.
1 parent 3dab787 commit ee68683

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

synapse/events/utils.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ async def _injected_bundled_relations(
433433
serialized_event: The serialized event which may be modified.
434434
435435
"""
436+
# Do not bundle aggregations for an event which represents an edit or annotation.
437+
relates_to = event.content.get("m.relates_to")
438+
if isinstance(relates_to, (dict, frozendict)):
439+
relation_type = relates_to.get("rel_type")
440+
if relation_type in (RelationTypes.ANNOTATION, RelationTypes.REPLACE):
441+
return
442+
436443
event_id = event.event_id
437444

438445
annotations = await self.store.get_aggregation_groups_for_event(event_id)
@@ -465,7 +472,6 @@ async def _injected_bundled_relations(
465472
serialized_event["content"] = edit_content.get("m.new_content", {})
466473

467474
# Check for existing relations
468-
relates_to = event.content.get("m.relates_to")
469475
if relates_to:
470476
# Keep the relations, ensuring we use a dict copy of the original
471477
serialized_event["content"]["m.relates_to"] = relates_to.copy()

tests/rest/client/test_relations.py

+72
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,28 @@ def test_aggregation_get_event(self):
461461
},
462462
)
463463

464+
def test_aggregation_get_event_for_annotation(self):
465+
"""Test that annotations do not get bundled relations included
466+
when directly requested.
467+
"""
468+
channel = self._send_relation(RelationTypes.ANNOTATION, "m.reaction", "a")
469+
self.assertEquals(200, channel.code, channel.json_body)
470+
annotation_id = channel.json_body["event_id"]
471+
472+
# Annotate the annotation.
473+
channel = self._send_relation(
474+
RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=annotation_id
475+
)
476+
self.assertEquals(200, channel.code, channel.json_body)
477+
478+
channel = self.make_request(
479+
"GET",
480+
f"/rooms/{self.room}/event/{annotation_id}",
481+
access_token=self.user_token,
482+
)
483+
self.assertEquals(200, channel.code, channel.json_body)
484+
self.assertIsNone(channel.json_body["unsigned"].get("m.relations"))
485+
464486
def test_aggregation_get_event_for_thread(self):
465487
"""Test that threads get bundled relations included when directly requested."""
466488
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
@@ -653,6 +675,56 @@ def test_edit_reply(self):
653675
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
654676
)
655677

678+
def test_edit_edit(self):
679+
"""Test that an edit cannot be edited."""
680+
new_body = {"msgtype": "m.text", "body": "Initial edit"}
681+
channel = self._send_relation(
682+
RelationTypes.REPLACE,
683+
"m.room.message",
684+
content={
685+
"msgtype": "m.text",
686+
"body": "Wibble",
687+
"m.new_content": new_body,
688+
},
689+
)
690+
self.assertEquals(200, channel.code, channel.json_body)
691+
edit_event_id = channel.json_body["event_id"]
692+
693+
# Edit the edit event.
694+
channel = self._send_relation(
695+
RelationTypes.REPLACE,
696+
"m.room.message",
697+
content={
698+
"msgtype": "m.text",
699+
"body": "foo",
700+
"m.new_content": {"msgtype": "m.text", "body": "Ignored edit"},
701+
},
702+
parent_id=edit_event_id,
703+
)
704+
self.assertEquals(200, channel.code, channel.json_body)
705+
706+
# Request the original event.
707+
channel = self.make_request(
708+
"GET",
709+
"/rooms/%s/event/%s" % (self.room, self.parent_id),
710+
access_token=self.user_token,
711+
)
712+
self.assertEquals(200, channel.code, channel.json_body)
713+
# The edit to the edit should be ignored.
714+
self.assertEquals(channel.json_body["content"], new_body)
715+
716+
# The relations information should not include the edit to the edit.
717+
relations_dict = channel.json_body["unsigned"].get("m.relations")
718+
self.assertIn(RelationTypes.REPLACE, relations_dict)
719+
720+
m_replace_dict = relations_dict[RelationTypes.REPLACE]
721+
for key in ["event_id", "sender", "origin_server_ts"]:
722+
self.assertIn(key, m_replace_dict)
723+
724+
self.assert_dict(
725+
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
726+
)
727+
656728
def test_relations_redaction_redacts_edits(self):
657729
"""Test that edits of an event are redacted when the original event
658730
is redacted.

0 commit comments

Comments
 (0)