@@ -349,12 +349,15 @@ async def inherit_depth_from_prev_ids(self, prev_event_ids) -> int:
349
349
350
350
return depth
351
351
352
- def _create_insertion_event_dict (self , sender : str , origin_server_ts : int ):
352
+ def _create_insertion_event_dict (
353
+ self , sender : str , room_id : str , origin_server_ts : int
354
+ ):
353
355
"""Creates an event dict for an "insertion" event with the proper fields
354
356
and a random chunk ID.
355
357
356
358
Args:
357
359
sender: The event author MXID
360
+ room_id: The room ID that the event belongs to
358
361
origin_server_ts: Timestamp when the event was sent
359
362
360
363
Returns:
@@ -365,6 +368,7 @@ def _create_insertion_event_dict(self, sender: str, origin_server_ts: int):
365
368
insertion_event = {
366
369
"type" : EventTypes .MSC2716_INSERTION ,
367
370
"sender" : sender ,
371
+ "room_id" : room_id ,
368
372
"content" : {
369
373
EventContentFields .MSC2716_NEXT_CHUNK_ID : next_chunk_id ,
370
374
EventContentFields .MSC2716_HISTORICAL : True ,
@@ -474,11 +478,15 @@ async def on_POST(self, request, room_id):
474
478
475
479
events_to_create = body ["events" ]
476
480
481
+ prev_event_ids = prev_events_from_query
482
+ inherited_depth = await self .inherit_depth_from_prev_ids (prev_events_from_query )
483
+
477
484
# Figure out which chunk to connect to. If they passed in
478
485
# chunk_id_from_query let's use it. The chunk ID passed in comes
479
486
# from the chunk_id in the "insertion" event from the previous chunk.
480
487
last_event_in_chunk = events_to_create [- 1 ]
481
488
chunk_id_to_connect_to = chunk_id_from_query
489
+ base_insertion_event = None
482
490
if chunk_id_from_query :
483
491
# TODO: Verify the chunk_id_from_query corresponds to an insertion event
484
492
pass
@@ -490,11 +498,25 @@ async def on_POST(self, request, room_id):
490
498
# an insertion event), in which case we just create a new insertion event
491
499
# that can then get pointed to by a "marker" event later.
492
500
else :
493
- base_insertion_event = self ._create_insertion_event_dict (
501
+ base_insertion_event_dict = self ._create_insertion_event_dict (
494
502
sender = requester .user .to_string (),
503
+ room_id = room_id ,
495
504
origin_server_ts = last_event_in_chunk ["origin_server_ts" ],
496
505
)
497
- events_to_create .append (base_insertion_event )
506
+ base_insertion_event_dict ["prev_events" ] = prev_event_ids .copy ()
507
+
508
+ (
509
+ base_insertion_event ,
510
+ _ ,
511
+ ) = await self .event_creation_handler .create_and_send_nonmember_event (
512
+ requester ,
513
+ base_insertion_event_dict ,
514
+ prev_event_ids = base_insertion_event_dict .get ("prev_events" ),
515
+ auth_event_ids = auth_event_ids ,
516
+ historical = True ,
517
+ depth = inherited_depth ,
518
+ )
519
+
498
520
chunk_id_to_connect_to = base_insertion_event ["content" ][
499
521
EventContentFields .MSC2716_NEXT_CHUNK_ID
500
522
]
@@ -508,6 +530,7 @@ async def on_POST(self, request, room_id):
508
530
# event in the chunk) so the next chunk can be connected to this one.
509
531
insertion_event = self ._create_insertion_event_dict (
510
532
sender = requester .user .to_string (),
533
+ room_id = room_id ,
511
534
# Since the insertion event is put at the start of the chunk,
512
535
# where the oldest-in-time event is, copy the origin_server_ts from
513
536
# the first event we're inserting
@@ -516,10 +539,7 @@ async def on_POST(self, request, room_id):
516
539
# Prepend the insertion event to the start of the chunk
517
540
events_to_create = [insertion_event ] + events_to_create
518
541
519
- inherited_depth = await self .inherit_depth_from_prev_ids (prev_events_from_query )
520
-
521
542
event_ids = []
522
- prev_event_ids = prev_events_from_query
523
543
events_to_persist = []
524
544
for ev in events_to_create :
525
545
assert_params_in_dict (ev , ["type" , "origin_server_ts" , "content" , "sender" ])
@@ -573,6 +593,10 @@ async def on_POST(self, request, room_id):
573
593
context = context ,
574
594
)
575
595
596
+ # Add the base_insertion_event to the bottom of the list we return
597
+ if base_insertion_event is not None :
598
+ event_ids .append (base_insertion_event .event_id )
599
+
576
600
return 200 , {
577
601
"state_events" : auth_event_ids ,
578
602
"events" : event_ids ,
0 commit comments