@@ -349,6 +349,30 @@ 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 ):
353
+ """
354
+ Creates an event dict for an "insertion" event with the proper fields
355
+ and a random chunk ID.
356
+ Args:
357
+ sender: The event author MXID
358
+ origin_server_ts: Timestamp when the event was sent
359
+ Returns:
360
+ Tuple of event ID and stream ordering position
361
+ """
362
+
363
+ next_chunk_id = random_string (64 )
364
+ insertion_event = {
365
+ "type" : EventTypes .MSC2716_INSERTION ,
366
+ "sender" : sender ,
367
+ "content" : {
368
+ EventContentFields .MSC2716_NEXT_CHUNK_ID : next_chunk_id ,
369
+ EventContentFields .MSC2716_HISTORICAL : True ,
370
+ },
371
+ "origin_server_ts" : origin_server_ts ,
372
+ }
373
+
374
+ return insertion_event
375
+
352
376
async def on_POST (self , request , room_id ):
353
377
requester = await self .auth .get_user_by_req (request , allow_guest = False )
354
378
@@ -449,30 +473,40 @@ async def on_POST(self, request, room_id):
449
473
450
474
events_to_create = body ["events" ]
451
475
452
- # If provided, connect the chunk to the last insertion point
453
- # The chunk ID passed in comes from the chunk_id in the
454
- # "insertion" event from the previous chunk.
476
+ # Figure out which chunk to connect to. If they passed in
477
+ # chunk_id_from_query let's use it. The chunk ID passed in comes
478
+ # from the chunk_id in the "insertion" event from the previous chunk.
479
+ last_event_in_chunk = events_to_create [- 1 ]
480
+ chunk_id_to_connect_to = chunk_id_from_query
455
481
if chunk_id_from_query :
456
- last_event_in_chunk = events_to_create [- 1 ]
457
- last_event_in_chunk ["content" ][
458
- EventContentFields .MSC2716_CHUNK_ID
459
- ] = chunk_id_from_query
482
+ # TODO: Verify the chunk_id_from_query corresponds to an insertion event
483
+ pass
484
+ # Otherwise, create an insertion event to be based off of and connect
485
+ # to as a starting point.
486
+ else :
487
+ base_insertion_event = self ._create_insertion_event_dict (
488
+ sender = requester .user .to_string (),
489
+ origin_server_ts = last_event_in_chunk ["origin_server_ts" ],
490
+ )
491
+ events_to_create .append (base_insertion_event )
492
+ chunk_id_to_connect_to = base_insertion_event ["content" ][
493
+ EventContentFields .MSC2716_NEXT_CHUNK_ID
494
+ ]
495
+
496
+ # Connect this current chunk to the insertion event from the previous chunk
497
+ last_event_in_chunk ["content" ][
498
+ EventContentFields .MSC2716_CHUNK_ID
499
+ ] = chunk_id_to_connect_to
460
500
461
- # Add an "insertion" event to the start of each chunk (next to the oldest
501
+ # Add an "insertion" event to the start of each chunk (next to the oldest-in-time
462
502
# event in the chunk) so the next chunk can be connected to this one.
463
- next_chunk_id = random_string (64 )
464
- insertion_event = {
465
- "type" : EventTypes .MSC2716_INSERTION ,
466
- "sender" : requester .user .to_string (),
467
- "content" : {
468
- EventContentFields .MSC2716_NEXT_CHUNK_ID : next_chunk_id ,
469
- EventContentFields .MSC2716_HISTORICAL : True ,
470
- },
503
+ insertion_event = self ._create_insertion_event_dict (
504
+ sender = requester .user .to_string (),
471
505
# Since the insertion event is put at the start of the chunk,
472
- # where the oldest event is, copy the origin_server_ts from
506
+ # where the oldest-in-time event is, copy the origin_server_ts from
473
507
# the first event we're inserting
474
- " origin_server_ts" : events_to_create [0 ]["origin_server_ts" ],
475
- }
508
+ origin_server_ts = events_to_create [0 ]["origin_server_ts" ],
509
+ )
476
510
# Prepend the insertion event to the start of the chunk
477
511
events_to_create = [insertion_event ] + events_to_create
478
512
@@ -536,7 +570,9 @@ async def on_POST(self, request, room_id):
536
570
return 200 , {
537
571
"state_events" : auth_event_ids ,
538
572
"events" : event_ids ,
539
- "next_chunk_id" : next_chunk_id ,
573
+ "next_chunk_id" : insertion_event ["content" ][
574
+ EventContentFields .MSC2716_NEXT_CHUNK_ID
575
+ ],
540
576
}
541
577
542
578
def on_GET (self , request , room_id ):
0 commit comments