matrix-sdk-base: Add method to get all members from the store

This commit is contained in:
Julian Sparber 2021-03-12 12:20:18 +01:00
parent 2d6502247b
commit a4bac499e9
4 changed files with 42 additions and 0 deletions

View file

@ -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<Vec<RoomMember>> {
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<Vec<RoomMember>> {

View file

@ -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<UserId> {
#[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<UserId> {
#[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<Vec<UserId>> {
Ok(self.get_user_ids(room_id))
}
async fn get_invited_user_ids(&self, room_id: &RoomId) -> Result<Vec<UserId>> {
Ok(self.get_invited_user_ids(room_id))
}

View file

@ -153,6 +153,9 @@ pub trait StateStore: AsyncTraitDeps {
state_key: &UserId,
) -> Result<Option<MemberEvent>>;
/// Get all the user ids of members for a given room.
async fn get_user_ids(&self, room_id: &RoomId) -> Result<Vec<UserId>>;
/// 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<Vec<UserId>>;

View file

@ -527,6 +527,13 @@ impl SledStore {
.transpose()?)
}
pub async fn get_user_ids(&self, room_id: &RoomId) -> impl Stream<Item = Result<UserId>> {
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<Vec<UserId>> {
self.get_user_ids(room_id).await.try_collect().await
}
async fn get_invited_user_ids(&self, room_id: &RoomId) -> Result<Vec<UserId>> {
self.get_invited_user_ids(room_id).await.try_collect().await
}