ruma_client_api/filter/
create_filter.rs
1pub mod v3 {
6 use ruma_common::{
11 api::{request, response, Metadata},
12 metadata, OwnedUserId,
13 };
14
15 use crate::filter::FilterDefinition;
16
17 const METADATA: Metadata = metadata! {
18 method: POST,
19 rate_limited: false,
20 authentication: AccessToken,
21 history: {
22 1.0 => "/_matrix/client/r0/user/:user_id/filter",
23 1.1 => "/_matrix/client/v3/user/:user_id/filter",
24 }
25 };
26
27 #[request(error = crate::Error)]
29 pub struct Request {
30 #[ruma_api(path)]
34 pub user_id: OwnedUserId,
35
36 #[ruma_api(body)]
38 pub filter: FilterDefinition,
39 }
40
41 #[response(error = crate::Error)]
43 pub struct Response {
44 pub filter_id: String,
46 }
47
48 impl Request {
49 pub fn new(user_id: OwnedUserId, filter: FilterDefinition) -> Self {
51 Self { user_id, filter }
52 }
53 }
54
55 impl Response {
56 pub fn new(filter_id: String) -> Self {
58 Self { filter_id }
59 }
60 }
61
62 #[cfg(all(test, any(feature = "client", feature = "server")))]
63 mod tests {
64 #[cfg(feature = "server")]
65 #[test]
66 fn deserialize_request() {
67 use ruma_common::api::IncomingRequest as _;
68
69 use super::Request;
70
71 let req = Request::try_from_http_request(
72 http::Request::builder()
73 .method(http::Method::POST)
74 .uri("https://matrix.org/_matrix/client/r0/user/@foo:bar.com/filter")
75 .body(b"{}" as &[u8])
76 .unwrap(),
77 &["@foo:bar.com"],
78 )
79 .unwrap();
80
81 assert_eq!(req.user_id, "@foo:bar.com");
82 assert!(req.filter.is_empty());
83 }
84
85 #[cfg(feature = "client")]
86 #[test]
87 fn serialize_request() {
88 use ruma_common::{
89 api::{MatrixVersion, OutgoingRequest, SendAccessToken},
90 owned_user_id,
91 };
92
93 use crate::filter::FilterDefinition;
94
95 let req =
96 super::Request::new(owned_user_id!("@foo:bar.com"), FilterDefinition::default())
97 .try_into_http_request::<Vec<u8>>(
98 "https://matrix.org",
99 SendAccessToken::IfRequired("tok"),
100 &[MatrixVersion::V1_1],
101 )
102 .unwrap();
103 assert_eq!(req.body(), b"{}");
104 }
105 }
106}