Skip to main content

tuwunel_api_client/delayed_events/
update_delayed_event.rs

1use axum::extract::{MatchedPath, State};
2use ruma::{
3	UserId,
4	api::client::delayed_events::update_delayed_event::{
5		UpdateAction,
6		unstable_v1::{Request as BodyRequest, Response as BodyResponse},
7		v1::{Request, Response},
8	},
9};
10use tuwunel_core::{Err, Result};
11use tuwunel_service::Services;
12
13use super::is_stable;
14use crate::Ruma;
15
16/// # `POST /_matrix/client/unstable/org.matrix.msc4140/delayed_events/{delayId}/{action}`
17///
18/// Restarts, cancels or sends a scheduled event (MSC4140).
19///
20/// On the unstable path the access token may be omitted, since the `delay_id`
21/// alone is hard to guess and delegated callers such as lk-jwt-service hold no
22/// token. The stable path requires one.
23#[tracing::instrument(level = "debug", skip_all)]
24pub(crate) async fn update_delayed_event_route(
25	State(services): State<crate::State>,
26	path: MatchedPath,
27	body: Ruma<Request>,
28) -> Result<Response> {
29	let owner = body.sender_user.as_deref();
30
31	if owner.is_none() && is_stable(&path) {
32		return Err!(Request(MissingToken("Missing access token.")));
33	}
34
35	update(&services, &body.delay_id, &body.action, owner)
36		.await
37		.map(|()| Response::default())
38}
39
40/// # `POST /_matrix/client/unstable/org.matrix.msc4140/delayed_events/{delayId}`
41///
42/// Restarts, cancels or sends a scheduled event named in the body (MSC4140).
43///
44/// This is an earlier unstable form of the request, with the `action` in the
45/// body rather than the path.
46#[tracing::instrument(level = "debug", skip_all)]
47pub(crate) async fn update_delayed_event_by_body_route(
48	State(services): State<crate::State>,
49	body: Ruma<BodyRequest>,
50) -> Result<BodyResponse> {
51	update(&services, &body.delay_id, &body.action, Some(body.sender_user()))
52		.await
53		.map(|()| BodyResponse::new())
54}
55
56#[tracing::instrument(level = "debug", skip(services))]
57async fn update(
58	services: &Services,
59	delay_id: &str,
60	action: &UpdateAction,
61	owner: Option<&UserId>,
62) -> Result {
63	let delayed_events = &services.delayed_events;
64
65	match action {
66		| UpdateAction::Restart => delayed_events.restart(delay_id, owner).await,
67		| UpdateAction::Cancel => delayed_events.cancel(delay_id, owner).await,
68		| UpdateAction::Send => delayed_events
69			.send(delay_id, owner)
70			.await
71			.map(|_| ()),
72		| _ => Err!(Request(NotFound("Unknown delayed event action."))),
73	}
74}