fix: Fix a deserialisation failure when the avatar_url is null.

This commit is contained in:
Doug
2026-02-09 16:42:01 +00:00
committed by Damir Jelić
parent df6fc21576
commit c246fd8f4d
2 changed files with 35 additions and 5 deletions
+12 -5
View File
@@ -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<Option<OwnedMxcUri>> {
let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?;
let avatar_url =
self.fetch_profile_field_of_static::<AvatarUrl>(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.
@@ -248,6 +248,29 @@ async fn test_fetch_user_profile() {
assert_eq!(profile.get_static::<AvatarUrl>().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");