ruma_client_api/user_directory/
search_users.rs

1//! `POST /_matrix/client/*/user_directory/search`
2//!
3//! Performs a search for users.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3user_directorysearch
9
10    use http::header::ACCEPT_LANGUAGE;
11    use js_int::{UInt, uint};
12    use ruma_common::{
13        OwnedMxcUri, OwnedUserId,
14        api::{auth_scheme::AccessToken, request, response},
15        metadata,
16    };
17    use serde::{Deserialize, Serialize};
18
19    metadata! {
20        method: POST,
21        rate_limited: true,
22        authentication: AccessToken,
23        history: {
24            1.0 => "/_matrix/client/r0/user_directory/search",
25            1.1 => "/_matrix/client/v3/user_directory/search",
26        }
27    }
28
29    /// Request type for the `search_users` endpoint.
30    #[request(error = crate::Error)]
31    pub struct Request {
32        /// The term to search for.
33        pub search_term: String,
34
35        /// The maximum number of results to return.
36        ///
37        /// Defaults to 10.
38        #[serde(default = "default_limit", skip_serializing_if = "is_default_limit")]
39        pub limit: UInt,
40
41        /// Language tag to determine the collation to use for the (case-insensitive) search.
42        ///
43        /// See [MDN] for the syntax.
44        ///
45        /// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language#Syntax
46        #[ruma_api(header = ACCEPT_LANGUAGE)]
47        pub language: Option<String>,
48    }
49
50    /// Response type for the `search_users` endpoint.
51    #[response(error = crate::Error)]
52    pub struct Response {
53        /// Ordered by rank and then whether or not profile info is available.
54        pub results: Vec<User>,
55
56        /// Indicates if the result list has been truncated by the limit.
57        pub limited: bool,
58    }
59
60    impl Request {
61        /// Creates a new `Request` with the given search term.
62        pub fn new(search_term: String) -> Self {
63            Self { search_term, limit: default_limit(), language: None }
64        }
65    }
66
67    impl Response {
68        /// Creates a new `Response` with the given results and limited flag
69        pub fn new(results: Vec<User>, limited: bool) -> Self {
70            Self { results, limited }
71        }
72    }
73
74    fn default_limit() -> UInt {
75        uint!(10)
76    }
77
78    #[cfg(feature = "client")]
79    fn is_default_limit(limit: &UInt) -> bool {
80        limit == &default_limit()
81    }
82
83    /// User data as result of a search.
84    #[derive(Clone, Debug, Deserialize, Serialize)]
85    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
86    pub struct User {
87        /// The user's matrix user ID.
88        pub user_id: OwnedUserId,
89
90        /// The display name of the user, if one exists.
91        #[serde(skip_serializing_if = "Option::is_none")]
92        pub display_name: Option<String>,
93
94        /// The avatar url, as an MXC, if one exists.
95        ///
96        /// If you activate the `compat-empty-string-null` feature, this field being an empty
97        /// string in JSON will result in `None` here during deserialization.
98        #[serde(skip_serializing_if = "Option::is_none")]
99        #[cfg_attr(
100            feature = "compat-empty-string-null",
101            serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
102        )]
103        pub avatar_url: Option<OwnedMxcUri>,
104    }
105
106    impl User {
107        /// Create a new `User` with the given `UserId`.
108        pub fn new(user_id: OwnedUserId) -> Self {
109            Self { user_id, display_name: None, avatar_url: None }
110        }
111    }
112}