ruma_federation_api/query/
get_profile_information.rs1pub mod v1 {
6 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]
31 pub struct Request {
32 #[ruma_api(query)]
34 pub user_id: OwnedUserId,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 #[ruma_api(query)]
39 pub field: Option<ProfileFieldName>,
40 }
41
42 impl Request {
43 pub fn new(user_id: OwnedUserId) -> Self {
45 Self { user_id, field: None }
46 }
47 }
48
49 #[response]
51 #[derive(Default)]
52 pub struct Response {
53 #[ruma_api(body)]
55 data: BTreeMap<String, JsonValue>,
56 }
57
58 impl Response {
59 pub fn new() -> Self {
61 Self::default()
62 }
63
64 pub fn get(&self, field: &str) -> Option<&JsonValue> {
66 self.data.get(field)
67 }
68
69 pub fn iter(&self) -> btree_map::Iter<'_, String, JsonValue> {
71 self.data.iter()
72 }
73
74 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}