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

Split out /batch_send meta events to their own fields (MSC2716) #10777

Merged
29 changes: 18 additions & 11 deletions synapse/rest/client/room_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
)

Expand All @@ -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,
)
Expand All @@ -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 = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to this PR, but this method really needs splitting up. I shouldn't have to scroll through over 200 lines to find out where a variable is used!

Copy link
Contributor Author

@MadLittleMods MadLittleMods Sep 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Definitely. I've been holding off on refactors like this while so many PRs have been in flight at the same time 😬

for state_event in body["state_events_at_start"]:
assert_params_in_dict(
state_event, ["type", "origin_server_ts", "content", "sender"]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started using HTTPStatus because I originally noticed it on this line and wanted to make it consistent in this file


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
Expand Down