ruma_client_api/push/
set_pushrule.rs1pub mod v3 {
6 use ruma_common::{
11 api::{auth_scheme::AccessToken, error::Error, response},
12 metadata,
13 push::{Action, NewPushRule, PushCondition},
14 };
15
16 metadata! {
17 method: PUT,
18 rate_limited: true,
19 authentication: AccessToken,
20 history: {
21 1.0 => "/_matrix/client/r0/pushrules/global/{kind}/{rule_id}",
22 1.1 => "/_matrix/client/v3/pushrules/global/{kind}/{rule_id}",
23 }
24 }
25
26 #[derive(Clone, Debug)]
28 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
29 pub struct Request {
30 pub rule: NewPushRule,
32
33 pub before: Option<String>,
36
37 pub after: Option<String>,
40 }
41
42 #[response]
44 #[derive(Default)]
45 pub struct Response {}
46
47 impl Request {
48 pub fn new(rule: NewPushRule) -> Self {
50 Self { rule, before: None, after: None }
51 }
52 }
53
54 impl Response {
55 pub fn new() -> Self {
57 Self {}
58 }
59 }
60
61 #[doc(hidden)]
62 #[cfg(feature = "client")]
63 pub struct RequestBody(NewPushRule);
64
65 #[cfg(feature = "client")]
66 impl ruma_common::api::OutgoingBody for RequestBody {
67 type Error = serde_json::Error;
68
69 fn content_type(&self) -> Option<http::HeaderValue> {
70 Some(ruma_common::http_headers::APPLICATION_JSON)
71 }
72
73 fn try_into_buf<T: Default + bytes::BufMut + AsRef<[u8]>>(self) -> serde_json::Result<T> {
74 match self.0 {
75 NewPushRule::Override(r) | NewPushRule::Underride(r) => {
76 let body =
77 ConditionalRequestBody { actions: r.actions, conditions: r.conditions };
78 ruma_common::serde::json_to_buf(&body)
79 }
80 NewPushRule::Content(r) => {
81 let body = PatternedRequestBody { actions: r.actions, pattern: r.pattern };
82 ruma_common::serde::json_to_buf(&body)
83 }
84 NewPushRule::Room(r) => {
85 let body = SimpleRequestBody { actions: r.actions };
86 ruma_common::serde::json_to_buf(&body)
87 }
88 NewPushRule::Sender(r) => {
89 let body = SimpleRequestBody { actions: r.actions };
90 ruma_common::serde::json_to_buf(&body)
91 }
92 #[cfg(not(ruma_unstable_exhaustive_types))]
93 _ => unreachable!("variant added to NewPushRule not serializable to request body"),
94 }
95 }
96 }
97
98 #[cfg(feature = "client")]
99 impl ruma_common::api::OutgoingRequest for Request {
100 type Body = RequestBody;
101 type EndpointError = Error;
102 type IncomingResponse = Response;
103
104 fn try_into_http_request_inner(
105 self,
106 base_url: &str,
107 considering: std::borrow::Cow<'_, ruma_common::api::SupportedVersions>,
108 ) -> Result<http::Request<RequestBody>, ruma_common::api::error::IntoHttpError> {
109 use ruma_common::api::Metadata;
110
111 let Self { rule, before, after } = self;
112
113 let query_string = serde_html_form::to_string(RequestQuery { before, after })?;
114
115 let url = Self::make_endpoint_url(
116 considering,
117 base_url,
118 &[&rule.kind(), &rule.rule_id()],
119 &query_string,
120 )?;
121
122 let http_request =
123 http::Request::builder().method(Self::METHOD).uri(url).body(RequestBody(rule))?;
124
125 Ok(http_request)
126 }
127 }
128
129 #[cfg(feature = "server")]
130 impl ruma_common::api::IncomingRequest for Request {
131 type EndpointError = Error;
132 type OutgoingResponse = Response;
133
134 fn try_from_http_request_inner(
135 request: http::Request<&[u8]>,
136 path_args: &[&str],
137 ) -> Result<Self, ruma_common::api::error::DeserializationError> {
138 use ruma_common::push::{
139 NewConditionalPushRule, NewPatternedPushRule, NewSimplePushRule,
140 };
141
142 #[derive(Debug, serde::Deserialize)]
144 #[serde(rename_all = "lowercase")]
145 enum RuleKind {
146 Override,
147 Underride,
148 Sender,
149 Room,
150 Content,
151 }
152
153 let (kind, rule_id): (RuleKind, String) =
154 serde::Deserialize::deserialize(serde::de::value::SeqDeserializer::<
155 _,
156 serde::de::value::Error,
157 >::new(path_args.iter().copied()))?;
158
159 let RequestQuery { before, after } =
160 serde_html_form::from_str(request.uri().query().unwrap_or(""))?;
161
162 let rule = match kind {
163 RuleKind::Override => {
164 let ConditionalRequestBody { actions, conditions } =
165 serde_json::from_slice(request.body())?;
166 NewPushRule::Override(NewConditionalPushRule::new(rule_id, conditions, actions))
167 }
168 RuleKind::Underride => {
169 let ConditionalRequestBody { actions, conditions } =
170 serde_json::from_slice(request.body())?;
171 NewPushRule::Underride(NewConditionalPushRule::new(
172 rule_id, conditions, actions,
173 ))
174 }
175 RuleKind::Sender => {
176 let SimpleRequestBody { actions } = serde_json::from_slice(request.body())?;
177 let rule_id = rule_id.try_into()?;
178 NewPushRule::Sender(NewSimplePushRule::new(rule_id, actions))
179 }
180 RuleKind::Room => {
181 let SimpleRequestBody { actions } = serde_json::from_slice(request.body())?;
182 let rule_id = rule_id.try_into()?;
183 NewPushRule::Room(NewSimplePushRule::new(rule_id, actions))
184 }
185 RuleKind::Content => {
186 let PatternedRequestBody { actions, pattern } =
187 serde_json::from_slice(request.body())?;
188 NewPushRule::Content(NewPatternedPushRule::new(rule_id, pattern, actions))
189 }
190 };
191
192 Ok(Self { rule, before, after })
193 }
194 }
195
196 #[derive(Debug)]
197 #[cfg_attr(feature = "client", derive(serde::Serialize))]
198 #[cfg_attr(feature = "server", derive(serde::Deserialize))]
199 struct RequestQuery {
200 #[serde(skip_serializing_if = "Option::is_none")]
201 before: Option<String>,
202
203 #[serde(skip_serializing_if = "Option::is_none")]
204 after: Option<String>,
205 }
206
207 #[derive(Debug)]
208 #[cfg_attr(feature = "client", derive(serde::Serialize))]
209 #[cfg_attr(feature = "server", derive(serde::Deserialize))]
210 struct SimpleRequestBody {
211 actions: Vec<Action>,
212 }
213
214 #[derive(Debug)]
215 #[cfg_attr(feature = "client", derive(serde::Serialize))]
216 #[cfg_attr(feature = "server", derive(serde::Deserialize))]
217 struct PatternedRequestBody {
218 actions: Vec<Action>,
219
220 pattern: String,
221 }
222
223 #[derive(Debug)]
224 #[cfg_attr(feature = "client", derive(serde::Serialize))]
225 #[cfg_attr(feature = "server", derive(serde::Deserialize))]
226 struct ConditionalRequestBody {
227 actions: Vec<Action>,
228
229 conditions: Vec<PushCondition>,
230 }
231}