Skip to main content

tuwunel_api/client/room/
timestamp.rs

1use axum::extract::State;
2use ruma::api::client::room::get_event_by_timestamp::v1;
3use tuwunel_core::{Err, Result};
4
5use crate::router::Ruma;
6
7/// # `GET /_matrix/client/v1/rooms/{roomId}/timestamp_to_event`
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 sender_user = body.sender_user();
15	let room_id = &body.room_id;
16
17	// check if user can see the room
18	if !services
19		.state_accessor
20		.user_can_see_state_events(sender_user, room_id)
21		.await
22	{
23		return Err!(Request(Forbidden("You don't have permission to view this room.")));
24	}
25
26	// get the closest event to the given timestamp
27	let (origin_server_ts, event_id) = services
28		.timeline
29		.get_event_id_near_ts(room_id, body.ts, body.dir)
30		.await?;
31
32	if !services
33		.state_accessor
34		.user_can_see_event(sender_user, room_id, &event_id)
35		.await
36	{
37		return Err!(Request(Forbidden("You don't have permission to view this event.")));
38	}
39
40	// return the closest event found locally or from federation
41	Ok(v1::Response::new(event_id, origin_server_ts))
42}