Skip to main content

ruma_client_api/delayed_events/
update_delayed_event.rs

1//! `POST /_matrix/client/*/delayed_events/{delayed_id}`
2//!
3//! Send a delayed event update. This can be a updateing/canceling/sending the associated delayed
4//! event.
5
6use ruma_common::serde::StringEnum;
7
8use crate::PrivOwnedStr;
9
10/// The possible update actions we can do for updating a delayed event.
11#[derive(Clone, StringEnum)]
12#[ruma_enum(rename_all = "lowercase")]
13#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
14pub enum UpdateAction {
15    /// Restart the delayed event timeout. (heartbeat ping)
16    Restart,
17    /// Send the delayed event immediately independent of the timeout state. (deletes all
18    /// timers)
19    Send,
20    /// Delete the delayed event and never send it. (deletes all timers)
21    Cancel,
22
23    #[doc(hidden)]
24    _Custom(PrivOwnedStr),
25}
26
27pub mod unstable_v1;
28
29pub mod unstable_v2 {
30    //! `msc3814` ([MSC])
31    //!
32    //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4140
33
34    use ruma_common::{
35        api::{auth_scheme::NoAccessToken, request, response},
36        metadata,
37    };
38
39    use super::UpdateAction;
40
41    metadata! {
42        method: POST,
43        rate_limited: true,
44        authentication: NoAccessToken,
45        history: {
46            unstable("org.matrix.msc4140") => "/_matrix/client/unstable/org.matrix.msc4140/delayed_events/{delay_id}/{action}",
47        }
48    }
49
50    /// Request type for the [`update_delayed_event`](crate::delayed_events::update_delayed_event)
51    /// endpoint.
52    #[request]
53    pub struct Request {
54        /// The delay id that we want to update.
55        #[ruma_api(path)]
56        pub delay_id: String,
57        /// Which kind of update we want to request for the delayed event.
58        #[ruma_api(path)]
59        pub action: UpdateAction,
60    }
61
62    impl Request {
63        /// Creates a new `Request` to update a delayed event.
64        pub fn new(delay_id: String, action: UpdateAction) -> Self {
65            Self { delay_id, action }
66        }
67    }
68
69    /// Response type for the [`update_delayed_event`](crate::delayed_events::update_delayed_event)
70    /// endpoint.
71    #[response]
72    pub struct Response {}
73    impl Response {
74        /// Creates a new empty response for the
75        /// [`update_delayed_event`](crate::delayed_events::update_delayed_event) endpoint.
76        pub fn new() -> Self {
77            Self {}
78        }
79    }
80
81    #[cfg(all(test, feature = "client"))]
82    mod client_tests {
83        use std::borrow::Cow;
84
85        use ruma_common::api::{
86            MatrixVersion, OutgoingRequest, SupportedVersions, auth_scheme::SendAccessToken,
87        };
88        use serde_json::{Value as JsonValue, json};
89
90        use super::{Request, UpdateAction};
91
92        #[test]
93        fn serialize_update_delayed_event_request() {
94            let supported = SupportedVersions {
95                versions: [MatrixVersion::V1_1].into(),
96                features: Default::default(),
97            };
98            let request: http::Request<Vec<u8>> =
99                Request::new("1234".to_owned(), UpdateAction::Cancel)
100                    .try_into_http_request(
101                        "https://homeserver.tld",
102                        SendAccessToken::None,
103                        Cow::Owned(supported),
104                    )
105                    .unwrap();
106
107            let (parts, body) = request.into_parts();
108
109            assert_eq!(
110                "https://homeserver.tld/_matrix/client/unstable/org.matrix.msc4140/delayed_events/1234/cancel",
111                parts.uri.to_string()
112            );
113            assert_eq!("POST", parts.method.to_string());
114            assert_eq!(
115                json!({}),
116                serde_json::from_str::<JsonValue>(std::str::from_utf8(&body).unwrap()).unwrap()
117            );
118        }
119    }
120
121    #[cfg(all(test, feature = "server"))]
122    mod server_tests {
123
124        use ruma_common::api::IncomingRequest;
125
126        use super::{Request, UpdateAction};
127
128        #[test]
129        fn deserialize_update_delayed_events_request() {
130            let uri = http::Uri::builder()
131                .scheme("https")
132                .authority("matrix.org")
133                .path_and_query(
134                    "/_matrix/client/unstable/org.matrix.msc4140/delayed_events/a_delay_id/send",
135                )
136                .build()
137                .unwrap();
138
139            let req = Request::try_from_http_request(
140                http::Request::builder().method("POST").uri(uri).body("").unwrap(),
141                &["a_delay_id", "send"],
142            )
143            .unwrap();
144
145            assert_eq!(req.delay_id, "a_delay_id".to_owned());
146            assert_eq!(req.action, UpdateAction::Send);
147        }
148    }
149}