This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add base starting insertion event when no chunk ID is provided (MSC2716) #10250
Merged
MadLittleMods
merged 5 commits into
develop
from
madlittlemods/2716-base-insertion-event
Jul 9, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ceec7f7
Add base starting insertion point when no chunk ID is provided
MadLittleMods 3083660
Add changelog
MadLittleMods 6d0f561
Merge branch 'develop' into madlittlemods/2716-base-insertion-event
MadLittleMods 9d60613
Add more better comments
MadLittleMods 4dfa4ff
Make base insertion event float off on its own
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -349,6 +349,35 @@ async def inherit_depth_from_prev_ids(self, prev_event_ids) -> int: | |
|
||
return depth | ||
|
||
def _create_insertion_event_dict( | ||
self, sender: str, room_id: 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 | ||
room_id: The room ID that the event belongs to | ||
origin_server_ts: Timestamp when the event was sent | ||
|
||
Returns: | ||
Tuple of event ID and stream ordering position | ||
""" | ||
|
||
next_chunk_id = random_string(8) | ||
insertion_event = { | ||
"type": EventTypes.MSC2716_INSERTION, | ||
"sender": sender, | ||
"room_id": room_id, | ||
"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) | ||
|
||
|
@@ -449,37 +478,68 @@ 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. | ||
prev_event_ids = prev_events_from_query | ||
inherited_depth = await self.inherit_depth_from_prev_ids(prev_events_from_query) | ||
|
||
# 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 | ||
base_insertion_event = None | ||
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 | ||
# Otherwise, create an insertion event to act as a starting point. | ||
# | ||
# We don't always have an insertion event to start hanging more history | ||
# off of (ideally there would be one in the main DAG, but that's not the | ||
# case if we're wanting to add history to e.g. existing rooms without | ||
# an insertion event), in which case we just create a new insertion event | ||
# that can then get pointed to by a "marker" event later. | ||
else: | ||
base_insertion_event_dict = self._create_insertion_event_dict( | ||
sender=requester.user.to_string(), | ||
room_id=room_id, | ||
origin_server_ts=last_event_in_chunk["origin_server_ts"], | ||
) | ||
base_insertion_event_dict["prev_events"] = prev_event_ids.copy() | ||
|
||
# Add an "insertion" event to the start of each chunk (next to the oldest | ||
( | ||
base_insertion_event, | ||
_, | ||
) = await self.event_creation_handler.create_and_send_nonmember_event( | ||
requester, | ||
base_insertion_event_dict, | ||
prev_event_ids=base_insertion_event_dict.get("prev_events"), | ||
auth_event_ids=auth_event_ids, | ||
historical=True, | ||
depth=inherited_depth, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erikjohnston I've decided to make the base insertion event float off on its own so its easier to reason about in my head (all of them are hooked up consistently). |
||
|
||
chunk_id_to_connect_to = base_insertion_event["content"][ | ||
EventContentFields.MSC2716_NEXT_CHUNK_ID | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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-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(), | ||
room_id=room_id, | ||
# 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 | ||
|
||
inherited_depth = await self.inherit_depth_from_prev_ids(prev_events_from_query) | ||
|
||
event_ids = [] | ||
prev_event_ids = prev_events_from_query | ||
events_to_persist = [] | ||
for ev in events_to_create: | ||
assert_params_in_dict(ev, ["type", "origin_server_ts", "content", "sender"]) | ||
|
@@ -533,10 +593,16 @@ async def on_POST(self, request, room_id): | |
context=context, | ||
) | ||
|
||
# Add the base_insertion_event to the bottom of the list we return | ||
if base_insertion_event is not None: | ||
event_ids.append(base_insertion_event.event_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): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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