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

Commit b703962

Browse files
committed
Make base insertion event float off on its own
See #10250 (comment) Conflicts: synapse/rest/client/v1/room.py
1 parent 04b1f7e commit b703962

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
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: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,15 @@ async def _inherit_depth_from_prev_ids(self, prev_event_ids) -> int:
350350

351351
return depth
352352

353-
def _create_insertion_event_dict(self, sender: str, origin_server_ts: int):
353+
def _create_insertion_event_dict(
354+
self, sender: str, room_id: str, origin_server_ts: int
355+
):
354356
"""Creates an event dict for an "insertion" event with the proper fields
355357
and a random chunk ID.
356358
357359
Args:
358360
sender: The event author MXID
361+
room_id: The room ID that the event belongs to
359362
origin_server_ts: Timestamp when the event was sent
360363
361364
Returns:
@@ -366,6 +369,7 @@ def _create_insertion_event_dict(self, sender: str, origin_server_ts: int):
366369
insertion_event = {
367370
"type": EventTypes.MSC2716_INSERTION,
368371
"sender": sender,
372+
"room_id": room_id,
369373
"content": {
370374
EventContentFields.MSC2716_NEXT_CHUNK_ID: next_chunk_id,
371375
EventContentFields.MSC2716_HISTORICAL: True,
@@ -479,11 +483,17 @@ async def on_POST(self, request, room_id):
479483

480484
events_to_create = body["events"]
481485

486+
prev_event_ids = prev_events_from_query
487+
inherited_depth = await self._inherit_depth_from_prev_ids(
488+
prev_events_from_query
489+
)
490+
482491
# Figure out which chunk to connect to. If they passed in
483492
# chunk_id_from_query let's use it. The chunk ID passed in comes
484493
# from the chunk_id in the "insertion" event from the previous chunk.
485494
last_event_in_chunk = events_to_create[-1]
486495
chunk_id_to_connect_to = chunk_id_from_query
496+
base_insertion_event = None
487497
if chunk_id_from_query:
488498
# TODO: Verify the chunk_id_from_query corresponds to an insertion event
489499
pass
@@ -495,11 +505,25 @@ async def on_POST(self, request, room_id):
495505
# an insertion event), in which case we just create a new insertion event
496506
# that can then get pointed to by a "marker" event later.
497507
else:
498-
base_insertion_event = self._create_insertion_event_dict(
508+
base_insertion_event_dict = self._create_insertion_event_dict(
499509
sender=requester.user.to_string(),
510+
room_id=room_id,
500511
origin_server_ts=last_event_in_chunk["origin_server_ts"],
501512
)
502-
events_to_create.append(base_insertion_event)
513+
base_insertion_event_dict["prev_events"] = prev_event_ids.copy()
514+
515+
(
516+
base_insertion_event,
517+
_,
518+
) = await self.event_creation_handler.create_and_send_nonmember_event(
519+
requester,
520+
base_insertion_event_dict,
521+
prev_event_ids=base_insertion_event_dict.get("prev_events"),
522+
auth_event_ids=auth_event_ids,
523+
historical=True,
524+
depth=inherited_depth,
525+
)
526+
503527
chunk_id_to_connect_to = base_insertion_event["content"][
504528
EventContentFields.MSC2716_NEXT_CHUNK_ID
505529
]
@@ -513,6 +537,7 @@ async def on_POST(self, request, room_id):
513537
# event in the chunk) so the next chunk can be connected to this one.
514538
insertion_event = self._create_insertion_event_dict(
515539
sender=requester.user.to_string(),
540+
room_id=room_id,
516541
# Since the insertion event is put at the start of the chunk,
517542
# where the oldest-in-time event is, copy the origin_server_ts from
518543
# the first event we're inserting
@@ -521,12 +546,7 @@ async def on_POST(self, request, room_id):
521546
# Prepend the insertion event to the start of the chunk
522547
events_to_create = [insertion_event] + events_to_create
523548

524-
inherited_depth = await self._inherit_depth_from_prev_ids(
525-
prev_events_from_query
526-
)
527-
528549
event_ids = []
529-
prev_event_ids = prev_events_from_query
530550
events_to_persist = []
531551
for ev in events_to_create:
532552
assert_params_in_dict(ev, ["type", "origin_server_ts", "content", "sender"])
@@ -580,6 +600,10 @@ async def on_POST(self, request, room_id):
580600
context=context,
581601
)
582602

603+
# Add the base_insertion_event to the bottom of the list we return
604+
if base_insertion_event is not None:
605+
event_ids.append(base_insertion_event.event_id)
606+
583607
return 200, {
584608
"state_events": auth_event_ids,
585609
"events": event_ids,

0 commit comments

Comments
 (0)