ruma_client_api/profile/
set_avatar_url.rs

1//! `PUT /_matrix/client/*/profile/{userId}/avatar_url`
2//!
3//! Set the avatar URL of the user.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/v1.15/client-server-api/#put_matrixclientv3profileuseridavatar_url
9
10    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 type for the `set_avatar_url` endpoint.
26    #[request(error = crate::Error)]
27    pub struct Request {
28        /// The user whose avatar URL will be set.
29        #[ruma_api(path)]
30        pub user_id: OwnedUserId,
31
32        /// The new avatar URL for the user.
33        ///
34        /// `None` is used to unset the avatar.
35        ///
36        /// If you activate the `compat-empty-string-null` feature, this field being an empty
37        /// string in JSON will result in `None` here during deserialization.
38        ///
39        /// If you active the `compat-unset-avatar` feature, this field being `None` will result
40        /// in an empty string in serialization, which is the same thing Element Web does (c.f.
41        /// <https://github.com/matrix-org/matrix-spec/issues/378#issuecomment-1055831264>).
42        #[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        /// The [BlurHash](https://blurha.sh) for the avatar pointed to by `avatar_url`.
57        ///
58        /// This uses the unstable prefix in
59        /// [MSC2448](https://github.com/matrix-org/matrix-spec-proposals/pull/2448).
60        #[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 type for the `set_avatar_url` endpoint.
66    #[response(error = crate::Error)]
67    #[derive(Default)]
68    pub struct Response {}
69
70    impl Request {
71        /// Creates a new `Request` with the given user ID and avatar URL.
72        #[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        /// Creates an empty `Response`.
85        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}