Skip to main content

ruma_federation_api/query/
get_profile_information.rs

1//! `GET /_matrix/federation/*/query/profile`
2//!
3//! Get profile information, such as a display name or avatar, for a given user.
4
5pub mod v1 {
6    //! `/v1/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/v1.18/server-server-api/#get_matrixfederationv1queryprofile
9
10    use std::collections::{BTreeMap, btree_map};
11
12    use ruma_common::{
13        OwnedUserId,
14        api::{request, response},
15        metadata,
16        profile::{ProfileFieldName, ProfileFieldValue},
17    };
18    use serde_json::Value as JsonValue;
19
20    use crate::authentication::ServerSignatures;
21
22    metadata! {
23        method: GET,
24        rate_limited: false,
25        authentication: ServerSignatures,
26        path: "/_matrix/federation/v1/query/profile",
27    }
28
29    /// Request type for the `get_profile_information` endpoint.
30    #[request]
31    pub struct Request {
32        /// User ID to query.
33        #[ruma_api(query)]
34        pub user_id: OwnedUserId,
35
36        /// Profile field to query.
37        #[serde(skip_serializing_if = "Option::is_none")]
38        #[ruma_api(query)]
39        pub field: Option<ProfileFieldName>,
40    }
41
42    impl Request {
43        /// Creates a new `Request` with the given user id.
44        pub fn new(user_id: OwnedUserId) -> Self {
45            Self { user_id, field: None }
46        }
47    }
48
49    /// Response type for the `get_profile_information` endpoint.
50    #[response]
51    #[derive(Default)]
52    pub struct Response {
53        /// The profile data.
54        #[ruma_api(body)]
55        data: BTreeMap<String, JsonValue>,
56    }
57
58    impl Response {
59        /// Creates a new empty `Response`.
60        pub fn new() -> Self {
61            Self::default()
62        }
63
64        /// Returns the value of the given profile field.
65        pub fn get(&self, field: &str) -> Option<&JsonValue> {
66            self.data.get(field)
67        }
68
69        /// Gets an iterator over the fields of the profile.
70        pub fn iter(&self) -> btree_map::Iter<'_, String, JsonValue> {
71            self.data.iter()
72        }
73
74        /// Sets a field to the given value.
75        pub fn set(&mut self, field: String, value: JsonValue) {
76            self.data.insert(field, value);
77        }
78    }
79
80    impl FromIterator<(String, JsonValue)> for Response {
81        fn from_iter<T: IntoIterator<Item = (String, JsonValue)>>(iter: T) -> Self {
82            Self { data: iter.into_iter().collect() }
83        }
84    }
85
86    impl FromIterator<(ProfileFieldName, JsonValue)> for Response {
87        fn from_iter<T: IntoIterator<Item = (ProfileFieldName, JsonValue)>>(iter: T) -> Self {
88            iter.into_iter().map(|(field, value)| (field.as_str().to_owned(), value)).collect()
89        }
90    }
91
92    impl FromIterator<ProfileFieldValue> for Response {
93        fn from_iter<T: IntoIterator<Item = ProfileFieldValue>>(iter: T) -> Self {
94            iter.into_iter().map(|value| (value.field_name(), value.value().into_owned())).collect()
95        }
96    }
97
98    impl Extend<(String, JsonValue)> for Response {
99        fn extend<T: IntoIterator<Item = (String, JsonValue)>>(&mut self, iter: T) {
100            self.data.extend(iter);
101        }
102    }
103
104    impl Extend<(ProfileFieldName, JsonValue)> for Response {
105        fn extend<T: IntoIterator<Item = (ProfileFieldName, JsonValue)>>(&mut self, iter: T) {
106            self.extend(iter.into_iter().map(|(field, value)| (field.as_str().to_owned(), value)));
107        }
108    }
109
110    impl Extend<ProfileFieldValue> for Response {
111        fn extend<T: IntoIterator<Item = ProfileFieldValue>>(&mut self, iter: T) {
112            self.extend(
113                iter.into_iter().map(|value| (value.field_name(), value.value().into_owned())),
114            );
115        }
116    }
117
118    impl IntoIterator for Response {
119        type Item = (String, JsonValue);
120        type IntoIter = btree_map::IntoIter<String, JsonValue>;
121
122        fn into_iter(self) -> Self::IntoIter {
123            self.data.into_iter()
124        }
125    }
126}