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

Commit 3dab787

Browse files
committed
Move bundle calculation to a separate method.
1 parent 041e6c0 commit 3dab787

File tree

1 file changed

+77
-65
lines changed

1 file changed

+77
-65
lines changed

synapse/events/utils.py

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ async def serialize_event(
398398
"""Serializes a single event.
399399
400400
Args:
401-
event
401+
event: The event being serialized.
402402
time_now: The current time in milliseconds
403403
bundle_aggregations: Whether to bundle in related events
404404
**kwargs: Arguments to pass to `serialize_event`
@@ -410,7 +410,6 @@ async def serialize_event(
410410
if not isinstance(event, EventBase):
411411
return event
412412

413-
event_id = event.event_id
414413
serialized_event = serialize_event(event, time_now, **kwargs)
415414

416415
# If MSC1849 is enabled then we need to look if there are any relations
@@ -419,72 +418,85 @@ async def serialize_event(
419418
if not event.internal_metadata.is_redacted() and (
420419
self._msc1849_enabled and bundle_aggregations
421420
):
422-
annotations = await self.store.get_aggregation_groups_for_event(event_id)
423-
references = await self.store.get_relations_for_event(
424-
event_id, RelationTypes.REFERENCE, direction="f"
425-
)
426-
427-
relations = {}
428-
429-
if annotations.chunk:
430-
relations[RelationTypes.ANNOTATION] = annotations.to_dict()
431-
432-
if references.chunk:
433-
relations[RelationTypes.REFERENCE] = references.to_dict()
434-
435-
edit = None
436-
if event.type == EventTypes.Message:
437-
edit = await self.store.get_applicable_edit(event_id)
438-
439-
if edit:
440-
# If there is an edit replace the content, preserving existing
441-
# relations.
442-
443-
# Ensure we take copies of the edit content, otherwise we risk modifying
444-
# the original event.
445-
edit_content = edit.content.copy()
446-
447-
# Unfreeze the event content if necessary, so that we may modify it below
448-
edit_content = unfreeze(edit_content)
449-
serialized_event["content"] = edit_content.get("m.new_content", {})
450-
451-
# Check for existing relations
452-
relates_to = event.content.get("m.relates_to")
453-
if relates_to:
454-
# Keep the relations, ensuring we use a dict copy of the original
455-
serialized_event["content"]["m.relates_to"] = relates_to.copy()
456-
else:
457-
serialized_event["content"].pop("m.relates_to", None)
458-
459-
relations[RelationTypes.REPLACE] = {
460-
"event_id": edit.event_id,
461-
"origin_server_ts": edit.origin_server_ts,
462-
"sender": edit.sender,
463-
}
464-
465-
# If this event is the start of a thread, include a summary of the replies.
466-
if self._msc3440_enabled:
467-
(
468-
thread_count,
469-
latest_thread_event,
470-
) = await self.store.get_thread_summary(event_id)
471-
if latest_thread_event:
472-
relations[RelationTypes.THREAD] = {
473-
# Don't bundle aggregations as this could recurse forever.
474-
"latest_event": await self.serialize_event(
475-
latest_thread_event, time_now, bundle_aggregations=False
476-
),
477-
"count": thread_count,
478-
}
479-
480-
# If any bundled relations were found, include them.
481-
if relations:
482-
serialized_event["unsigned"].setdefault("m.relations", {}).update(
483-
relations
484-
)
421+
await self._injected_bundled_relations(event, time_now, serialized_event)
485422

486423
return serialized_event
487424

425+
async def _injected_bundled_relations(
426+
self, event: EventBase, time_now: int, serialized_event: JsonDict
427+
) -> None:
428+
"""Potentially injects bundled relations into the unsigend portion of the serialized event.
429+
430+
Args:
431+
event: The event being serialized.
432+
time_now: The current time in milliseconds
433+
serialized_event: The serialized event which may be modified.
434+
435+
"""
436+
event_id = event.event_id
437+
438+
annotations = await self.store.get_aggregation_groups_for_event(event_id)
439+
references = await self.store.get_relations_for_event(
440+
event_id, RelationTypes.REFERENCE, direction="f"
441+
)
442+
443+
relations = {}
444+
445+
if annotations.chunk:
446+
relations[RelationTypes.ANNOTATION] = annotations.to_dict()
447+
448+
if references.chunk:
449+
relations[RelationTypes.REFERENCE] = references.to_dict()
450+
451+
edit = None
452+
if event.type == EventTypes.Message:
453+
edit = await self.store.get_applicable_edit(event_id)
454+
455+
if edit:
456+
# If there is an edit replace the content, preserving existing
457+
# relations.
458+
459+
# Ensure we take copies of the edit content, otherwise we risk modifying
460+
# the original event.
461+
edit_content = edit.content.copy()
462+
463+
# Unfreeze the event content if necessary, so that we may modify it below
464+
edit_content = unfreeze(edit_content)
465+
serialized_event["content"] = edit_content.get("m.new_content", {})
466+
467+
# Check for existing relations
468+
relates_to = event.content.get("m.relates_to")
469+
if relates_to:
470+
# Keep the relations, ensuring we use a dict copy of the original
471+
serialized_event["content"]["m.relates_to"] = relates_to.copy()
472+
else:
473+
serialized_event["content"].pop("m.relates_to", None)
474+
475+
relations[RelationTypes.REPLACE] = {
476+
"event_id": edit.event_id,
477+
"origin_server_ts": edit.origin_server_ts,
478+
"sender": edit.sender,
479+
}
480+
481+
# If this event is the start of a thread, include a summary of the replies.
482+
if self._msc3440_enabled:
483+
(
484+
thread_count,
485+
latest_thread_event,
486+
) = await self.store.get_thread_summary(event_id)
487+
if latest_thread_event:
488+
relations[RelationTypes.THREAD] = {
489+
# Don't bundle aggregations as this could recurse forever.
490+
"latest_event": await self.serialize_event(
491+
latest_thread_event, time_now, bundle_aggregations=False
492+
),
493+
"count": thread_count,
494+
}
495+
496+
# If any bundled relations were found, include them.
497+
if relations:
498+
serialized_event["unsigned"].setdefault("m.relations", {}).update(relations)
499+
488500
async def serialize_events(
489501
self, events: Iterable[Union[JsonDict, EventBase]], time_now: int, **kwargs: Any
490502
) -> List[JsonDict]:

0 commit comments

Comments
 (0)