From c5453df97b496c5692f66591c023c06ca3e992dd Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Tue, 7 Sep 2021 18:44:58 -0500 Subject: [PATCH 1/4] Split out /batch_send meta properties to their own fields Part of https://github.com/matrix-org/synapse/issues/10737 and more generally [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716) See https://github.com/matrix-org/matrix-doc/pull/2716#discussion_r684765034 --- synapse/rest/client/room_batch.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/synapse/rest/client/room_batch.py b/synapse/rest/client/room_batch.py index ed9697844833..c032817d0b81 100644 --- a/synapse/rest/client/room_batch.py +++ b/synapse/rest/client/room_batch.py @@ -15,6 +15,7 @@ import logging import re from typing import TYPE_CHECKING, Awaitable, List, Tuple +from http import HTTPStatus from twisted.web.server import Request @@ -179,7 +180,7 @@ async def on_POST( if not requester.app_service: raise AuthError( - 403, + HTTPStatus.FORBIDDEN, "Only application services can use the /batchsend endpoint", ) @@ -192,7 +193,7 @@ async def on_POST( if prev_events_from_query is None: raise SynapseError( - 400, + HTTPStatus.BAD_REQUEST, "prev_event query parameter is required when inserting historical messages back in time", errcode=Codes.MISSING_PARAM, ) @@ -213,7 +214,7 @@ async def on_POST( prev_state_ids = list(prev_state_map.values()) auth_event_ids = prev_state_ids - state_events_at_start = [] + state_event_ids_at_start = [] for state_event in body["state_events_at_start"]: assert_params_in_dict( state_event, ["type", "origin_server_ts", "content", "sender"] @@ -279,7 +280,7 @@ async def on_POST( ) event_id = event.event_id - state_events_at_start.append(event_id) + state_event_ids_at_start.append(event_id) auth_event_ids.append(event_id) events_to_create = body["events"] @@ -424,20 +425,26 @@ async def on_POST( 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) + insertion_event_id = event_ids.pop(0) + chunk_event_id = event_ids.pop(len(event_ids) - 1) + historical_event_ids = event_ids - return 200, { - "state_events": state_events_at_start, - "events": event_ids, + response_dict = { + "state_event_ids": state_event_ids_at_start, + "event_ids": historical_event_ids, "next_chunk_id": insertion_event["content"][ EventContentFields.MSC2716_NEXT_CHUNK_ID ], + "insertion_event_id": insertion_event_id, + "chunk_event_id": chunk_event_id, } + if base_insertion_event is not None: + response_dict["base_insertion_event_id"] = base_insertion_event.event_id + + return HTTPStatus.OK, response_dict def on_GET(self, request: Request, room_id: str) -> Tuple[int, str]: - return 501, "Not implemented" + return HTTPStatus.NOT_IMPLEMENTED, "Not implemented" def on_PUT( self, request: SynapseRequest, room_id: str From 341b87308af3175c290f0661f17c1adda358fdf2 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Tue, 7 Sep 2021 18:54:40 -0500 Subject: [PATCH 2/4] Add changelog --- changelog.d/10777.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/10777.misc diff --git a/changelog.d/10777.misc b/changelog.d/10777.misc new file mode 100644 index 000000000000..aed78a16f550 --- /dev/null +++ b/changelog.d/10777.misc @@ -0,0 +1 @@ +Split out [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716) meta events to their own fields in the `/batch_send` response. From e8a90e961fb1e5c3e2e96fb66a2cf6c83e24029f Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Tue, 7 Sep 2021 18:55:59 -0500 Subject: [PATCH 3/4] Fix linting --- synapse/rest/client/room_batch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/rest/client/room_batch.py b/synapse/rest/client/room_batch.py index c032817d0b81..dd22435b1d48 100644 --- a/synapse/rest/client/room_batch.py +++ b/synapse/rest/client/room_batch.py @@ -14,8 +14,8 @@ import logging import re -from typing import TYPE_CHECKING, Awaitable, List, Tuple from http import HTTPStatus +from typing import TYPE_CHECKING, Awaitable, List, Tuple from twisted.web.server import Request From b411a7769bde5106d43014d11c07ff32ecab51ed Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Fri, 10 Sep 2021 12:16:49 -0500 Subject: [PATCH 4/4] More understandable array slicing and dicing Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- synapse/rest/client/room_batch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/synapse/rest/client/room_batch.py b/synapse/rest/client/room_batch.py index dd22435b1d48..783fecf194cf 100644 --- a/synapse/rest/client/room_batch.py +++ b/synapse/rest/client/room_batch.py @@ -425,9 +425,9 @@ async def on_POST( context=context, ) - insertion_event_id = event_ids.pop(0) - chunk_event_id = event_ids.pop(len(event_ids) - 1) - historical_event_ids = event_ids + insertion_event_id = event_ids[0] + chunk_event_id = event_ids[-1] + historical_event_ids = event_ids[1:-1] response_dict = { "state_event_ids": state_event_ids_at_start,