ruma_client_api/profile/
get_profile.rs1pub mod v3 {
6 use std::collections::btree_map;
11
12 use ruma_common::{
13 OwnedUserId,
14 api::{auth_scheme::NoAccessToken, request, response},
15 metadata,
16 profile::{ProfileFieldName, ProfileFieldValue, StaticProfileField, UserProfile},
17 };
18 use serde_json::Value as JsonValue;
19
20 metadata! {
21 method: GET,
22 rate_limited: false,
23 authentication: NoAccessToken,
24 history: {
25 1.0 => "/_matrix/client/r0/profile/{user_id}",
26 1.1 => "/_matrix/client/v3/profile/{user_id}",
27 }
28 }
29
30 #[request]
32 pub struct Request {
33 #[ruma_api(path)]
35 pub user_id: OwnedUserId,
36 }
37
38 #[response]
40 #[derive(Default)]
41 pub struct Response {
42 #[ruma_api(body)]
44 pub data: UserProfile,
45 }
46
47 impl Request {
48 pub fn new(user_id: OwnedUserId) -> Self {
50 Self { user_id }
51 }
52 }
53
54 impl Response {
55 pub fn new() -> Self {
57 Self::default()
58 }
59
60 pub fn get(&self, field: &str) -> Option<&JsonValue> {
62 self.data.get(field)
63 }
64
65 pub fn get_static<F: StaticProfileField>(
71 &self,
72 ) -> Result<Option<F::Value>, serde_json::Error> {
73 self.data.get_static::<F>()
74 }
75
76 pub fn iter(&self) -> btree_map::Iter<'_, String, JsonValue> {
78 self.data.iter()
79 }
80
81 pub fn set(&mut self, field: String, value: JsonValue) {
83 self.data.set(field, value);
84 }
85 }
86
87 impl From<UserProfile> for Response {
88 fn from(value: UserProfile) -> Self {
89 Self { data: value }
90 }
91 }
92
93 impl FromIterator<(String, JsonValue)> for Response {
94 fn from_iter<T: IntoIterator<Item = (String, JsonValue)>>(iter: T) -> Self {
95 Self { data: UserProfile::from_iter(iter) }
96 }
97 }
98
99 impl FromIterator<(ProfileFieldName, JsonValue)> for Response {
100 fn from_iter<T: IntoIterator<Item = (ProfileFieldName, JsonValue)>>(iter: T) -> Self {
101 Self { data: UserProfile::from_iter(iter) }
102 }
103 }
104
105 impl FromIterator<ProfileFieldValue> for Response {
106 fn from_iter<T: IntoIterator<Item = ProfileFieldValue>>(iter: T) -> Self {
107 Self { data: UserProfile::from_iter(iter) }
108 }
109 }
110
111 impl Extend<(String, JsonValue)> for Response {
112 fn extend<T: IntoIterator<Item = (String, JsonValue)>>(&mut self, iter: T) {
113 self.data.extend(iter);
114 }
115 }
116
117 impl Extend<(ProfileFieldName, JsonValue)> for Response {
118 fn extend<T: IntoIterator<Item = (ProfileFieldName, JsonValue)>>(&mut self, iter: T) {
119 self.data.extend(iter);
120 }
121 }
122
123 impl Extend<ProfileFieldValue> for Response {
124 fn extend<T: IntoIterator<Item = ProfileFieldValue>>(&mut self, iter: T) {
125 self.data.extend(iter);
126 }
127 }
128
129 impl IntoIterator for Response {
130 type Item = (String, JsonValue);
131 type IntoIter = btree_map::IntoIter<String, JsonValue>;
132
133 fn into_iter(self) -> Self::IntoIter {
134 self.data.into_iter()
135 }
136 }
137}
138
139#[cfg(test)]
140mod tests {
141 use serde_json::json;
142
143 use super::v3::Response;
144
145 #[test]
146 #[cfg(feature = "server")]
147 fn serialize_response() {
148 use ruma_common::{api::OutgoingResponse, owned_mxc_uri, profile::ProfileFieldValue};
149 use serde_json::{Value as JsonValue, from_slice as from_json_slice};
150
151 let response = [
152 ProfileFieldValue::AvatarUrl(owned_mxc_uri!("mxc://localhost/abcdef")),
153 ProfileFieldValue::DisplayName("Alice".to_owned()),
154 ProfileFieldValue::new("custom_field", "value".into()).unwrap(),
155 ]
156 .into_iter()
157 .collect::<Response>();
158
159 let http_response = response.try_into_http_response::<Vec<u8>>().unwrap();
160
161 assert_eq!(
162 from_json_slice::<JsonValue>(http_response.body().as_ref()).unwrap(),
163 json!({
164 "avatar_url": "mxc://localhost/abcdef",
165 "displayname": "Alice",
166 "custom_field": "value",
167 })
168 );
169 }
170
171 #[test]
172 #[cfg(feature = "client")]
173 fn deserialize_response() {
174 use ruma_common::api::IncomingResponse;
175 use serde_json::to_vec as to_json_vec;
176
177 use crate::profile::{AvatarUrl, DisplayName};
178
179 let body = to_json_vec(&json!({
181 "avatar_url": "mxc://localhost/abcdef",
182 "displayname": "Alice",
183 "custom_field": "value",
184 }))
185 .unwrap();
186
187 let response = Response::try_from_http_response(http::Response::new(body)).unwrap();
188 assert_eq!(response.get_static::<AvatarUrl>().unwrap().unwrap(), "mxc://localhost/abcdef");
189 assert_eq!(response.get_static::<DisplayName>().unwrap().unwrap(), "Alice");
190 assert_eq!(response.get("custom_field").unwrap().as_str().unwrap(), "value");
191
192 let body = to_json_vec(&json!({
194 "custom_field": null,
195 }))
196 .unwrap();
197
198 let response = Response::try_from_http_response(http::Response::new(body)).unwrap();
199 assert_eq!(response.get_static::<AvatarUrl>().unwrap(), None);
200 assert_eq!(response.get_static::<DisplayName>().unwrap(), None);
201 assert!(response.get("custom_field").unwrap().is_null());
202 }
203}