@@ -306,6 +306,7 @@ def format_event_for_client_v2_without_room_id(d: JsonDict) -> JsonDict:
306
306
def serialize_event (
307
307
e : Union [JsonDict , EventBase ],
308
308
time_now_ms : int ,
309
+ * ,
309
310
as_client_event : bool = True ,
310
311
event_format : Callable [[JsonDict ], JsonDict ] = format_event_for_client_v1 ,
311
312
token_id : Optional [str ] = None ,
@@ -393,16 +394,18 @@ async def serialize_event(
393
394
self ,
394
395
event : Union [JsonDict , EventBase ],
395
396
time_now : int ,
396
- bundle_relations : bool = True ,
397
+ * ,
398
+ bundle_aggregations : bool = True ,
397
399
** kwargs : Any ,
398
400
) -> JsonDict :
399
401
"""Serializes a single event.
400
402
401
403
Args:
402
404
event: The event being serialized.
403
405
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.)
406
409
**kwargs: Arguments to pass to `serialize_event`
407
410
408
411
Returns:
@@ -414,28 +417,35 @@ async def serialize_event(
414
417
415
418
serialized_event = serialize_event (event , time_now , ** kwargs )
416
419
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 ()
422
432
):
423
- await self ._injected_bundled_relations (event , time_now , serialized_event )
433
+ await self ._injected_bundled_aggregations (event , time_now , serialized_event )
424
434
425
435
return serialized_event
426
436
427
- async def _injected_bundled_relations (
437
+ async def _injected_bundled_aggregations (
428
438
self , event : EventBase , time_now : int , serialized_event : JsonDict
429
439
) -> 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.
431
441
432
442
Args:
433
443
event: The event being serialized.
434
444
time_now: The current time in milliseconds
435
445
serialized_event: The serialized event which may be modified.
436
446
437
447
"""
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
439
449
# annotation. It does not make sense for them to have related events.
440
450
relates_to = event .content .get ("m.relates_to" )
441
451
if isinstance (relates_to , (dict , frozendict )):
@@ -445,18 +455,18 @@ async def _injected_bundled_relations(
445
455
446
456
event_id = event .event_id
447
457
448
- # The bundled relations to include.
449
- relations = {}
458
+ # The bundled aggregations to include.
459
+ aggregations = {}
450
460
451
461
annotations = await self .store .get_aggregation_groups_for_event (event_id )
452
462
if annotations .chunk :
453
- relations [RelationTypes .ANNOTATION ] = annotations .to_dict ()
463
+ aggregations [RelationTypes .ANNOTATION ] = annotations .to_dict ()
454
464
455
465
references = await self .store .get_relations_for_event (
456
466
event_id , RelationTypes .REFERENCE , direction = "f"
457
467
)
458
468
if references .chunk :
459
- relations [RelationTypes .REFERENCE ] = references .to_dict ()
469
+ aggregations [RelationTypes .REFERENCE ] = references .to_dict ()
460
470
461
471
edit = None
462
472
if event .type == EventTypes .Message :
@@ -482,7 +492,7 @@ async def _injected_bundled_relations(
482
492
else :
483
493
serialized_event ["content" ].pop ("m.relates_to" , None )
484
494
485
- relations [RelationTypes .REPLACE ] = {
495
+ aggregations [RelationTypes .REPLACE ] = {
486
496
"event_id" : edit .event_id ,
487
497
"origin_server_ts" : edit .origin_server_ts ,
488
498
"sender" : edit .sender ,
@@ -495,17 +505,19 @@ async def _injected_bundled_relations(
495
505
latest_thread_event ,
496
506
) = await self .store .get_thread_summary (event_id )
497
507
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.
500
510
"latest_event" : await self .serialize_event (
501
- latest_thread_event , time_now , bundle_relations = False
511
+ latest_thread_event , time_now , bundle_aggregations = False
502
512
),
503
513
"count" : thread_count ,
504
514
}
505
515
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
+ )
509
521
510
522
async def serialize_events (
511
523
self , events : Iterable [Union [JsonDict , EventBase ]], time_now : int , ** kwargs : Any
0 commit comments