Skip to content

Commit 274d1cf

Browse files
committed
Merge with element-hq#19152
2 parents f1383cc + 9e23cde commit 274d1cf

File tree

7 files changed

+185
-220
lines changed

7 files changed

+185
-220
lines changed

changelog.d/19152.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove authentication from `POST /_matrix/client/v1/delayed_events`, and allow calling this endpoint with the update action to take (`send`/`cancel`/`restart`) in the request path instead of the body.

synapse/handlers/delayed_events.py

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from synapse.api.errors import ShadowBanError, SynapseError, cs_error
2222
from synapse.api.ratelimiting import Ratelimiter
2323
from synapse.config.workers import MAIN_PROCESS_INSTANCE_NAME
24+
from synapse.http.site import SynapseRequest
2425
from synapse.logging.context import make_deferred_yieldable
2526
from synapse.logging.opentracing import set_tag
2627
from synapse.metrics import SERVER_NAME_LABEL, event_processing_positions
@@ -30,11 +31,9 @@
3031
)
3132
from synapse.storage.databases.main.delayed_events import (
3233
DelayedEventDetails,
33-
DelayID,
3434
EventType,
3535
StateKey,
3636
Timestamp,
37-
UserLocalpart,
3837
)
3938
from synapse.storage.databases.main.state_deltas import StateDelta
4039
from synapse.types import (
@@ -416,98 +415,66 @@ def on_added(self, next_send_ts: int) -> None:
416415
if self._next_send_ts_changed(next_send_ts):
417416
self._schedule_next_at(next_send_ts)
418417

419-
async def cancel(self, requester: Requester, delay_id: str) -> None:
418+
async def cancel(self, request: SynapseRequest, delay_id: str) -> None:
420419
"""
421420
Cancels the scheduled delivery of the matching delayed event.
422421
423-
Args:
424-
requester: The owner of the delayed event to act on.
425-
delay_id: The ID of the delayed event to act on.
426-
427422
Raises:
428423
NotFoundError: if no matching delayed event could be found.
429424
"""
430425
assert self._is_master
431426
await self._delayed_event_mgmt_ratelimiter.ratelimit(
432-
requester,
433-
(requester.user.to_string(), requester.device_id),
427+
None, request.getClientAddress().host
434428
)
435429
await make_deferred_yieldable(self._initialized_from_db)
436430

437431
next_send_ts = await self._store.cancel_delayed_event(
438-
delay_id=delay_id,
439-
user_localpart=requester.user.localpart,
440-
finalised_ts=self._get_current_ts(),
432+
delay_id, self._get_current_ts()
441433
)
442434

443435
if self._next_send_ts_changed(next_send_ts):
444436
self._schedule_next_at_or_none(next_send_ts)
445437

446-
async def restart(self, requester: Requester, delay_id: str) -> None:
438+
async def restart(self, request: SynapseRequest, delay_id: str) -> None:
447439
"""
448440
Restarts the scheduled delivery of the matching delayed event.
449441
450-
Args:
451-
requester: The owner of the delayed event to act on.
452-
delay_id: The ID of the delayed event to act on.
453-
454442
Raises:
455443
NotFoundError: if no matching delayed event could be found.
456444
"""
457445
assert self._is_master
458446
await self._delayed_event_mgmt_ratelimiter.ratelimit(
459-
requester,
460-
(requester.user.to_string(), requester.device_id),
447+
None, request.getClientAddress().host
461448
)
462449
await make_deferred_yieldable(self._initialized_from_db)
463450

464451
next_send_ts = await self._store.restart_delayed_event(
465-
delay_id=delay_id,
466-
user_localpart=requester.user.localpart,
467-
current_ts=self._get_current_ts(),
452+
delay_id, self._get_current_ts()
468453
)
469454

470455
if self._next_send_ts_changed(next_send_ts):
471456
self._schedule_next_at(next_send_ts)
472457

473-
async def send(self, requester: Requester, delay_id: str) -> None:
458+
async def send(self, request: SynapseRequest, delay_id: str) -> None:
474459
"""
475460
Immediately sends the matching delayed event, instead of waiting for its scheduled delivery.
476461
477-
Args:
478-
requester: The owner of the delayed event to act on.
479-
delay_id: The ID of the delayed event to act on.
480-
481462
Raises:
482463
NotFoundError: if no matching delayed event could be found.
483464
"""
484465
assert self._is_master
485-
# Use standard request limiter for sending delayed events on-demand,
486-
# as an on-demand send is similar to sending a regular event.
487-
await self._request_ratelimiter.ratelimit(requester)
466+
await self._delayed_event_mgmt_ratelimiter.ratelimit(
467+
None, request.getClientAddress().host
468+
)
488469
await make_deferred_yieldable(self._initialized_from_db)
489470

490-
event, next_send_ts = await self._store.process_target_delayed_event(
491-
delay_id=delay_id,
492-
user_localpart=requester.user.localpart,
493-
)
471+
event, next_send_ts = await self._store.process_target_delayed_event(delay_id)
494472

495473
if self._next_send_ts_changed(next_send_ts):
496474
self._schedule_next_at_or_none(next_send_ts)
497475

498476
if event:
499-
await self._send_event(
500-
DelayedEventDetails(
501-
delay_id=DelayID(delay_id),
502-
user_localpart=UserLocalpart(requester.user.localpart),
503-
room_id=event.room_id,
504-
type=event.type,
505-
state_key=event.state_key,
506-
origin_server_ts=event.origin_server_ts,
507-
content=event.content,
508-
device_id=event.device_id,
509-
)
510-
)
477+
await self._send_event(event)
511478

512479
async def _send_on_timeout(self) -> None:
513480
self._next_delayed_event_call = None
@@ -674,7 +641,6 @@ async def _send_event(
674641
try:
675642
await self._store.finalise_processed_delayed_event(
676643
event.delay_id,
677-
event.user_localpart,
678644
send_error or event_id,
679645
finalised_ts,
680646
)

synapse/rest/client/delayed_events.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,11 @@ class UpdateDelayedEventServlet(RestServlet):
5757

5858
def __init__(self, hs: "HomeServer"):
5959
super().__init__()
60-
self.auth = hs.get_auth()
6160
self.delayed_events_handler = hs.get_delayed_events_handler()
6261

6362
async def on_POST(
6463
self, request: SynapseRequest, delay_id: str
6564
) -> tuple[int, JsonDict]:
66-
requester = await self.auth.get_user_by_req(request)
67-
6865
body = parse_json_object_from_request(request)
6966
try:
7067
action = str(body["action"])
@@ -85,11 +82,65 @@ async def on_POST(
8582
)
8683

8784
if enum_action == _UpdateDelayedEventAction.CANCEL:
88-
await self.delayed_events_handler.cancel(requester, delay_id)
85+
await self.delayed_events_handler.cancel(request, delay_id)
8986
elif enum_action == _UpdateDelayedEventAction.RESTART:
90-
await self.delayed_events_handler.restart(requester, delay_id)
87+
await self.delayed_events_handler.restart(request, delay_id)
9188
elif enum_action == _UpdateDelayedEventAction.SEND:
92-
await self.delayed_events_handler.send(requester, delay_id)
89+
await self.delayed_events_handler.send(request, delay_id)
90+
return 200, {}
91+
92+
93+
class CancelDelayedEventServlet(RestServlet):
94+
PATTERNS = client_patterns(
95+
r"/org\.matrix\.msc4140/delayed_events/(?P<delay_id>[^/]+)/cancel$",
96+
releases=(),
97+
)
98+
CATEGORY = "Delayed event management requests"
99+
100+
def __init__(self, hs: "HomeServer"):
101+
super().__init__()
102+
self.delayed_events_handler = hs.get_delayed_events_handler()
103+
104+
async def on_POST(
105+
self, request: SynapseRequest, delay_id: str
106+
) -> tuple[int, JsonDict]:
107+
await self.delayed_events_handler.cancel(request, delay_id)
108+
return 200, {}
109+
110+
111+
class RestartDelayedEventServlet(RestServlet):
112+
PATTERNS = client_patterns(
113+
r"/org\.matrix\.msc4140/delayed_events/(?P<delay_id>[^/]+)/restart$",
114+
releases=(),
115+
)
116+
CATEGORY = "Delayed event management requests"
117+
118+
def __init__(self, hs: "HomeServer"):
119+
super().__init__()
120+
self.delayed_events_handler = hs.get_delayed_events_handler()
121+
122+
async def on_POST(
123+
self, request: SynapseRequest, delay_id: str
124+
) -> tuple[int, JsonDict]:
125+
await self.delayed_events_handler.restart(request, delay_id)
126+
return 200, {}
127+
128+
129+
class SendDelayedEventServlet(RestServlet):
130+
PATTERNS = client_patterns(
131+
r"/org\.matrix\.msc4140/delayed_events/(?P<delay_id>[^/]+)/send$",
132+
releases=(),
133+
)
134+
CATEGORY = "Delayed event management requests"
135+
136+
def __init__(self, hs: "HomeServer"):
137+
super().__init__()
138+
self.delayed_events_handler = hs.get_delayed_events_handler()
139+
140+
async def on_POST(
141+
self, request: SynapseRequest, delay_id: str
142+
) -> tuple[int, JsonDict]:
143+
await self.delayed_events_handler.send(request, delay_id)
93144
return 200, {}
94145

95146

@@ -135,4 +186,7 @@ def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
135186
# The following can't currently be instantiated on workers.
136187
if hs.config.worker.worker_app is None:
137188
UpdateDelayedEventServlet(hs).register(http_server)
189+
CancelDelayedEventServlet(hs).register(http_server)
190+
RestartDelayedEventServlet(hs).register(http_server)
191+
SendDelayedEventServlet(hs).register(http_server)
138192
DelayedEventsServlet(hs).register(http_server)

0 commit comments

Comments
 (0)