Skip to main content

tuwunel_api_client/delayed_events/
send_delayed_event.rs

1use axum::extract::{MatchedPath, State};
2use futures::FutureExt;
3use ruma::{
4	api::client::delayed_events::send_delayed_event::v3::{Request, Response},
5	events::{MessageLikeEventType, StateEventType},
6};
7use tuwunel_core::Result;
8use tuwunel_service::delayed_events::DelayId;
9
10use super::{Kind, Schedule, is_stable, schedule};
11use crate::Ruma;
12
13/// # `PUT /_matrix/client/unstable/org.matrix.msc4140/rooms/{roomId}/delayed_event/{eventType}/{txnId}`
14///
15/// Schedules an event to be sent after `delay_ms` (MSC4140).
16///
17/// A `state_key` in the body makes it a state event. The stable path answers
18/// with the stable error codes.
19#[tracing::instrument(level = "debug", skip_all)]
20pub(crate) async fn send_delayed_event_route(
21	State(services): State<crate::State>,
22	path: MatchedPath,
23	body: Ruma<Request>,
24) -> Result<Response> {
25	let event_type = body.event_type.to_cow_str();
26	let message_type = MessageLikeEventType::from(&*event_type);
27	let state_type = StateEventType::from(&*event_type);
28	let kind = match body.state_key.as_deref() {
29		| None => Kind::Message {
30			event_type: &message_type,
31			content: body.content.cast_ref_unchecked(),
32		},
33		| Some(state_key) => Kind::State {
34			event_type: &state_type,
35			state_key,
36			content: body.content.cast_ref_unchecked(),
37		},
38	};
39
40	let request = Schedule {
41		sender: body.sender_user(),
42		device: body.sender_device.as_deref(),
43		room_id: &body.room_id,
44		kind,
45		delay: body.delay_ms,
46		txn_id: Some(&body.txn_id),
47		timestamp: body.appservice_info.as_ref().and(body.ts),
48		stable: is_stable(&path),
49	};
50
51	schedule(&services, request)
52		.boxed() // size firewall
53		.await
54		.map(DelayId::into_string)
55		.map(Response::new)
56}