diff --git a/matrix_sdk_base/src/rooms/normal.rs b/matrix_sdk_base/src/rooms/normal.rs index 96fa47fe..391b732d 100644 --- a/matrix_sdk_base/src/rooms/normal.rs +++ b/matrix_sdk_base/src/rooms/normal.rs @@ -261,6 +261,22 @@ impl Room { self.store.get_joined_user_ids(self.room_id()).await } + /// Get the all `RoomMember`s of this room that are known to the store. + pub async fn members(&self) -> StoreResult> { + let user_ids = self.store.get_user_ids(self.room_id()).await?; + let mut members = Vec::new(); + + for u in user_ids { + let m = self.get_member(&u).await?; + + if let Some(member) = m { + members.push(member); + } + } + + Ok(members) + } + /// Get the list of `RoomMember`s that are considered to be joined members /// of this room. pub async fn joined_members(&self) -> StoreResult> { diff --git a/matrix_sdk_base/src/store/memory_store.rs b/matrix_sdk_base/src/store/memory_store.rs index 014c20de..f2772ca8 100644 --- a/matrix_sdk_base/src/store/memory_store.rs +++ b/matrix_sdk_base/src/store/memory_store.rs @@ -270,6 +270,14 @@ impl MemoryStore { .and_then(|m| m.get(state_key).map(|m| m.clone()))) } + fn get_user_ids(&self, room_id: &RoomId) -> Vec { + #[allow(clippy::map_clone)] + self.members + .get(room_id) + .map(|u| u.iter().map(|u| u.key().clone()).collect()) + .unwrap_or_default() + } + fn get_invited_user_ids(&self, room_id: &RoomId) -> Vec { #[allow(clippy::map_clone)] self.invited_user_ids @@ -345,6 +353,10 @@ impl StateStore for MemoryStore { self.get_member_event(room_id, state_key).await } + async fn get_user_ids(&self, room_id: &RoomId) -> Result> { + Ok(self.get_user_ids(room_id)) + } + async fn get_invited_user_ids(&self, room_id: &RoomId) -> Result> { Ok(self.get_invited_user_ids(room_id)) } diff --git a/matrix_sdk_base/src/store/mod.rs b/matrix_sdk_base/src/store/mod.rs index 1f6e3d48..82d9b4d8 100644 --- a/matrix_sdk_base/src/store/mod.rs +++ b/matrix_sdk_base/src/store/mod.rs @@ -153,6 +153,9 @@ pub trait StateStore: AsyncTraitDeps { state_key: &UserId, ) -> Result>; + /// Get all the user ids of members for a given room. + async fn get_user_ids(&self, room_id: &RoomId) -> Result>; + /// Get all the user ids of members that are in the invited state for a /// given room. async fn get_invited_user_ids(&self, room_id: &RoomId) -> Result>; diff --git a/matrix_sdk_base/src/store/sled_store/mod.rs b/matrix_sdk_base/src/store/sled_store/mod.rs index 495b9477..ddec715c 100644 --- a/matrix_sdk_base/src/store/sled_store/mod.rs +++ b/matrix_sdk_base/src/store/sled_store/mod.rs @@ -527,6 +527,13 @@ impl SledStore { .transpose()?) } + pub async fn get_user_ids(&self, room_id: &RoomId) -> impl Stream> { + stream::iter(self.members.scan_prefix(room_id.encode()).map(|u| { + UserId::try_from(String::from_utf8_lossy(&u?.1).to_string()) + .map_err(StoreError::Identifier) + })) + } + pub async fn get_invited_user_ids( &self, room_id: &RoomId, @@ -632,6 +639,10 @@ impl StateStore for SledStore { self.get_member_event(room_id, state_key).await } + async fn get_user_ids(&self, room_id: &RoomId) -> Result> { + self.get_user_ids(room_id).await.try_collect().await + } + async fn get_invited_user_ids(&self, room_id: &RoomId) -> Result> { self.get_invited_user_ids(room_id).await.try_collect().await }