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