ruma_client_api/delayed_events/
update_delayed_event.rs1use ruma_common::serde::StringEnum;
7
8use crate::PrivOwnedStr;
9
10#[derive(Clone, StringEnum)]
12#[ruma_enum(rename_all = "lowercase")]
13#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
14pub enum UpdateAction {
15 Restart,
17 Send,
20 Cancel,
22
23 #[doc(hidden)]
24 _Custom(PrivOwnedStr),
25}
26
27pub mod unstable_v1;
28
29pub mod unstable_v2 {
30 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]
53 pub struct Request {
54 #[ruma_api(path)]
56 pub delay_id: String,
57 #[ruma_api(path)]
59 pub action: UpdateAction,
60 }
61
62 impl Request {
63 pub fn new(delay_id: String, action: UpdateAction) -> Self {
65 Self { delay_id, action }
66 }
67 }
68
69 #[response]
72 pub struct Response {}
73 impl Response {
74 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::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
112 );
113 assert_eq!("POST", parts.method);
114 assert_eq!(json!({}), serde_json::from_slice::<JsonValue>(&body).unwrap());
115 }
116 }
117
118 #[cfg(all(test, feature = "server"))]
119 mod server_tests {
120
121 use ruma_common::api::IncomingRequestExt as _;
122
123 use super::{Request, UpdateAction};
124
125 #[test]
126 fn deserialize_update_delayed_events_request() {
127 let uri = http::Uri::builder()
128 .scheme("https")
129 .authority("matrix.org")
130 .path_and_query(
131 "/_matrix/client/unstable/org.matrix.msc4140/delayed_events/a_delay_id/send",
132 )
133 .build()
134 .unwrap();
135
136 let req = Request::try_from_http_request(
137 http::Request::builder().method("POST").uri(uri).body(&[] as &[u8]).unwrap(),
138 &["a_delay_id", "send"],
139 )
140 .unwrap();
141
142 assert_eq!(req.delay_id, "a_delay_id");
143 assert_eq!(req.action, UpdateAction::Send);
144 }
145 }
146}