From 4e50bcddc2675895c13815e75956df23dcb581e4 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 7 Sep 2023 11:03:16 +0200 Subject: [PATCH] feat(ffi) `Room::members` returns a `RoomMembersIterator`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch updates `Room::members` to return `Result, ClientError>`. This `RoomMembersIterator` type is new, and is implemented in this patch too. The idea behind this patch is to allow the bindings to “paginate” over the list of members for a particular room, in case the room has 17k members for example. --- bindings/matrix-sdk-ffi/src/room.rs | 35 ++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/room.rs b/bindings/matrix-sdk-ffi/src/room.rs index f905b556b..f1f36d7d9 100644 --- a/bindings/matrix-sdk-ffi/src/room.rs +++ b/bindings/matrix-sdk-ffi/src/room.rs @@ -42,6 +42,7 @@ use uuid::Uuid; use super::RUNTIME; use crate::{ + chunk_iterator::ChunkIterator, client::ProgressWatcher, error::{ClientError, RoomError}, room_info::RoomInfo, @@ -197,14 +198,8 @@ impl Room { }) } - pub async fn members(&self) -> Result>, ClientError> { - Ok(self - .inner - .members(RoomMemberships::empty()) - .await? - .iter() - .map(|m| Arc::new(RoomMember::new(m.clone()))) - .collect()) + pub async fn members(&self) -> Result, ClientError> { + Ok(Arc::new(RoomMembersIterator::new(self.inner.members(RoomMemberships::empty()).await?))) } pub async fn member(&self, user_id: String) -> Result, ClientError> { @@ -1142,3 +1137,27 @@ impl From for RumaAssetType { } } } + +#[derive(uniffi::Object)] +pub struct RoomMembersIterator { + chunk_iterator: ChunkIterator, +} + +impl RoomMembersIterator { + fn new(members: Vec) -> Self { + Self { chunk_iterator: ChunkIterator::new(members) } + } +} + +#[uniffi::export] +impl RoomMembersIterator { + fn len(&self) -> u32 { + self.chunk_iterator.len() + } + + fn next_chunk(&self, chunk_size: u32) -> Option>> { + self.chunk_iterator + .next(chunk_size) + .map(|members| members.into_iter().map(RoomMember::new).map(Arc::new).collect()) + } +}