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 updating/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
18    /// Send the delayed event immediately independent of the timeout state. (deletes all
19    /// timers)
20    Send,
21
22    /// Delete the delayed event and never send it. (deletes all timers)
23    Cancel,
24
25    #[doc(hidden)]
26    _Custom(PrivOwnedStr),
27}
28
29pub mod msc4140_v1;
30
31pub mod msc4140_v2 {
32    //! `/unstable/org.matrix.msc4140/` ([MSC])
33    //!
34    //! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/4140
35
36    use ruma_common::{
37        api::{auth_scheme::AccessToken, request, response},
38        metadata,
39    };
40
41    use super::UpdateAction;
42
43    metadata! {
44        method: POST,
45        rate_limited: true,
46        authentication: AccessToken,
47        history: {
48            unstable("org.matrix.msc4140") => "/_matrix/client/unstable/org.matrix.msc4140/delayed_events/{delay_id}/{action}",
49        }
50    }
51
52    /// Request type for the `update_delayed_event` endpoint.
53    #[request]
54    pub struct Request {
55        /// The delay id that we want to update.
56        #[ruma_api(path)]
57        pub delay_id: String,
58
59        /// Which kind of update we want to request for the delayed event.
60        #[ruma_api(path)]
61        pub action: UpdateAction,
62    }
63
64    impl Request {
65        /// Creates a new `Request` to update a delayed event.
66        pub fn new(delay_id: String, action: UpdateAction) -> Self {
67            Self { delay_id, action }
68        }
69    }
70
71    /// Response type for the `update_delayed_event` endpoint.
72    #[response]
73    pub struct Response {}
74    impl Response {
75        /// Creates a new empty response.
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, OutgoingRequestExt as _, 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::IfRequired("auth_tok"),
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
112            );
113            assert_eq!("POST", parts.method);
114            assert_eq!(parts.headers.get(http::header::AUTHORIZATION).unwrap(), "Bearer auth_tok");
115            assert_eq!(json!({}), serde_json::from_slice::<JsonValue>(&body).unwrap());
116        }
117    }
118
119    #[cfg(all(test, feature = "server"))]
120    mod server_tests {
121
122        use ruma_common::api::IncomingRequestExt as _;
123
124        use super::{Request, UpdateAction};
125
126        #[test]
127        fn deserialize_update_delayed_events_request() {
128            let uri = http::Uri::builder()
129                .scheme("https")
130                .authority("matrix.org")
131                .path_and_query(
132                    "/_matrix/client/unstable/org.matrix.msc4140/delayed_events/a_delay_id/send",
133                )
134                .build()
135                .unwrap();
136
137            let req = Request::try_from_http_request(
138                http::Request::builder().method("POST").uri(uri).body(&[] as &[u8]).unwrap(),
139                &["a_delay_id", "send"],
140            )
141            .unwrap();
142
143            assert_eq!(req.delay_id, "a_delay_id");
144            assert_eq!(req.action, UpdateAction::Send);
145        }
146    }
147}