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
6pub mod unstable {
7    //! `msc3814` ([MSC])
8    //!
9    //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4140
10
11    use ruma_common::{
12        api::{auth_scheme::AccessToken, request, response},
13        metadata,
14        serde::StringEnum,
15    };
16
17    use crate::PrivOwnedStr;
18
19    metadata! {
20        method: POST,
21        rate_limited: true,
22        authentication: AccessToken,
23        history: {
24            unstable("org.matrix.msc4140") => "/_matrix/client/unstable/org.matrix.msc4140/delayed_events/{delay_id}",
25        }
26    }
27
28    /// The possible update actions we can do for updating a delayed event.
29    #[derive(Clone, StringEnum)]
30    #[ruma_enum(rename_all = "lowercase")]
31    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
32    pub enum UpdateAction {
33        /// Restart the delayed event timeout. (heartbeat ping)
34        Restart,
35        /// Send the delayed event immediately independent of the timeout state. (deletes all
36        /// timers)
37        Send,
38        /// Delete the delayed event and never send it. (deletes all timers)
39        Cancel,
40
41        #[doc(hidden)]
42        _Custom(PrivOwnedStr),
43    }
44    /// Request type for the [`update_delayed_event`](crate::delayed_events::update_delayed_event)
45    /// endpoint.
46    #[request(error = crate::Error)]
47    pub struct Request {
48        /// The delay id that we want to update.
49        #[ruma_api(path)]
50        pub delay_id: String,
51        /// Which kind of update we want to request for the delayed event.
52        pub action: UpdateAction,
53    }
54
55    impl Request {
56        /// Creates a new `Request` to update a delayed event.
57        pub fn new(delay_id: String, action: UpdateAction) -> Self {
58            Self { delay_id, action }
59        }
60    }
61
62    /// Response type for the [`update_delayed_event`](crate::delayed_events::update_delayed_event)
63    /// endpoint.
64    #[response(error = crate::Error)]
65    pub struct Response {}
66    impl Response {
67        /// Creates a new empty response for the
68        /// [`update_delayed_event`](crate::delayed_events::update_delayed_event) endpoint.
69        pub fn new() -> Self {
70            Self {}
71        }
72    }
73
74    #[cfg(all(test, feature = "client"))]
75    mod tests {
76        use std::borrow::Cow;
77
78        use ruma_common::api::{
79            auth_scheme::SendAccessToken, MatrixVersion, OutgoingRequest, SupportedVersions,
80        };
81        use serde_json::{json, Value as JsonValue};
82
83        use super::{Request, UpdateAction};
84        #[test]
85        fn serialize_update_delayed_event_request() {
86            let supported = SupportedVersions {
87                versions: [MatrixVersion::V1_1].into(),
88                features: Default::default(),
89            };
90            let request: http::Request<Vec<u8>> =
91                Request::new("1234".to_owned(), UpdateAction::Cancel)
92                    .try_into_http_request(
93                        "https://homeserver.tld",
94                        SendAccessToken::IfRequired("auth_tok"),
95                        Cow::Owned(supported),
96                    )
97                    .unwrap();
98
99            let (parts, body) = request.into_parts();
100
101            assert_eq!(
102                "https://homeserver.tld/_matrix/client/unstable/org.matrix.msc4140/delayed_events/1234",
103                parts.uri.to_string()
104            );
105            assert_eq!("POST", parts.method.to_string());
106            assert_eq!(
107                json!({"action": "cancel"}),
108                serde_json::from_str::<JsonValue>(std::str::from_utf8(&body).unwrap()).unwrap()
109            );
110        }
111    }
112}