ruma_client_api/delayed_events/
delayed_state_event.rs
1pub mod unstable {
6 use ruma_common::{
11 api::{request, response, Metadata},
12 metadata,
13 serde::Raw,
14 OwnedRoomId,
15 };
16 use ruma_events::{AnyStateEventContent, StateEventContent, StateEventType};
17 use serde_json::value::to_raw_value as to_raw_json_value;
18
19 use crate::delayed_events::DelayParameters;
20
21 const METADATA: Metadata = metadata! {
22 method: PUT,
23 rate_limited: false,
24 authentication: AccessToken,
25 history: {
26 unstable => "/_matrix/client/v3/rooms/:room_id/state/:event_type/:state_key",
28 }
29 };
30
31 #[request(error = crate::Error)]
34 pub struct Request {
35 #[ruma_api(path)]
37 pub room_id: OwnedRoomId,
38
39 #[ruma_api(path)]
41 pub event_type: StateEventType,
42
43 #[ruma_api(path)]
45 pub state_key: String,
46
47 #[ruma_api(query_all)]
52 pub delay_parameters: DelayParameters,
53
54 #[ruma_api(body)]
56 pub body: Raw<AnyStateEventContent>,
57 }
58
59 #[response(error = crate::Error)]
62 pub struct Response {
63 pub delay_id: String,
65 }
66
67 impl Request {
68 pub fn new<T>(
76 room_id: OwnedRoomId,
77 state_key: String,
78 delay_parameters: DelayParameters,
79 content: &T,
80 ) -> serde_json::Result<Self>
81 where
82 T: StateEventContent,
83 {
84 Ok(Self {
85 room_id,
86 state_key,
87 event_type: content.event_type(),
88 delay_parameters,
89 body: Raw::from_json(to_raw_json_value(content)?),
90 })
91 }
92
93 pub fn new_raw(
96 room_id: OwnedRoomId,
97 state_key: String,
98 event_type: StateEventType,
99 delay_parameters: DelayParameters,
100 body: Raw<AnyStateEventContent>,
101 ) -> Self {
102 Self { room_id, event_type, state_key, body, delay_parameters }
103 }
104 }
105
106 impl Response {
107 pub fn new(delay_id: String) -> Self {
110 Self { delay_id }
111 }
112 }
113
114 #[cfg(all(test, feature = "client"))]
115 mod tests {
116 use ruma_common::{
117 api::{MatrixVersion, OutgoingRequest, SendAccessToken},
118 owned_room_id,
119 };
120 use ruma_events::room::topic::RoomTopicEventContent;
121 use serde_json::{json, Value as JsonValue};
122 use web_time::Duration;
123
124 use super::Request;
125 use crate::delayed_events::DelayParameters;
126
127 fn create_delayed_event_request(
128 delay_parameters: DelayParameters,
129 ) -> (http::request::Parts, Vec<u8>) {
130 Request::new(
131 owned_room_id!("!roomid:example.org"),
132 "@userAsStateKey:example.org".to_owned(),
133 delay_parameters,
134 &RoomTopicEventContent::new("my_topic".to_owned()),
135 )
136 .unwrap()
137 .try_into_http_request(
138 "https://homeserver.tld",
139 SendAccessToken::IfRequired("auth_tok"),
140 &[MatrixVersion::V1_1],
141 )
142 .unwrap()
143 .into_parts()
144 }
145
146 #[test]
147 fn serialize_delayed_state_request() {
148 let (parts, body) = create_delayed_event_request(DelayParameters::Timeout {
149 timeout: Duration::from_millis(1_234_321),
150 });
151 assert_eq!(
152 "https://homeserver.tld/_matrix/client/v3/rooms/!roomid:example.org/state/m.room.topic/@userAsStateKey:example.org?org.matrix.msc4140.delay=1234321",
153 parts.uri.to_string()
154 );
155 assert_eq!("PUT", parts.method.to_string());
156 assert_eq!(
157 json!({"topic": "my_topic"}),
158 serde_json::from_str::<JsonValue>(std::str::from_utf8(&body).unwrap()).unwrap()
159 );
160 }
161 }
162}