mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-16 12:43:01 -04:00
feat(sdk+ui+ffi): Implement (SlidingSyncRoom|Room|RoomListItem)::avatar_url
feat(sdk+ui+ffi): Implement `(SlidingSyncRoom|Room|RoomListItem)::avatar_url`
This commit is contained in:
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -2372,7 +2372,6 @@ checksum = "1d9a8bb6c7c71d151b25936b03e012a4c00daea99e3a3797c6ead66b0a0d55e2"
|
||||
dependencies = [
|
||||
"const_panic",
|
||||
"konst_kernel",
|
||||
"konst_proc_macros",
|
||||
"typewit",
|
||||
]
|
||||
|
||||
@@ -2385,12 +2384,6 @@ dependencies = [
|
||||
"typewit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "konst_proc_macros"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e28ab1dc35e09d60c2b8c90d12a9a8d9666c876c10a3739a3196db0103b6043"
|
||||
|
||||
[[package]]
|
||||
name = "kv-log-macro"
|
||||
version = "1.0.7"
|
||||
@@ -4085,7 +4078,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma"
|
||||
version = "0.8.2"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"assign",
|
||||
"js_int",
|
||||
@@ -4100,7 +4093,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-appservice-api"
|
||||
version = "0.8.1"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"js_int",
|
||||
"ruma-common",
|
||||
@@ -4111,7 +4104,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-client-api"
|
||||
version = "0.16.2"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"assign",
|
||||
"bytes",
|
||||
@@ -4128,7 +4121,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-common"
|
||||
version = "0.11.3"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"base64 0.21.2",
|
||||
"bytes",
|
||||
@@ -4161,7 +4154,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-federation-api"
|
||||
version = "0.7.1"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"js_int",
|
||||
"ruma-common",
|
||||
@@ -4172,7 +4165,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-identifiers-validation"
|
||||
version = "0.9.1"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"js_int",
|
||||
"thiserror",
|
||||
@@ -4181,7 +4174,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-macros"
|
||||
version = "0.11.3"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"proc-macro-crate",
|
||||
@@ -4196,7 +4189,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "ruma-push-gateway-api"
|
||||
version = "0.7.1"
|
||||
source = "git+https://github.com/ruma/ruma?rev=a641adb4287267ba9c4778ac1ee83b4ecab11e26#a641adb4287267ba9c4778ac1ee83b4ecab11e26"
|
||||
source = "git+https://github.com/ruma/ruma?rev=1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9#1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9"
|
||||
dependencies = [
|
||||
"js_int",
|
||||
"ruma-common",
|
||||
|
||||
@@ -34,8 +34,8 @@ futures-executor = "0.3.21"
|
||||
futures-util = { version = "0.3.26", default-features = false, features = ["alloc"] }
|
||||
http = "0.2.6"
|
||||
itertools = "0.11.0"
|
||||
ruma = { git = "https://github.com/ruma/ruma", rev = "a641adb4287267ba9c4778ac1ee83b4ecab11e26", features = ["client-api-c", "compat-upload-signatures", "compat-user-id"] }
|
||||
ruma-common = { git = "https://github.com/ruma/ruma", rev = "a641adb4287267ba9c4778ac1ee83b4ecab11e26" }
|
||||
ruma = { git = "https://github.com/ruma/ruma", rev = "1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9", features = ["client-api-c", "compat-upload-signatures", "compat-user-id"] }
|
||||
ruma-common = { git = "https://github.com/ruma/ruma", rev = "1d81c7c6d49aec053d5b84441bc5a4e62d8cc8a9" }
|
||||
once_cell = "1.16.0"
|
||||
serde = "1.0.151"
|
||||
serde_html_form = "0.2.0"
|
||||
|
||||
@@ -296,15 +296,15 @@ impl RoomListItem {
|
||||
RUNTIME.block_on(async { self.inner.name().await })
|
||||
}
|
||||
|
||||
pub fn is_direct(&self) -> bool {
|
||||
fn avatar_url(&self) -> Option<String> {
|
||||
self.inner.avatar_url().map(|uri| uri.to_string())
|
||||
}
|
||||
|
||||
fn is_direct(&self) -> bool {
|
||||
RUNTIME.block_on(async { self.inner.inner_room().is_direct().await.unwrap_or(false) })
|
||||
}
|
||||
|
||||
pub fn avatar_url(&self) -> Option<String> {
|
||||
self.inner.inner_room().avatar_url().map(|uri| uri.to_string())
|
||||
}
|
||||
|
||||
pub fn canonical_alias(&self) -> Option<String> {
|
||||
fn canonical_alias(&self) -> Option<String> {
|
||||
self.inner.inner_room().canonical_alias().map(|alias| alias.to_string())
|
||||
}
|
||||
|
||||
@@ -399,13 +399,15 @@ pub struct UnreadNotificationsCount {
|
||||
|
||||
#[uniffi::export]
|
||||
impl UnreadNotificationsCount {
|
||||
pub fn highlight_count(&self) -> u32 {
|
||||
fn highlight_count(&self) -> u32 {
|
||||
self.highlight_count
|
||||
}
|
||||
pub fn notification_count(&self) -> u32 {
|
||||
|
||||
fn notification_count(&self) -> u32 {
|
||||
self.notification_count
|
||||
}
|
||||
pub fn has_notifications(&self) -> bool {
|
||||
|
||||
fn has_notifications(&self) -> bool {
|
||||
self.notification_count != 0 || self.highlight_count != 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -19,7 +19,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};
|
||||
@@ -1694,6 +1694,7 @@ async fn test_room() -> Result<(), Error> {
|
||||
"rooms": {
|
||||
room_id_0: {
|
||||
"name": "Room #0",
|
||||
"avatar": "mxc://homeserver/media",
|
||||
"initial": true,
|
||||
},
|
||||
room_id_1: {
|
||||
@@ -1703,14 +1704,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 >= {},
|
||||
@@ -1733,6 +1743,7 @@ async fn test_room() -> Result<(), Error> {
|
||||
"rooms": {
|
||||
room_id_1: {
|
||||
"name": "Room #1",
|
||||
"avatar": "mxc://homeserver/other-media",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1741,6 +1752,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(())
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ use ruma::{
|
||||
api::client::sync::sync_events::{v4, UnreadNotificationsCount},
|
||||
events::AnySyncStateEvent,
|
||||
serde::Raw,
|
||||
OwnedRoomId, RoomId,
|
||||
OwnedMxcUri, OwnedRoomId, RoomId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -74,6 +74,13 @@ impl SlidingSyncRoom {
|
||||
inner.name.to_owned()
|
||||
}
|
||||
|
||||
/// Get the room avatar URL.
|
||||
pub fn avatar_url(&self) -> Option<OwnedMxcUri> {
|
||||
let inner = self.inner.inner.read().unwrap();
|
||||
|
||||
inner.avatar.clone()
|
||||
}
|
||||
|
||||
/// Is this a direct message?
|
||||
pub fn is_dm(&self) -> Option<bool> {
|
||||
let inner = self.inner.inner.read().unwrap();
|
||||
@@ -137,6 +144,7 @@ impl SlidingSyncRoom {
|
||||
) {
|
||||
let v4::SlidingSyncRoom {
|
||||
name,
|
||||
avatar,
|
||||
initial,
|
||||
limited,
|
||||
is_dm,
|
||||
@@ -159,6 +167,10 @@ impl SlidingSyncRoom {
|
||||
inner.name = name;
|
||||
}
|
||||
|
||||
if avatar.is_some() {
|
||||
inner.avatar = avatar;
|
||||
}
|
||||
|
||||
if initial.is_some() {
|
||||
inner.initial = initial;
|
||||
}
|
||||
@@ -307,7 +319,7 @@ mod tests {
|
||||
use matrix_sdk_base::deserialized_responses::TimelineEvent;
|
||||
use ruma::{
|
||||
api::client::sync::sync_events::v4, events::room::message::RoomMessageEventContent,
|
||||
room_id, uint, RoomId,
|
||||
mxc_uri, room_id, uint, RoomId,
|
||||
};
|
||||
use serde_json::json;
|
||||
use wiremock::MockServer;
|
||||
@@ -432,6 +444,14 @@ mod tests {
|
||||
_ = Some("gordon".to_owned());
|
||||
}
|
||||
|
||||
test_avatar {
|
||||
avatar_url() = None;
|
||||
receives room_response!({"avatar": "mxc://homeserver/media"});
|
||||
_ = Some(mxc_uri!("mxc://homeserver/media").to_owned());
|
||||
receives nothing;
|
||||
_ = Some(mxc_uri!("mxc://homeserver/media").to_owned());
|
||||
}
|
||||
|
||||
test_room_is_dm {
|
||||
is_dm() = None;
|
||||
receives room_response!({"is_dm": true});
|
||||
|
||||
Reference in New Issue
Block a user