diff --git a/synapse/events/utils.py b/synapse/events/utils.py
index a6c48308b3..026dcde8d8 100644
--- a/synapse/events/utils.py
+++ b/synapse/events/utils.py
@@ -426,13 +426,12 @@ class EventClientSerializer:
# Check if there are any bundled aggregations to include with the event.
if bundle_aggregations:
- event_aggregations = bundle_aggregations.get(event.event_id)
- if event_aggregations:
+ if event.event_id in bundle_aggregations:
self._inject_bundled_aggregations(
event,
time_now,
config,
- event_aggregations,
+ bundle_aggregations,
serialized_event,
apply_edits=apply_edits,
)
@@ -471,7 +470,7 @@ class EventClientSerializer:
event: EventBase,
time_now: int,
config: SerializeEventConfig,
- aggregations: "BundledAggregations",
+ bundled_aggregations: Dict[str, "BundledAggregations"],
serialized_event: JsonDict,
apply_edits: bool,
) -> None:
@@ -481,22 +480,37 @@ class EventClientSerializer:
event: The event being serialized.
time_now: The current time in milliseconds
config: Event serialization config
- aggregations: The bundled aggregation to serialize.
+ bundled_aggregations: Bundled aggregations to be injected.
+ A map from event_id to aggregation data. Must contain at least an
+ entry for `event`.
+
+ While serializing the bundled aggregations this map may be searched
+ again for additional events in a recursive manner.
serialized_event: The serialized event which may be modified.
apply_edits: Whether the content of the event should be modified to reflect
any replacement in `aggregations.replace`.
"""
+
+ # We have already checked that aggregations exist for this event.
+ event_aggregations = bundled_aggregations[event.event_id]
+
+ # The JSON dictionary to be added under the unsigned property of the event
+ # being serialized.
serialized_aggregations = {}
- if aggregations.annotations:
- serialized_aggregations[RelationTypes.ANNOTATION] = aggregations.annotations
+ if event_aggregations.annotations:
+ serialized_aggregations[
+ RelationTypes.ANNOTATION
+ ] = event_aggregations.annotations
- if aggregations.references:
- serialized_aggregations[RelationTypes.REFERENCE] = aggregations.references
+ if event_aggregations.references:
+ serialized_aggregations[
+ RelationTypes.REFERENCE
+ ] = event_aggregations.references
- if aggregations.replace:
+ if event_aggregations.replace:
# If there is an edit, optionally apply it to the event.
- edit = aggregations.replace
+ edit = event_aggregations.replace
if apply_edits:
self._apply_edit(event, serialized_event, edit)
@@ -507,19 +521,16 @@ class EventClientSerializer:
"sender": edit.sender,
}
- # If this event is the start of a thread, include a summary of the replies.
- if aggregations.thread:
- thread = aggregations.thread
+ # Include any threaded replies to this event.
+ if event_aggregations.thread:
+ thread = event_aggregations.thread
- # Don't bundle aggregations as this could recurse forever.
- serialized_latest_event = serialize_event(
- thread.latest_event, time_now, config=config
+ serialized_latest_event = self.serialize_event(
+ thread.latest_event,
+ time_now,
+ config=config,
+ bundle_aggregations=bundled_aggregations,
)
- # Manually apply an edit, if one exists.
- if thread.latest_edit:
- self._apply_edit(
- thread.latest_event, serialized_latest_event, thread.latest_edit
- )
thread_summary = {
"latest_event": serialized_latest_event,
|