Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ceec7f7

Browse files
committed
Add base starting insertion point when no chunk ID is provided
This is so we can have the marker event point to this initial insertion event and be able to traverse the events in the first chunk.
1 parent 33701dc commit ceec7f7

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

synapse/rest/client/v1/room.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,30 @@ 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):
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+
352376
async def on_POST(self, request, room_id):
353377
requester = await self.auth.get_user_by_req(request, allow_guest=False)
354378

@@ -449,30 +473,40 @@ async def on_POST(self, request, room_id):
449473

450474
events_to_create = body["events"]
451475

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
455481
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
460500

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
462502
# 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(),
471505
# 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
473507
# 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+
)
476510
# Prepend the insertion event to the start of the chunk
477511
events_to_create = [insertion_event] + events_to_create
478512

@@ -536,7 +570,9 @@ async def on_POST(self, request, room_id):
536570
return 200, {
537571
"state_events": auth_event_ids,
538572
"events": event_ids,
539-
"next_chunk_id": next_chunk_id,
573+
"next_chunk_id": insertion_event["content"][
574+
EventContentFields.MSC2716_NEXT_CHUNK_ID
575+
],
540576
}
541577

542578
def on_GET(self, request, room_id):

0 commit comments

Comments
 (0)