From 7833ce0beb769109a0fbcb740151082e526fe38b Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Tue, 26 Oct 2021 17:34:05 -0500 Subject: [PATCH 1/5] Support sending no ?state_events_at_start As brought up by @tulir, https://matrix.to/#/!SBYNQlpqkwJzFIdzxI:nevarro.space/$Gwnb2ZvXHc3poYXuBhho0cmoYq4KJ11Jh3m5s8kjNOM?via=nevarro.space&via=beeper.com&via=matrix.org This use case only works if the user is already joined in the current room state at the given `?prev_event_id` --- synapse/rest/client/room_batch.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/synapse/rest/client/room_batch.py b/synapse/rest/client/room_batch.py index 99f8156ad0ec..e0dc04fc7840 100644 --- a/synapse/rest/client/room_batch.py +++ b/synapse/rest/client/room_batch.py @@ -131,20 +131,24 @@ async def on_POST( prev_event_ids_from_query ) + state_event_ids_at_start = [] # Create and persist all of the state events that float off on their own # before the batch. These will most likely be all of the invite/member # state events used to auth the upcoming historical messages. - state_event_ids_at_start = ( - await self.room_batch_handler.persist_state_events_at_start( - state_events_at_start=body["state_events_at_start"], - room_id=room_id, - initial_auth_event_ids=auth_event_ids, - app_service_requester=requester, + if body["state_events_at_start"] is not None and len( + body["state_events_at_start"] + ): + state_event_ids_at_start = ( + await self.room_batch_handler.persist_state_events_at_start( + state_events_at_start=body["state_events_at_start"], + room_id=room_id, + initial_auth_event_ids=auth_event_ids, + app_service_requester=requester, + ) ) - ) - # Update our ongoing auth event ID list with all of the new state we - # just created - auth_event_ids.extend(state_event_ids_at_start) + # Update our ongoing auth event ID list with all of the new state we + # just created + auth_event_ids.extend(state_event_ids_at_start) inherited_depth = await self.room_batch_handler.inherit_depth_from_prev_ids( prev_event_ids_from_query @@ -197,8 +201,11 @@ async def on_POST( # Also connect the historical event chain to the end of the floating # state chain, which causes the HS to ask for the state at the start of - # the batch later. - prev_event_ids = [state_event_ids_at_start[-1]] + # the batch later. If there is no state chain to connect to, just make + # the insertion event float itself. + prev_event_ids = [] + if len(state_event_ids_at_start): + prev_event_ids = [state_event_ids_at_start[-1]] # Create and persist all of the historical events as well as insertion # and batch meta events to make the batch navigable in the DAG. From 6286e7c0bc1d09e0663f15a47937ac9a00d5677e Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Tue, 26 Oct 2021 17:50:47 -0500 Subject: [PATCH 2/5] Add changelog --- changelog.d/11188.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/11188.bugfix diff --git a/changelog.d/11188.bugfix b/changelog.d/11188.bugfix new file mode 100644 index 000000000000..0688743c008b --- /dev/null +++ b/changelog.d/11188.bugfix @@ -0,0 +1 @@ +Allow an empty list of `state_events_at_start` to be sent when using the [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716) `/batch_send` endpoint and the author of the historical messages is already part of the current room state at the given `?prev_event_id`. From b350f46b47c26f575b961593c9e574529e6113e2 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 27 Oct 2021 23:10:40 -0500 Subject: [PATCH 3/5] Assert messages in order --- synapse/events/utils.py | 17 ++++++++++------- synapse/handlers/room.py | 1 + 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/synapse/events/utils.py b/synapse/events/utils.py index 6fa631aa1d4d..e3f01e6bdf35 100644 --- a/synapse/events/utils.py +++ b/synapse/events/utils.py @@ -283,13 +283,13 @@ def format_event_for_client_v1(d: JsonDict) -> JsonDict: def format_event_for_client_v2(d: JsonDict) -> JsonDict: drop_keys = ( - "auth_events", - "prev_events", - "hashes", - "signatures", - "depth", - "origin", - "prev_state", + # "auth_events", + # "prev_events", + # "hashes", + # "signatures", + # "depth", + # "origin", + # "prev_state", ) for key in drop_keys: d.pop(key, None) @@ -340,6 +340,9 @@ def serialize_event( d["event_id"] = e.event_id + # TODO: Remove + d["stream_ordering"] = e.internal_metadata.stream_ordering + if "age_ts" in d["unsigned"]: d["unsigned"]["age"] = time_now_ms - d["unsigned"]["age_ts"] del d["unsigned"]["age_ts"] diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index cf01d58ea104..1a1b8f1081e2 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -1156,6 +1156,7 @@ async def filter_evts(events: List[EventBase]) -> List[EventBase]: results = await self.store.get_events_around( room_id, event_id, before_limit, after_limit, event_filter ) + logger.info("results=%s", results) if event_filter: results["events_before"] = event_filter.filter(results["events_before"]) From 0751b4afbf4598c6b88207376d71136323edfb4a Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 27 Oct 2021 23:13:47 -0500 Subject: [PATCH 4/5] Revert "Assert messages in order" This reverts commit b350f46b47c26f575b961593c9e574529e6113e2. --- synapse/events/utils.py | 17 +++++++---------- synapse/handlers/room.py | 1 - 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/synapse/events/utils.py b/synapse/events/utils.py index e3f01e6bdf35..6fa631aa1d4d 100644 --- a/synapse/events/utils.py +++ b/synapse/events/utils.py @@ -283,13 +283,13 @@ def format_event_for_client_v1(d: JsonDict) -> JsonDict: def format_event_for_client_v2(d: JsonDict) -> JsonDict: drop_keys = ( - # "auth_events", - # "prev_events", - # "hashes", - # "signatures", - # "depth", - # "origin", - # "prev_state", + "auth_events", + "prev_events", + "hashes", + "signatures", + "depth", + "origin", + "prev_state", ) for key in drop_keys: d.pop(key, None) @@ -340,9 +340,6 @@ def serialize_event( d["event_id"] = e.event_id - # TODO: Remove - d["stream_ordering"] = e.internal_metadata.stream_ordering - if "age_ts" in d["unsigned"]: d["unsigned"]["age"] = time_now_ms - d["unsigned"]["age_ts"] del d["unsigned"]["age_ts"] diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 1a1b8f1081e2..cf01d58ea104 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -1156,7 +1156,6 @@ async def filter_evts(events: List[EventBase]) -> List[EventBase]: results = await self.store.get_events_around( room_id, event_id, before_limit, after_limit, event_filter ) - logger.info("results=%s", results) if event_filter: results["events_before"] = event_filter.filter(results["events_before"]) From eb33a0143c5896861e630fb0068e45a4cb2b0ea0 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 3 Nov 2021 02:21:44 -0500 Subject: [PATCH 5/5] Simplify logic See https://github.com/matrix-org/synapse/pull/11188#discussion_r738226483 --- synapse/rest/client/room_batch.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/synapse/rest/client/room_batch.py b/synapse/rest/client/room_batch.py index e0dc04fc7840..84c5ccbf8f58 100644 --- a/synapse/rest/client/room_batch.py +++ b/synapse/rest/client/room_batch.py @@ -135,9 +135,7 @@ async def on_POST( # Create and persist all of the state events that float off on their own # before the batch. These will most likely be all of the invite/member # state events used to auth the upcoming historical messages. - if body["state_events_at_start"] is not None and len( - body["state_events_at_start"] - ): + if body["state_events_at_start"]: state_event_ids_at_start = ( await self.room_batch_handler.persist_state_events_at_start( state_events_at_start=body["state_events_at_start"],