ruma_client_api/user_directory/
search_users.rs1pub mod v3 {
6 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(error = crate::Error)]
31 pub struct Request {
32 pub search_term: String,
34
35 #[serde(default = "default_limit", skip_serializing_if = "is_default_limit")]
39 pub limit: UInt,
40
41 #[ruma_api(header = ACCEPT_LANGUAGE)]
47 pub language: Option<String>,
48 }
49
50 #[response(error = crate::Error)]
52 pub struct Response {
53 pub results: Vec<User>,
55
56 pub limited: bool,
58 }
59
60 impl Request {
61 pub fn new(search_term: String) -> Self {
63 Self { search_term, limit: default_limit(), language: None }
64 }
65 }
66
67 impl Response {
68 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 #[derive(Clone, Debug, Deserialize, Serialize)]
84 #[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
85 pub struct User {
86 pub user_id: OwnedUserId,
88
89 #[serde(skip_serializing_if = "Option::is_none")]
91 pub display_name: Option<String>,
92
93 #[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 pub fn new(user_id: OwnedUserId) -> Self {
108 Self { user_id, display_name: None, avatar_url: None }
109 }
110 }
111}