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::btree_map;
11
12    use ruma_common::{
13        OwnedUserId,
14        api::{request, response},
15        metadata,
16        profile::{ProfileFieldName, ProfileFieldValue, StaticProfileField, UserProfile},
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        pub data: UserProfile,
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        /// Returns the value of the given [`StaticProfileField`].
70        ///
71        /// Returns `Ok(Some(_))` if the field is present and the value was deserialized
72        /// successfully, `Ok(None)` if the field is not set, or an error if deserialization of the
73        /// value failed.
74        pub fn get_static<F: StaticProfileField>(
75            &self,
76        ) -> Result<Option<F::Value>, serde_json::Error> {
77            self.data.get_static::<F>()
78        }
79
80        /// Gets an iterator over the fields of the profile.
81        pub fn iter(&self) -> btree_map::Iter<'_, String, JsonValue> {
82            self.data.iter()
83        }
84
85        /// Sets a field to the given value.
86        pub fn set(&mut self, field: String, value: JsonValue) {
87            self.data.set(field, value);
88        }
89    }
90
91    impl From<UserProfile> for Response {
92        fn from(value: UserProfile) -> Self {
93            Self { data: value }
94        }
95    }
96
97    impl FromIterator<(String, JsonValue)> for Response {
98        fn from_iter<T: IntoIterator<Item = (String, JsonValue)>>(iter: T) -> Self {
99            Self { data: UserProfile::from_iter(iter) }
100        }
101    }
102
103    impl FromIterator<(ProfileFieldName, JsonValue)> for Response {
104        fn from_iter<T: IntoIterator<Item = (ProfileFieldName, JsonValue)>>(iter: T) -> Self {
105            Self { data: UserProfile::from_iter(iter) }
106        }
107    }
108
109    impl FromIterator<ProfileFieldValue> for Response {
110        fn from_iter<T: IntoIterator<Item = ProfileFieldValue>>(iter: T) -> Self {
111            Self { data: UserProfile::from_iter(iter) }
112        }
113    }
114
115    impl Extend<(String, JsonValue)> for Response {
116        fn extend<T: IntoIterator<Item = (String, JsonValue)>>(&mut self, iter: T) {
117            self.data.extend(iter);
118        }
119    }
120
121    impl Extend<(ProfileFieldName, JsonValue)> for Response {
122        fn extend<T: IntoIterator<Item = (ProfileFieldName, JsonValue)>>(&mut self, iter: T) {
123            self.data.extend(iter);
124        }
125    }
126
127    impl Extend<ProfileFieldValue> for Response {
128        fn extend<T: IntoIterator<Item = ProfileFieldValue>>(&mut self, iter: T) {
129            self.data.extend(iter);
130        }
131    }
132
133    impl IntoIterator for Response {
134        type Item = (String, JsonValue);
135        type IntoIter = btree_map::IntoIter<String, JsonValue>;
136
137        fn into_iter(self) -> Self::IntoIter {
138            self.data.into_iter()
139        }
140    }
141}