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