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

Add base starting insertion event when no chunk ID is provided (MSC2716) #10250

Merged
merged 5 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10250.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add base starting insertion event when no chunk ID is specified in the historical batch send API.
76 changes: 56 additions & 20 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,30 @@ async def inherit_depth_from_prev_ids(self, prev_event_ids) -> int:

return depth

def _create_insertion_event_dict(self, sender: str, origin_server_ts: int):
"""
Creates an event dict for an "insertion" event with the proper fields
and a random chunk ID.
Args:
sender: The event author MXID
origin_server_ts: Timestamp when the event was sent
Returns:
Tuple of event ID and stream ordering position
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
"""

next_chunk_id = random_string(64)
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
insertion_event = {
"type": EventTypes.MSC2716_INSERTION,
"sender": sender,
"content": {
EventContentFields.MSC2716_NEXT_CHUNK_ID: next_chunk_id,
EventContentFields.MSC2716_HISTORICAL: True,
},
"origin_server_ts": origin_server_ts,
}

return insertion_event

async def on_POST(self, request, room_id):
requester = await self.auth.get_user_by_req(request, allow_guest=False)

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

events_to_create = body["events"]

# If provided, connect the chunk to the last insertion point
# The chunk ID passed in comes from the chunk_id in the
# "insertion" event from the previous chunk.
# Figure out which chunk to connect to. If they passed in
# chunk_id_from_query let's use it. The chunk ID passed in comes
# from the chunk_id in the "insertion" event from the previous chunk.
last_event_in_chunk = events_to_create[-1]
chunk_id_to_connect_to = chunk_id_from_query
if chunk_id_from_query:
last_event_in_chunk = events_to_create[-1]
last_event_in_chunk["content"][
EventContentFields.MSC2716_CHUNK_ID
] = chunk_id_from_query
# TODO: Verify the chunk_id_from_query corresponds to an insertion event
pass
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a database index to make this lookup fast. Going to add this in #10245

# Otherwise, create an insertion event to be based off of and connect
# to as a starting point.
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
else:
base_insertion_event = self._create_insertion_event_dict(
sender=requester.user.to_string(),
origin_server_ts=last_event_in_chunk["origin_server_ts"],
)
events_to_create.append(base_insertion_event)
chunk_id_to_connect_to = base_insertion_event["content"][
EventContentFields.MSC2716_NEXT_CHUNK_ID
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is where we are creating the base insertion event


# Connect this current chunk to the insertion event from the previous chunk
last_event_in_chunk["content"][
EventContentFields.MSC2716_CHUNK_ID
] = chunk_id_to_connect_to

# Add an "insertion" event to the start of each chunk (next to the oldest
# Add an "insertion" event to the start of each chunk (next to the oldest-in-time
# event in the chunk) so the next chunk can be connected to this one.
next_chunk_id = random_string(64)
insertion_event = {
"type": EventTypes.MSC2716_INSERTION,
"sender": requester.user.to_string(),
"content": {
EventContentFields.MSC2716_NEXT_CHUNK_ID: next_chunk_id,
EventContentFields.MSC2716_HISTORICAL: True,
},
insertion_event = self._create_insertion_event_dict(
sender=requester.user.to_string(),
# Since the insertion event is put at the start of the chunk,
# where the oldest event is, copy the origin_server_ts from
# where the oldest-in-time event is, copy the origin_server_ts from
# the first event we're inserting
"origin_server_ts": events_to_create[0]["origin_server_ts"],
}
origin_server_ts=events_to_create[0]["origin_server_ts"],
)
# Prepend the insertion event to the start of the chunk
events_to_create = [insertion_event] + events_to_create

Expand Down Expand Up @@ -536,7 +570,9 @@ async def on_POST(self, request, room_id):
return 200, {
"state_events": auth_event_ids,
"events": event_ids,
"next_chunk_id": next_chunk_id,
"next_chunk_id": insertion_event["content"][
EventContentFields.MSC2716_NEXT_CHUNK_ID
],
}

def on_GET(self, request, room_id):
Expand Down