From 87f417698f6d05e03e049a251802b25298201f92 Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 4 Jun 2024 11:58:58 +0100 Subject: [PATCH] ffi: Expose room heroes on the Room. --- bindings/matrix-sdk-ffi/src/room.rs | 28 +++++++++++++++++++++- crates/matrix-sdk-base/src/lib.rs | 4 ++-- crates/matrix-sdk-base/src/rooms/mod.rs | 2 +- crates/matrix-sdk-base/src/rooms/normal.rs | 10 ++++++++ crates/matrix-sdk/src/lib.rs | 2 +- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/room.rs b/bindings/matrix-sdk-ffi/src/room.rs index bf90d8f0fa2..2f4052b4bb7 100644 --- a/bindings/matrix-sdk-ffi/src/room.rs +++ b/bindings/matrix-sdk-ffi/src/room.rs @@ -4,7 +4,7 @@ use anyhow::{Context, Result}; use matrix_sdk::{ event_cache::paginator::PaginatorError, room::{power_levels::RoomPowerLevelChanges, Room as SdkRoom, RoomMemberRole}, - RoomMemberships, RoomState, + RoomHero as SdkRoomHero, RoomMemberships, RoomState, }; use matrix_sdk_ui::timeline::{PaginationError, RoomExt, TimelineFocus}; use mime::Mime; @@ -126,6 +126,11 @@ impl Room { self.inner.state().into() } + /// Returns the room heroes for this room. + pub fn heroes(&self) -> Vec { + self.inner.heroes().iter().map(Into::into).collect() + } + /// Is there a non expired membership with application "m.call" and scope /// "m.room" in this room. pub fn has_active_room_call(&self) -> bool { @@ -780,6 +785,27 @@ impl RoomMembersIterator { } } +/// Information about a member considered to be a room hero. +#[derive(uniffi::Record)] +pub struct RoomHero { + /// The user ID of the hero. + user_id: String, + /// The display name of the hero. + display_name: Option, + /// The avatar URL of the hero. + avatar_url: Option, +} + +impl From<&SdkRoomHero> for RoomHero { + fn from(value: &SdkRoomHero) -> Self { + Self { + user_id: value.user_id.to_string(), + display_name: value.display_name.clone(), + avatar_url: value.avatar_url.as_ref().map(ToString::to_string), + } + } +} + /// An update for a particular user's power level within the room. #[derive(uniffi::Record)] pub struct UserPowerLevelUpdate { diff --git a/crates/matrix-sdk-base/src/lib.rs b/crates/matrix-sdk-base/src/lib.rs index e382c78234d..1af76af8d3e 100644 --- a/crates/matrix-sdk-base/src/lib.rs +++ b/crates/matrix-sdk-base/src/lib.rs @@ -52,8 +52,8 @@ pub use http; pub use matrix_sdk_crypto as crypto; pub use once_cell; pub use rooms::{ - DisplayName, Room, RoomCreateWithCreatorEventContent, RoomInfo, RoomInfoUpdate, RoomMember, - RoomMemberships, RoomState, RoomStateFilter, + DisplayName, Room, RoomCreateWithCreatorEventContent, RoomHero, RoomInfo, RoomInfoUpdate, + RoomMember, RoomMemberships, RoomState, RoomStateFilter, }; pub use store::{StateChanges, StateStore, StateStoreDataKey, StateStoreDataValue, StoreError}; pub use utils::{ diff --git a/crates/matrix-sdk-base/src/rooms/mod.rs b/crates/matrix-sdk-base/src/rooms/mod.rs index 48ef48c12b5..83e68b1ba6c 100644 --- a/crates/matrix-sdk-base/src/rooms/mod.rs +++ b/crates/matrix-sdk-base/src/rooms/mod.rs @@ -11,7 +11,7 @@ use std::{ use bitflags::bitflags; pub use members::RoomMember; -pub use normal::{Room, RoomInfo, RoomInfoUpdate, RoomState, RoomStateFilter}; +pub use normal::{Room, RoomHero, RoomInfo, RoomInfoUpdate, RoomState, RoomStateFilter}; use ruma::{ assign, events::{ diff --git a/crates/matrix-sdk-base/src/rooms/normal.rs b/crates/matrix-sdk-base/src/rooms/normal.rs index e728125c61f..e4472087433 100644 --- a/crates/matrix-sdk-base/src/rooms/normal.rs +++ b/crates/matrix-sdk-base/src/rooms/normal.rs @@ -589,6 +589,11 @@ impl Room { Ok(members) } + /// Get the heroes for this room. + pub fn heroes(&self) -> Vec { + self.inner.read().heroes().to_owned() + } + /// Get the list of `RoomMember`s that are considered to be joined members /// of this room. #[deprecated = "Use members with RoomMemberships::JOIN instead"] @@ -1166,6 +1171,11 @@ impl RoomInfo { self.summary.room_heroes = heroes; } + /// The heroes for this room. + pub fn heroes(&self) -> &[RoomHero] { + self.summary.room_heroes.deref() + } + /// The number of active members (invited + joined) in the room. /// /// The return value is saturated at `u64::MAX`. diff --git a/crates/matrix-sdk/src/lib.rs b/crates/matrix-sdk/src/lib.rs index 4ebf762997c..8e8a26ec728 100644 --- a/crates/matrix-sdk/src/lib.rs +++ b/crates/matrix-sdk/src/lib.rs @@ -23,7 +23,7 @@ pub use matrix_sdk_base::crypto; pub use matrix_sdk_base::{ deserialized_responses, store::{DynStateStore, MemoryStore, StateStoreExt}, - DisplayName, Room as BaseRoom, RoomCreateWithCreatorEventContent, RoomInfo, + DisplayName, Room as BaseRoom, RoomCreateWithCreatorEventContent, RoomHero, RoomInfo, RoomMember as BaseRoomMember, RoomMemberships, RoomState, SessionMeta, StateChanges, StateStore, StoreError, };