ruma_client_api/profile/
set_avatar_url.rs1pub mod v3 {
6    use ruma_common::{
11        api::{request, response, Metadata},
12        metadata, OwnedMxcUri, OwnedUserId,
13    };
14
15    const METADATA: Metadata = 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        pub fn new(user_id: OwnedUserId, avatar_url: Option<OwnedMxcUri>) -> Self {
73            Self {
74                user_id,
75                avatar_url,
76                #[cfg(feature = "unstable-msc2448")]
77                blurhash: None,
78            }
79        }
80    }
81
82    impl Response {
83        pub fn new() -> Self {
85            Self {}
86        }
87    }
88
89    #[cfg(all(test, feature = "server"))]
90    mod tests {
91        use ruma_common::api::IncomingRequest as _;
92
93        use super::Request;
94
95        #[test]
96        fn deserialize_unset_request() {
97            let req = Request::try_from_http_request(
98                http::Request::builder()
99                    .method("PUT")
100                    .uri("https://bar.org/_matrix/client/r0/profile/@foo:bar.org/avatar_url")
101                    .body(&[] as &[u8])
102                    .unwrap(),
103                &["@foo:bar.org"],
104            )
105            .unwrap();
106            assert_eq!(req.user_id, "@foo:bar.org");
107            assert_eq!(req.avatar_url, None);
108
109            #[cfg(feature = "compat-empty-string-null")]
110            {
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(serde_json::to_vec(&serde_json::json!({ "avatar_url": "" })).unwrap())
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        }
124    }
125}