feat(ui): Implement Room::avatar_url.

This patch implements `Room::avatar_url`. It tries to calculate
the best avatar URL as much as possible. It's either the URL from
`SlidingSyncRoom::avatar_url` or from `Room::avatar_url`.
This commit is contained in:
Ivan Enderlin
2023-07-26 15:54:47 +02:00
parent 62a203f41e
commit 38d28e9aa0
2 changed files with 26 additions and 4 deletions

View File

@@ -20,7 +20,7 @@ use async_once_cell::OnceCell as AsyncOnceCell;
use matrix_sdk::{SlidingSync, SlidingSyncRoom};
use ruma::{
api::client::sync::sync_events::{v4::RoomSubscription, UnreadNotificationsCount},
RoomId,
OwnedMxcUri, RoomId,
};
use super::Error;
@@ -89,6 +89,14 @@ impl Room {
})
}
/// Get the best possible avatar for the room.
///
/// If the sliding sync room has received an avatar from the server, then
/// use it, otherwise, let's try to find one from `Room`.
pub fn avatar_url(&self) -> Option<OwnedMxcUri> {
self.inner.sliding_sync_room.avatar_url().or_else(|| self.inner.room.avatar_url())
}
/// Get the underlying [`matrix_sdk::Room`].
pub fn inner_room(&self) -> &matrix_sdk::Room {
&self.inner.room

View File

@@ -18,7 +18,7 @@ use ruma::{
api::client::sync::sync_events::{v4::RoomSubscription, UnreadNotificationsCount},
assign, event_id,
events::StateEventType,
room_id, uint,
mxc_uri, room_id, uint,
};
use serde_json::json;
use stream_assert::{assert_next_matches, assert_pending};
@@ -1697,6 +1697,7 @@ async fn test_room() -> Result<(), Error> {
"rooms": {
room_id_0: {
"name": "Room #0",
"avatar": "mxc://homeserver/media",
"initial": true,
},
room_id_1: {
@@ -1706,14 +1707,23 @@ async fn test_room() -> Result<(), Error> {
},
};
// Room has received a name from sliding sync.
let room0 = room_list.room(room_id_0).await?;
// Room has received a name from sliding sync.
assert_eq!(room0.name().await, Some("Room #0".to_owned()));
// Room has not received a name from sliding sync, then it's calculated.
// Room has received an avatar from sliding sync.
assert_eq!(room0.avatar_url(), Some(mxc_uri!("mxc://homeserver/media").to_owned()));
let room1 = room_list.room(room_id_1).await?;
// Room has not received a name from sliding sync, then it's calculated.
assert_eq!(room1.name().await, Some("Empty Room".to_owned()));
// Room has not received an avatar from sliding sync, then it's calculated, but
// there is nothing to calculate from, so there is no URL.
assert_eq!(room1.avatar_url(), None);
sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
assert request >= {},
@@ -1736,6 +1746,7 @@ async fn test_room() -> Result<(), Error> {
"rooms": {
room_id_1: {
"name": "Room #1",
"avatar": "mxc://homeserver/other-media",
},
},
},
@@ -1744,6 +1755,9 @@ async fn test_room() -> Result<(), Error> {
// Room has _now_ received a name from sliding sync!
assert_eq!(room1.name().await, Some("Room #1".to_owned()));
// Room has _now_ received an avatar URL from sliding sync!
assert_eq!(room1.avatar_url(), Some(mxc_uri!("mxc://homeserver/other-media").to_owned()));
Ok(())
}