ruma_client_api/profile/
set_avatar_url.rs1pub mod v3 {
6 use ruma_common::{
11 OwnedMxcUri, OwnedUserId,
12 api::{auth_scheme::AccessToken, request, response},
13 metadata,
14 };
15
16 #[cfg(feature = "unstable-msc4466")]
17 use crate::profile::PropagateTo;
18
19 metadata! {
20 method: PUT,
21 rate_limited: true,
22 authentication: AccessToken,
23 history: {
24 1.0 => "/_matrix/client/r0/profile/{user_id}/avatar_url",
25 1.1 => "/_matrix/client/v3/profile/{user_id}/avatar_url",
26 }
27 }
28
29 #[request]
31 pub struct Request {
32 #[ruma_api(path)]
34 pub user_id: OwnedUserId,
35
36 #[cfg_attr(
47 feature = "compat-empty-string-null",
48 serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
49 )]
50 #[cfg_attr(
51 feature = "compat-unset-avatar",
52 serde(serialize_with = "ruma_common::serde::none_as_empty_string")
53 )]
54 #[cfg_attr(
55 not(feature = "compat-unset-avatar"),
56 serde(skip_serializing_if = "Option::is_none")
57 )]
58 pub avatar_url: Option<OwnedMxcUri>,
59
60 #[cfg(feature = "unstable-msc2448")]
65 #[serde(rename = "xyz.amorgan.blurhash", skip_serializing_if = "Option::is_none")]
66 pub blurhash: Option<String>,
67
68 #[cfg(feature = "unstable-msc4466")]
70 #[ruma_api(query)]
71 #[serde(rename = "computer.gingershaped.msc4466.propagate_to")]
72 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
73 pub propagate_to: PropagateTo,
74 }
75
76 #[response]
78 #[derive(Default)]
79 pub struct Response {}
80
81 impl Request {
82 #[deprecated = "Use the set_profile_field endpoint instead."]
84 pub fn new(user_id: OwnedUserId, avatar_url: Option<OwnedMxcUri>) -> Self {
85 Self {
86 user_id,
87 avatar_url,
88 #[cfg(feature = "unstable-msc2448")]
89 blurhash: None,
90 #[cfg(feature = "unstable-msc4466")]
91 propagate_to: PropagateTo::default(),
92 }
93 }
94 }
95
96 impl Response {
97 pub fn new() -> Self {
99 Self {}
100 }
101 }
102
103 #[cfg(all(test, feature = "server"))]
104 mod tests {
105 use ruma_common::api::IncomingRequest as _;
106
107 use super::Request;
108
109 #[test]
110 fn deserialize_unset_request() {
111 let req = Request::try_from_http_request(
112 http::Request::builder()
113 .method("PUT")
114 .uri("https://bar.org/_matrix/client/r0/profile/@foo:bar.org/avatar_url")
115 .body(&[] as &[u8])
116 .unwrap(),
117 &["@foo:bar.org"],
118 )
119 .unwrap();
120 assert_eq!(req.user_id, "@foo:bar.org");
121 assert_eq!(req.avatar_url, None);
122
123 #[cfg(feature = "compat-empty-string-null")]
124 {
125 let req = Request::try_from_http_request(
126 http::Request::builder()
127 .method("PUT")
128 .uri("https://bar.org/_matrix/client/r0/profile/@foo:bar.org/avatar_url")
129 .body(serde_json::to_vec(&serde_json::json!({ "avatar_url": "" })).unwrap())
130 .unwrap(),
131 &["@foo:bar.org"],
132 )
133 .unwrap();
134 assert_eq!(req.user_id, "@foo:bar.org");
135 assert_eq!(req.avatar_url, None);
136 }
137 }
138 }
139}