diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index 87e885ab7..89f1ff89e 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -42,9 +42,9 @@ use ruma::{ config::{get_global_account_data, set_global_account_data}, error::ErrorKind, profile::{ - AvatarUrl, DisplayName, ProfileFieldName, ProfileFieldValue, StaticProfileField, - delete_profile_field, get_profile, get_profile_field, set_avatar_url, - set_display_name, set_profile_field, + DisplayName, ProfileFieldName, ProfileFieldValue, StaticProfileField, + delete_profile_field, get_avatar_url, get_profile, get_profile_field, + set_avatar_url, set_display_name, set_profile_field, }, uiaa::AuthData, }, @@ -178,8 +178,15 @@ impl Account { /// ``` pub async fn get_avatar_url(&self) -> Result> { let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; - let avatar_url = - self.fetch_profile_field_of_static::(user_id.to_owned()).await?; + + #[allow(deprecated)] // get_profile_field fails when the response is {"avatar_url":null} 🤷‍♂️ + let request = get_avatar_url::v3::Request::new(user_id.to_owned()); + let avatar_url = self + .client + .send(request) + .with_request_config(RequestConfig::short_retry().force_auth()) + .await? + .avatar_url; if let Some(url) = avatar_url.clone() { // If an avatar is found cache it. diff --git a/crates/matrix-sdk/tests/integration/account.rs b/crates/matrix-sdk/tests/integration/account.rs index 11635b111..9a11510bb 100644 --- a/crates/matrix-sdk/tests/integration/account.rs +++ b/crates/matrix-sdk/tests/integration/account.rs @@ -248,6 +248,29 @@ async fn test_fetch_user_profile() { assert_eq!(profile.get_static::().unwrap(), None); } +#[async_test] +async fn test_fetch_removed_avatar_url() { + let server = MatrixMockServer::new().await; + let client = server.client_builder().server_versions(vec![MatrixVersion::V1_16]).build().await; + let user_id = client.user_id().unwrap(); + + server + .mock_get_profile_field(user_id, ProfileFieldName::AvatarUrl) + .respond_with(ResponseTemplate::new(200).set_body_json( + // This is what Synapse returns after calling Account::set_avatar_url(None). + json!({"avatar_url":null}), + )) + .mock_once() + .named("get avatar_url") + .mount() + .await; + + let account = client.account(); + + let res_avatar_url = account.get_avatar_url().await.unwrap(); + assert_eq!(res_avatar_url, None); +} + #[async_test] async fn test_get_cached_avatar_url() { let avatar_url = mxc_uri!("mxc://localhost/1mA63");