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