ruma_client_api/filter/
create_filter.rs

1//! `POST /_matrix/client/*/user/{userId}/filter`
2//!
3//! Create a new filter for event retrieval.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3useruseridfilter
9
10    use ruma_common::{
11        OwnedUserId,
12        api::{auth_scheme::AccessToken, request, response},
13        metadata,
14    };
15
16    use crate::filter::FilterDefinition;
17
18    metadata! {
19        method: POST,
20        rate_limited: false,
21        authentication: AccessToken,
22        history: {
23            1.0 => "/_matrix/client/r0/user/{user_id}/filter",
24            1.1 => "/_matrix/client/v3/user/{user_id}/filter",
25        }
26    }
27
28    /// Request type for the `create_filter` endpoint.
29    #[request(error = crate::Error)]
30    pub struct Request {
31        /// The ID of the user uploading the filter.
32        ///
33        /// The access token must be authorized to make requests for this user ID.
34        #[ruma_api(path)]
35        pub user_id: OwnedUserId,
36
37        /// The filter definition.
38        #[ruma_api(body)]
39        pub filter: FilterDefinition,
40    }
41
42    /// Response type for the `create_filter` endpoint.
43    #[response(error = crate::Error)]
44    pub struct Response {
45        /// The ID of the filter that was created.
46        pub filter_id: String,
47    }
48
49    impl Request {
50        /// Creates a new `Request` with the given user ID and filter definition.
51        pub fn new(user_id: OwnedUserId, filter: FilterDefinition) -> Self {
52            Self { user_id, filter }
53        }
54    }
55
56    impl Response {
57        /// Creates a new `Response` with the given filter ID.
58        pub fn new(filter_id: String) -> Self {
59            Self { filter_id }
60        }
61    }
62
63    #[cfg(all(test, any(feature = "client", feature = "server")))]
64    mod tests {
65        #[cfg(feature = "server")]
66        #[test]
67        fn deserialize_request() {
68            use ruma_common::api::IncomingRequest as _;
69
70            use super::Request;
71
72            let req = Request::try_from_http_request(
73                http::Request::builder()
74                    .method(http::Method::POST)
75                    .uri("https://matrix.org/_matrix/client/r0/user/@foo:bar.com/filter")
76                    .body(b"{}" as &[u8])
77                    .unwrap(),
78                &["@foo:bar.com"],
79            )
80            .unwrap();
81
82            assert_eq!(req.user_id, "@foo:bar.com");
83            assert!(req.filter.is_empty());
84        }
85
86        #[cfg(feature = "client")]
87        #[test]
88        fn serialize_request() {
89            use std::borrow::Cow;
90
91            use ruma_common::{
92                api::{
93                    MatrixVersion, OutgoingRequest, SupportedVersions, auth_scheme::SendAccessToken,
94                },
95                owned_user_id,
96            };
97
98            use crate::filter::FilterDefinition;
99
100            let supported = SupportedVersions {
101                versions: [MatrixVersion::V1_1].into(),
102                features: Default::default(),
103            };
104
105            let req =
106                super::Request::new(owned_user_id!("@foo:bar.com"), FilterDefinition::default())
107                    .try_into_http_request::<Vec<u8>>(
108                        "https://matrix.org",
109                        SendAccessToken::IfRequired("tok"),
110                        Cow::Owned(supported),
111                    )
112                    .unwrap();
113            assert_eq!(req.body(), b"{}");
114        }
115    }
116}