@@ -349,12 +349,15 @@ async def inherit_depth_from_prev_ids(self, prev_event_ids) -> int:
349349
350350        return  depth 
351351
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+     ):
353355        """Creates an event dict for an "insertion" event with the proper fields 
354356        and a random chunk ID. 
355357
356358        Args: 
357359            sender: The event author MXID 
360+             room_id: The room ID that the event belongs to 
358361            origin_server_ts: Timestamp when the event was sent 
359362
360363        Returns: 
@@ -365,6 +368,7 @@ def _create_insertion_event_dict(self, sender: str, origin_server_ts: int):
365368        insertion_event  =  {
366369            "type" : EventTypes .MSC2716_INSERTION ,
367370            "sender" : sender ,
371+             "room_id" : room_id ,
368372            "content" : {
369373                EventContentFields .MSC2716_NEXT_CHUNK_ID : next_chunk_id ,
370374                EventContentFields .MSC2716_HISTORICAL : True ,
@@ -474,11 +478,15 @@ async def on_POST(self, request, room_id):
474478
475479        events_to_create  =  body ["events" ]
476480
481+         prev_event_ids  =  prev_events_from_query 
482+         inherited_depth  =  await  self .inherit_depth_from_prev_ids (prev_events_from_query )
483+ 
477484        # Figure out which chunk to connect to. If they passed in 
478485        # chunk_id_from_query let's use it. The chunk ID passed in comes 
479486        # from the chunk_id in the "insertion" event from the previous chunk. 
480487        last_event_in_chunk  =  events_to_create [- 1 ]
481488        chunk_id_to_connect_to  =  chunk_id_from_query 
489+         base_insertion_event  =  None 
482490        if  chunk_id_from_query :
483491            # TODO: Verify the chunk_id_from_query corresponds to an insertion event 
484492            pass 
@@ -490,11 +498,25 @@ async def on_POST(self, request, room_id):
490498        # an insertion event), in which case we just create a new insertion event 
491499        # that can then get pointed to by a "marker" event later. 
492500        else :
493-             base_insertion_event  =  self ._create_insertion_event_dict (
501+             base_insertion_event_dict  =  self ._create_insertion_event_dict (
494502                sender = requester .user .to_string (),
503+                 room_id = room_id ,
495504                origin_server_ts = last_event_in_chunk ["origin_server_ts" ],
496505            )
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+ 
498520            chunk_id_to_connect_to  =  base_insertion_event ["content" ][
499521                EventContentFields .MSC2716_NEXT_CHUNK_ID 
500522            ]
@@ -508,6 +530,7 @@ async def on_POST(self, request, room_id):
508530        # event in the chunk) so the next chunk can be connected to this one. 
509531        insertion_event  =  self ._create_insertion_event_dict (
510532            sender = requester .user .to_string (),
533+             room_id = room_id ,
511534            # Since the insertion event is put at the start of the chunk, 
512535            # where the oldest-in-time event is, copy the origin_server_ts from 
513536            # the first event we're inserting 
@@ -516,10 +539,7 @@ async def on_POST(self, request, room_id):
516539        # Prepend the insertion event to the start of the chunk 
517540        events_to_create  =  [insertion_event ] +  events_to_create 
518541
519-         inherited_depth  =  await  self .inherit_depth_from_prev_ids (prev_events_from_query )
520- 
521542        event_ids  =  []
522-         prev_event_ids  =  prev_events_from_query 
523543        events_to_persist  =  []
524544        for  ev  in  events_to_create :
525545            assert_params_in_dict (ev , ["type" , "origin_server_ts" , "content" , "sender" ])
@@ -573,6 +593,10 @@ async def on_POST(self, request, room_id):
573593                context = context ,
574594            )
575595
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+ 
576600        return  200 , {
577601            "state_events" : auth_event_ids ,
578602            "events" : event_ids ,
0 commit comments