ruma_client_api/room/
get_event_by_timestamp.rs

1//! `GET /_matrix/client/*/rooms/{roomId}/timestamp_to_event`
2//!
3//! Get the ID of the event closest to the given timestamp.
4
5pub mod v1 {
6    //! `/v1/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1roomsroomidtimestamp_to_event
9
10    use ruma_common::{
11        MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId,
12        api::{Direction, auth_scheme::AccessToken, request, response},
13        metadata,
14    };
15
16    metadata! {
17        method: GET,
18        rate_limited: true,
19        authentication: AccessToken,
20        history: {
21            unstable("org.matrix.msc3030") => "/_matrix/client/unstable/org.matrix.msc3030/rooms/{room_id}/timestamp_to_event",
22            1.6 => "/_matrix/client/v1/rooms/{room_id}/timestamp_to_event",
23        }
24    }
25
26    /// Request type for the `get_event_by_timestamp` endpoint.
27    #[request(error = crate::Error)]
28    pub struct Request {
29        /// The ID of the room the event is in.
30        #[ruma_api(path)]
31        pub room_id: OwnedRoomId,
32
33        /// The timestamp to search from, inclusively.
34        #[ruma_api(query)]
35        pub ts: MilliSecondsSinceUnixEpoch,
36
37        /// The direction in which to search.
38        #[ruma_api(query)]
39        pub dir: Direction,
40    }
41
42    /// Response type for the `get_event_by_timestamp` endpoint.
43    #[response(error = crate::Error)]
44    pub struct Response {
45        /// The ID of the event found.
46        pub event_id: OwnedEventId,
47
48        /// The event's timestamp.
49        pub origin_server_ts: MilliSecondsSinceUnixEpoch,
50    }
51
52    impl Request {
53        /// Creates a new `Request` with the given room ID, timestamp and direction.
54        pub fn new(room_id: OwnedRoomId, ts: MilliSecondsSinceUnixEpoch, dir: Direction) -> Self {
55            Self { room_id, ts, dir }
56        }
57
58        /// Creates a new `Request` with the given room ID and timestamp, and the direction set to
59        /// `Backward`.
60        ///
61        /// Allows to have the latest event before or including the given timestamp.
62        pub fn until(room_id: OwnedRoomId, ts: MilliSecondsSinceUnixEpoch) -> Self {
63            Self::new(room_id, ts, Direction::Backward)
64        }
65
66        /// Creates a new `Request` with the given room ID and timestamp, and the direction set to
67        /// `Forward`.
68        ///
69        /// Allows to have the earliest event including or after the given timestamp.
70        pub fn since(room_id: OwnedRoomId, ts: MilliSecondsSinceUnixEpoch) -> Self {
71            Self::new(room_id, ts, Direction::Forward)
72        }
73    }
74
75    impl Response {
76        /// Creates a new `Response` with the given event ID and timestamp.
77        pub fn new(event_id: OwnedEventId, origin_server_ts: MilliSecondsSinceUnixEpoch) -> Self {
78            Self { event_id, origin_server_ts }
79        }
80    }
81}