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

Commit 4dfa4ff

Browse files
committed
Make base insertion event float off on its own
See #10250 (comment)
1 parent 9d60613 commit 4dfa4ff

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

synapse/handlers/message.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ async def create_event(
518518
outlier: Indicates whether the event is an `outlier`, i.e. if
519519
it's from an arbitrary point and floating in the DAG as
520520
opposed to being inline with the current DAG.
521+
historical: Indicates whether the message is being inserted
522+
back in time around some existing events. This is used to skip
523+
a few checks and mark the event as backfilled.
521524
depth: Override the depth used to order the event in the DAG.
522525
Should normally be set to None, which will cause the depth to be calculated
523526
based on the prev_events.
@@ -772,6 +775,7 @@ async def create_and_send_nonmember_event(
772775
txn_id: Optional[str] = None,
773776
ignore_shadow_ban: bool = False,
774777
outlier: bool = False,
778+
historical: bool = False,
775779
depth: Optional[int] = None,
776780
) -> Tuple[EventBase, int]:
777781
"""
@@ -799,6 +803,9 @@ async def create_and_send_nonmember_event(
799803
outlier: Indicates whether the event is an `outlier`, i.e. if
800804
it's from an arbitrary point and floating in the DAG as
801805
opposed to being inline with the current DAG.
806+
historical: Indicates whether the message is being inserted
807+
back in time around some existing events. This is used to skip
808+
a few checks and mark the event as backfilled.
802809
depth: Override the depth used to order the event in the DAG.
803810
Should normally be set to None, which will cause the depth to be calculated
804811
based on the prev_events.
@@ -847,6 +854,7 @@ async def create_and_send_nonmember_event(
847854
prev_event_ids=prev_event_ids,
848855
auth_event_ids=auth_event_ids,
849856
outlier=outlier,
857+
historical=historical,
850858
depth=depth,
851859
)
852860

synapse/rest/client/v1/room.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)