ruma_federation_api/event/
get_missing_events.rs

1//! `POST /_matrix/federation/*/get_missing_events/{roomId}`
2//!
3//! Retrieves previous events that the sender is missing.
4
5pub mod v1 {
6    //! `/v1/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/server-server-api/#post_matrixfederationv1get_missing_eventsroomid
9
10    use js_int::{UInt, uint};
11    use ruma_common::{
12        OwnedEventId, OwnedRoomId,
13        api::{request, response},
14        metadata,
15    };
16    use serde_json::value::RawValue as RawJsonValue;
17
18    use crate::authentication::ServerSignatures;
19
20    metadata! {
21        method: POST,
22        rate_limited: false,
23        authentication: ServerSignatures,
24        path: "/_matrix/federation/v1/get_missing_events/{room_id}",
25    }
26
27    /// Request type for the `get_missing_events` endpoint.
28    #[request]
29    pub struct Request {
30        /// The room ID to search in.
31        #[ruma_api(path)]
32        pub room_id: OwnedRoomId,
33
34        /// The maximum number of events to retrieve.
35        ///
36        /// Defaults to 10.
37        #[serde(default = "default_limit", skip_serializing_if = "is_default_limit")]
38        pub limit: UInt,
39
40        /// The minimum depth of events to retrieve.
41        ///
42        /// Defaults to 0.
43        #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
44        pub min_depth: UInt,
45
46        /// The latest event IDs that the sender already has.
47        ///
48        /// These are skipped when retrieving the previous events of `latest_events`.
49        pub earliest_events: Vec<OwnedEventId>,
50
51        /// The event IDs to retrieve the previous events for.
52        pub latest_events: Vec<OwnedEventId>,
53    }
54
55    /// Response type for the `get_missing_events` endpoint.
56    #[response]
57    #[derive(Default)]
58    pub struct Response {
59        /// The missing PDUs.
60        pub events: Vec<Box<RawJsonValue>>,
61    }
62
63    impl Request {
64        /// Creates a new `Request` for events in the given room with the given constraints.
65        pub fn new(
66            room_id: OwnedRoomId,
67            earliest_events: Vec<OwnedEventId>,
68            latest_events: Vec<OwnedEventId>,
69        ) -> Self {
70            Self {
71                room_id,
72                limit: default_limit(),
73                min_depth: UInt::default(),
74                earliest_events,
75                latest_events,
76            }
77        }
78    }
79
80    impl Response {
81        /// Creates a new `Response` with the given events.
82        pub fn new(events: Vec<Box<RawJsonValue>>) -> Self {
83            Self { events }
84        }
85    }
86
87    fn default_limit() -> UInt {
88        uint!(10)
89    }
90
91    fn is_default_limit(val: &UInt) -> bool {
92        *val == default_limit()
93    }
94}