@@ -306,6 +306,7 @@ def format_event_for_client_v2_without_room_id(d: JsonDict) -> JsonDict:
306306def serialize_event (
307307 e : Union [JsonDict , EventBase ],
308308 time_now_ms : int ,
309+ * ,
309310 as_client_event : bool = True ,
310311 event_format : Callable [[JsonDict ], JsonDict ] = format_event_for_client_v1 ,
311312 token_id : Optional [str ] = None ,
@@ -393,16 +394,18 @@ async def serialize_event(
393394 self ,
394395 event : Union [JsonDict , EventBase ],
395396 time_now : int ,
396- bundle_relations : bool = True ,
397+ * ,
398+ bundle_aggregations : bool = True ,
397399 ** kwargs : Any ,
398400 ) -> JsonDict :
399401 """Serializes a single event.
400402
401403 Args:
402404 event: The event being serialized.
403405 time_now: The current time in milliseconds
404- bundle_relations: Whether to include the bundled relations for this
405- event.
406+ bundle_aggregations: Whether to include the bundled aggregations for this
407+ event. Only applies to non-state events. (State events never include
408+ bundled aggregations.)
406409 **kwargs: Arguments to pass to `serialize_event`
407410
408411 Returns:
@@ -414,28 +417,35 @@ async def serialize_event(
414417
415418 serialized_event = serialize_event (event , time_now , ** kwargs )
416419
417- # If MSC1849 is enabled then we need to look if there are any relations
418- # we need to bundle in with the event.
419- # Do not bundle relations if the event has been redacted
420- if not event .internal_metadata .is_redacted () and (
421- self ._msc1849_enabled and bundle_relations
420+ # Check if there are any bundled aggregations to include with the event.
421+ #
422+ # Do not bundle aggregations if any of the following at true:
423+ #
424+ # * Support is disabled via the configuration or the caller.
425+ # * The event is a state event.
426+ # * The event has been redacted.
427+ if (
428+ self ._msc1849_enabled
429+ and bundle_aggregations
430+ and not event .is_state ()
431+ and not event .internal_metadata .is_redacted ()
422432 ):
423- await self ._injected_bundled_relations (event , time_now , serialized_event )
433+ await self ._injected_bundled_aggregations (event , time_now , serialized_event )
424434
425435 return serialized_event
426436
427- async def _injected_bundled_relations (
437+ async def _injected_bundled_aggregations (
428438 self , event : EventBase , time_now : int , serialized_event : JsonDict
429439 ) -> None :
430- """Potentially injects bundled relations into the unsigned portion of the serialized event.
440+ """Potentially injects bundled aggregations into the unsigned portion of the serialized event.
431441
432442 Args:
433443 event: The event being serialized.
434444 time_now: The current time in milliseconds
435445 serialized_event: The serialized event which may be modified.
436446
437447 """
438- # Do not bundle relations for an event which represents an edit or an
448+ # Do not bundle aggregations for an event which represents an edit or an
439449 # annotation. It does not make sense for them to have related events.
440450 relates_to = event .content .get ("m.relates_to" )
441451 if isinstance (relates_to , (dict , frozendict )):
@@ -445,18 +455,18 @@ async def _injected_bundled_relations(
445455
446456 event_id = event .event_id
447457
448- # The bundled relations to include.
449- relations = {}
458+ # The bundled aggregations to include.
459+ aggregations = {}
450460
451461 annotations = await self .store .get_aggregation_groups_for_event (event_id )
452462 if annotations .chunk :
453- relations [RelationTypes .ANNOTATION ] = annotations .to_dict ()
463+ aggregations [RelationTypes .ANNOTATION ] = annotations .to_dict ()
454464
455465 references = await self .store .get_relations_for_event (
456466 event_id , RelationTypes .REFERENCE , direction = "f"
457467 )
458468 if references .chunk :
459- relations [RelationTypes .REFERENCE ] = references .to_dict ()
469+ aggregations [RelationTypes .REFERENCE ] = references .to_dict ()
460470
461471 edit = None
462472 if event .type == EventTypes .Message :
@@ -482,7 +492,7 @@ async def _injected_bundled_relations(
482492 else :
483493 serialized_event ["content" ].pop ("m.relates_to" , None )
484494
485- relations [RelationTypes .REPLACE ] = {
495+ aggregations [RelationTypes .REPLACE ] = {
486496 "event_id" : edit .event_id ,
487497 "origin_server_ts" : edit .origin_server_ts ,
488498 "sender" : edit .sender ,
@@ -495,17 +505,19 @@ async def _injected_bundled_relations(
495505 latest_thread_event ,
496506 ) = await self .store .get_thread_summary (event_id )
497507 if latest_thread_event :
498- relations [RelationTypes .THREAD ] = {
499- # Don't bundle relations as this could recurse forever.
508+ aggregations [RelationTypes .THREAD ] = {
509+ # Don't bundle aggregations as this could recurse forever.
500510 "latest_event" : await self .serialize_event (
501- latest_thread_event , time_now , bundle_relations = False
511+ latest_thread_event , time_now , bundle_aggregations = False
502512 ),
503513 "count" : thread_count ,
504514 }
505515
506- # If any bundled relations were found, include them.
507- if relations :
508- serialized_event ["unsigned" ].setdefault ("m.relations" , {}).update (relations )
516+ # If any bundled aggregations were found, include them.
517+ if aggregations :
518+ serialized_event ["unsigned" ].setdefault ("m.relations" , {}).update (
519+ aggregations
520+ )
509521
510522 async def serialize_events (
511523 self , events : Iterable [Union [JsonDict , EventBase ]], time_now : int , ** kwargs : Any
0 commit comments