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

Commit 185da8f

Browse files
authored
Misc. clean-ups to the relations code (#12519)
* Corrects some typos / copy & paste errors in tests. * Clarifies docstrings. * Removes an unnecessary method.
1 parent d9b7141 commit 185da8f

File tree

4 files changed

+68
-103
lines changed

4 files changed

+68
-103
lines changed

changelog.d/12519.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor the relations code for clarity.

synapse/events/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ def _inject_bundled_aggregations(
479479
Args:
480480
event: The event being serialized.
481481
time_now: The current time in milliseconds
482+
config: Event serialization config
482483
aggregations: The bundled aggregation to serialize.
483484
serialized_event: The serialized event which may be modified.
484-
config: Event serialization config
485485
apply_edits: Whether the content of the event should be modified to reflect
486486
any replacement in `aggregations.replace`.
487487
"""

synapse/handlers/relations.py

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -256,64 +256,6 @@ async def get_annotations_for_event(
256256

257257
return filtered_results
258258

259-
async def _get_bundled_aggregation_for_event(
260-
self, event: EventBase, ignored_users: FrozenSet[str]
261-
) -> Optional[BundledAggregations]:
262-
"""Generate bundled aggregations for an event.
263-
264-
Note that this does not use a cache, but depends on cached methods.
265-
266-
Args:
267-
event: The event to calculate bundled aggregations for.
268-
ignored_users: The users ignored by the requesting user.
269-
270-
Returns:
271-
The bundled aggregations for an event, if bundled aggregations are
272-
enabled and the event can have bundled aggregations.
273-
"""
274-
275-
# Do not bundle aggregations for an event which represents an edit or an
276-
# annotation. It does not make sense for them to have related events.
277-
relates_to = event.content.get("m.relates_to")
278-
if isinstance(relates_to, (dict, frozendict)):
279-
relation_type = relates_to.get("rel_type")
280-
if relation_type in (RelationTypes.ANNOTATION, RelationTypes.REPLACE):
281-
return None
282-
283-
event_id = event.event_id
284-
room_id = event.room_id
285-
286-
# The bundled aggregations to include, a mapping of relation type to a
287-
# type-specific value. Some types include the direct return type here
288-
# while others need more processing during serialization.
289-
aggregations = BundledAggregations()
290-
291-
annotations = await self.get_annotations_for_event(
292-
event_id, room_id, ignored_users=ignored_users
293-
)
294-
if annotations:
295-
aggregations.annotations = {"chunk": annotations}
296-
297-
references, next_token = await self.get_relations_for_event(
298-
event_id,
299-
event,
300-
room_id,
301-
RelationTypes.REFERENCE,
302-
ignored_users=ignored_users,
303-
)
304-
if references:
305-
aggregations.references = {
306-
"chunk": [{"event_id": event.event_id} for event in references]
307-
}
308-
309-
if next_token:
310-
aggregations.references["next_batch"] = await next_token.to_string(
311-
self._main_store
312-
)
313-
314-
# Store the bundled aggregations in the event metadata for later use.
315-
return aggregations
316-
317259
async def get_threads_for_events(
318260
self, event_ids: Collection[str], user_id: str, ignored_users: FrozenSet[str]
319261
) -> Dict[str, _ThreadAggregation]:
@@ -435,11 +377,39 @@ async def get_bundled_aggregations(
435377

436378
# Fetch other relations per event.
437379
for event in events_by_id.values():
438-
event_result = await self._get_bundled_aggregation_for_event(
439-
event, ignored_users
380+
# Do not bundle aggregations for an event which represents an edit or an
381+
# annotation. It does not make sense for them to have related events.
382+
relates_to = event.content.get("m.relates_to")
383+
if isinstance(relates_to, (dict, frozendict)):
384+
relation_type = relates_to.get("rel_type")
385+
if relation_type in (RelationTypes.ANNOTATION, RelationTypes.REPLACE):
386+
continue
387+
388+
annotations = await self.get_annotations_for_event(
389+
event.event_id, event.room_id, ignored_users=ignored_users
440390
)
441-
if event_result:
442-
results[event.event_id] = event_result
391+
if annotations:
392+
results.setdefault(
393+
event.event_id, BundledAggregations()
394+
).annotations = {"chunk": annotations}
395+
396+
references, next_token = await self.get_relations_for_event(
397+
event.event_id,
398+
event,
399+
event.room_id,
400+
RelationTypes.REFERENCE,
401+
ignored_users=ignored_users,
402+
)
403+
if references:
404+
aggregations = results.setdefault(event.event_id, BundledAggregations())
405+
aggregations.references = {
406+
"chunk": [{"event_id": ev.event_id} for ev in references]
407+
}
408+
409+
if next_token:
410+
aggregations.references["next_batch"] = await next_token.to_string(
411+
self._main_store
412+
)
443413

444414
# Fetch any edits (but not for redacted events).
445415
#

tests/rest/client/test_relations.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -560,43 +560,6 @@ def test_edit_reply(self) -> None:
560560
{"event_id": edit_event_id, "sender": self.user_id}, m_replace_dict
561561
)
562562

563-
def test_edit_thread(self) -> None:
564-
"""Test that editing a thread works."""
565-
566-
# Create a thread and edit the last event.
567-
channel = self._send_relation(
568-
RelationTypes.THREAD,
569-
"m.room.message",
570-
content={"msgtype": "m.text", "body": "A threaded reply!"},
571-
)
572-
threaded_event_id = channel.json_body["event_id"]
573-
574-
new_body = {"msgtype": "m.text", "body": "I've been edited!"}
575-
self._send_relation(
576-
RelationTypes.REPLACE,
577-
"m.room.message",
578-
content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
579-
parent_id=threaded_event_id,
580-
)
581-
582-
# Fetch the thread root, to get the bundled aggregation for the thread.
583-
channel = self.make_request(
584-
"GET",
585-
f"/rooms/{self.room}/event/{self.parent_id}",
586-
access_token=self.user_token,
587-
)
588-
self.assertEqual(200, channel.code, channel.json_body)
589-
590-
# We expect that the edit message appears in the thread summary in the
591-
# unsigned relations section.
592-
relations_dict = channel.json_body["unsigned"].get("m.relations")
593-
self.assertIn(RelationTypes.THREAD, relations_dict)
594-
595-
thread_summary = relations_dict[RelationTypes.THREAD]
596-
self.assertIn("latest_event", thread_summary)
597-
latest_event_in_thread = thread_summary["latest_event"]
598-
self.assertEqual(latest_event_in_thread["content"]["body"], "I've been edited!")
599-
600563
def test_edit_edit(self) -> None:
601564
"""Test that an edit cannot be edited."""
602565
new_body = {"msgtype": "m.text", "body": "Initial edit"}
@@ -1047,7 +1010,7 @@ def test_thread(self) -> None:
10471010
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
10481011
thread_2 = channel.json_body["event_id"]
10491012

1050-
def assert_annotations(bundled_aggregations: JsonDict) -> None:
1013+
def assert_thread(bundled_aggregations: JsonDict) -> None:
10511014
self.assertEqual(2, bundled_aggregations.get("count"))
10521015
self.assertTrue(bundled_aggregations.get("current_user_participated"))
10531016
# The latest thread event has some fields that don't matter.
@@ -1066,7 +1029,38 @@ def assert_annotations(bundled_aggregations: JsonDict) -> None:
10661029
bundled_aggregations.get("latest_event"),
10671030
)
10681031

1069-
self._test_bundled_aggregations(RelationTypes.THREAD, assert_annotations, 9)
1032+
self._test_bundled_aggregations(RelationTypes.THREAD, assert_thread, 9)
1033+
1034+
def test_thread_edit_latest_event(self) -> None:
1035+
"""Test that editing the latest event in a thread works."""
1036+
1037+
# Create a thread and edit the last event.
1038+
channel = self._send_relation(
1039+
RelationTypes.THREAD,
1040+
"m.room.message",
1041+
content={"msgtype": "m.text", "body": "A threaded reply!"},
1042+
)
1043+
threaded_event_id = channel.json_body["event_id"]
1044+
1045+
new_body = {"msgtype": "m.text", "body": "I've been edited!"}
1046+
channel = self._send_relation(
1047+
RelationTypes.REPLACE,
1048+
"m.room.message",
1049+
content={"msgtype": "m.text", "body": "foo", "m.new_content": new_body},
1050+
parent_id=threaded_event_id,
1051+
)
1052+
1053+
# Fetch the thread root, to get the bundled aggregation for the thread.
1054+
relations_dict = self._get_bundled_aggregations()
1055+
1056+
# We expect that the edit message appears in the thread summary in the
1057+
# unsigned relations section.
1058+
self.assertIn(RelationTypes.THREAD, relations_dict)
1059+
1060+
thread_summary = relations_dict[RelationTypes.THREAD]
1061+
self.assertIn("latest_event", thread_summary)
1062+
latest_event_in_thread = thread_summary["latest_event"]
1063+
self.assertEqual(latest_event_in_thread["content"]["body"], "I've been edited!")
10701064

10711065
def test_aggregation_get_event_for_annotation(self) -> None:
10721066
"""Test that annotations do not get bundled aggregations included
@@ -1093,7 +1087,7 @@ def test_aggregation_get_event_for_thread(self) -> None:
10931087
channel = self._send_relation(RelationTypes.THREAD, "m.room.test")
10941088
thread_id = channel.json_body["event_id"]
10951089

1096-
# Annotate the annotation.
1090+
# Annotate the thread.
10971091
self._send_relation(
10981092
RelationTypes.ANNOTATION, "m.reaction", "a", parent_id=thread_id
10991093
)

0 commit comments

Comments
 (0)