Skip to main content

tuwunel_api/server/
timestamp.rs

1use axum::extract::State;
2use ruma::api::federation::event::get_event_by_timestamp::v1;
3use tuwunel_core::{Err, Result};
4
5use crate::router::Ruma;
6
7/// # `GET /_matrix/federation/v1/timestamp_to_event/{roomId}`
8///
9/// Get the ID of the event closest to the given timestamp.
10pub(crate) async fn get_event_by_timestamp_route(
11	State(services): State<crate::State>,
12	body: Ruma<v1::Request>,
13) -> Result<v1::Response> {
14	let origin = body.origin();
15	let room_id = &body.room_id;
16
17	// check if the server is allowed to see the room
18	services
19		.event_handler
20		.acl_check(origin, room_id)
21		.await?;
22
23	// get the closest event to the timestamp
24	let (origin_server_ts, event_id) = services
25		.timeline
26		.get_event_id_near_ts(room_id, body.ts, body.dir)
27		.await?;
28
29	// check if the server is allowed to see the event
30	if !services
31		.state_accessor
32		.server_can_see_event(origin, room_id, &event_id)
33		.await
34	{
35		return Err!(Request(Forbidden("Server is not allowed to see this event")));
36	}
37
38	Ok(v1::Response::new(event_id, origin_server_ts))
39}