From 38d28e9aa042e643032ea4ef87d9319be2497f52 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 26 Jul 2023 15:54:47 +0200 Subject: [PATCH] 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`. --- .../src/room_list_service/room.rs | 10 +++++++++- .../tests/integration/room_list_service.rs | 20 ++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/matrix-sdk-ui/src/room_list_service/room.rs b/crates/matrix-sdk-ui/src/room_list_service/room.rs index 54dedb895..5dc824ec7 100644 --- a/crates/matrix-sdk-ui/src/room_list_service/room.rs +++ b/crates/matrix-sdk-ui/src/room_list_service/room.rs @@ -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 { + 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 diff --git a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs index 737ffde99..2fd64a097 100644 --- a/crates/matrix-sdk-ui/tests/integration/room_list_service.rs +++ b/crates/matrix-sdk-ui/tests/integration/room_list_service.rs @@ -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(()) }