@@ -350,12 +350,15 @@ async def _inherit_depth_from_prev_ids(self, prev_event_ids) -> int:
350350
351351 return depth
352352
353- def _create_insertion_event_dict (self , sender : str , origin_server_ts : int ):
353+ def _create_insertion_event_dict (
354+ self , sender : str , room_id : str , origin_server_ts : int
355+ ):
354356 """Creates an event dict for an "insertion" event with the proper fields
355357 and a random chunk ID.
356358
357359 Args:
358360 sender: The event author MXID
361+ room_id: The room ID that the event belongs to
359362 origin_server_ts: Timestamp when the event was sent
360363
361364 Returns:
@@ -366,6 +369,7 @@ def _create_insertion_event_dict(self, sender: str, origin_server_ts: int):
366369 insertion_event = {
367370 "type" : EventTypes .MSC2716_INSERTION ,
368371 "sender" : sender ,
372+ "room_id" : room_id ,
369373 "content" : {
370374 EventContentFields .MSC2716_NEXT_CHUNK_ID : next_chunk_id ,
371375 EventContentFields .MSC2716_HISTORICAL : True ,
@@ -479,11 +483,17 @@ async def on_POST(self, request, room_id):
479483
480484 events_to_create = body ["events" ]
481485
486+ prev_event_ids = prev_events_from_query
487+ inherited_depth = await self ._inherit_depth_from_prev_ids (
488+ prev_events_from_query
489+ )
490+
482491 # Figure out which chunk to connect to. If they passed in
483492 # chunk_id_from_query let's use it. The chunk ID passed in comes
484493 # from the chunk_id in the "insertion" event from the previous chunk.
485494 last_event_in_chunk = events_to_create [- 1 ]
486495 chunk_id_to_connect_to = chunk_id_from_query
496+ base_insertion_event = None
487497 if chunk_id_from_query :
488498 # TODO: Verify the chunk_id_from_query corresponds to an insertion event
489499 pass
@@ -495,11 +505,25 @@ async def on_POST(self, request, room_id):
495505 # an insertion event), in which case we just create a new insertion event
496506 # that can then get pointed to by a "marker" event later.
497507 else :
498- base_insertion_event = self ._create_insertion_event_dict (
508+ base_insertion_event_dict = self ._create_insertion_event_dict (
499509 sender = requester .user .to_string (),
510+ room_id = room_id ,
500511 origin_server_ts = last_event_in_chunk ["origin_server_ts" ],
501512 )
502- events_to_create .append (base_insertion_event )
513+ base_insertion_event_dict ["prev_events" ] = prev_event_ids .copy ()
514+
515+ (
516+ base_insertion_event ,
517+ _ ,
518+ ) = await self .event_creation_handler .create_and_send_nonmember_event (
519+ requester ,
520+ base_insertion_event_dict ,
521+ prev_event_ids = base_insertion_event_dict .get ("prev_events" ),
522+ auth_event_ids = auth_event_ids ,
523+ historical = True ,
524+ depth = inherited_depth ,
525+ )
526+
503527 chunk_id_to_connect_to = base_insertion_event ["content" ][
504528 EventContentFields .MSC2716_NEXT_CHUNK_ID
505529 ]
@@ -513,6 +537,7 @@ async def on_POST(self, request, room_id):
513537 # event in the chunk) so the next chunk can be connected to this one.
514538 insertion_event = self ._create_insertion_event_dict (
515539 sender = requester .user .to_string (),
540+ room_id = room_id ,
516541 # Since the insertion event is put at the start of the chunk,
517542 # where the oldest-in-time event is, copy the origin_server_ts from
518543 # the first event we're inserting
@@ -521,12 +546,7 @@ async def on_POST(self, request, room_id):
521546 # Prepend the insertion event to the start of the chunk
522547 events_to_create = [insertion_event ] + events_to_create
523548
524- inherited_depth = await self ._inherit_depth_from_prev_ids (
525- prev_events_from_query
526- )
527-
528549 event_ids = []
529- prev_event_ids = prev_events_from_query
530550 events_to_persist = []
531551 for ev in events_to_create :
532552 assert_params_in_dict (ev , ["type" , "origin_server_ts" , "content" , "sender" ])
@@ -580,6 +600,10 @@ async def on_POST(self, request, room_id):
580600 context = context ,
581601 )
582602
603+ # Add the base_insertion_event to the bottom of the list we return
604+ if base_insertion_event is not None :
605+ event_ids .append (base_insertion_event .event_id )
606+
583607 return 200 , {
584608 "state_events" : auth_event_ids ,
585609 "events" : event_ids ,
0 commit comments