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    fn is_default_limit(limit: &UInt) -> bool {
79        limit == &default_limit()
80    }
81
82    /// User data as result of a search.
83    #[derive(Clone, Debug, Deserialize, Serialize)]
84    #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
85    pub struct User {
86        /// The user's matrix user ID.
87        pub user_id: OwnedUserId,
88
89        /// The display name of the user, if one exists.
90        #[serde(skip_serializing_if = "Option::is_none")]
91        pub display_name: Option<String>,
92
93        /// The avatar url, as an MXC, if one exists.
94        ///
95        /// If you activate the `compat-empty-string-null` feature, this field being an empty
96        /// string in JSON will result in `None` here during deserialization.
97        #[serde(skip_serializing_if = "Option::is_none")]
98        #[cfg_attr(
99            feature = "compat-empty-string-null",
100            serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
101        )]
102        pub avatar_url: Option<OwnedMxcUri>,
103    }
104
105    impl User {
106        /// Create a new `User` with the given `UserId`.
107        pub fn new(user_id: OwnedUserId) -> Self {
108            Self { user_id, display_name: None, avatar_url: None }
109        }
110    }
111}