diff --git a/bindings/matrix-sdk-ffi/CHANGELOG.md b/bindings/matrix-sdk-ffi/CHANGELOG.md index e9e0b9acff6..b5a74bcce07 100644 --- a/bindings/matrix-sdk-ffi/CHANGELOG.md +++ b/bindings/matrix-sdk-ffi/CHANGELOG.md @@ -8,14 +8,16 @@ All notable changes to this project will be documented in this file. ### Features +- Add `Room::list_threads` to list all the threads in a room. + ([#5953](https://github.com/matrix-org/matrix-rust-sdk/pull/5953)) - Add `SpaceService::get_space_room` to get a space given its id from the space graph if available. -[#5944](https://github.com/matrix-org/matrix-rust-sdk/pull/5944) + ([#5944](https://github.com/matrix-org/matrix-rust-sdk/pull/5944)) - Add `QrCodeData::to_bytes()` to allow generation of a QR code. ([#5939](https://github.com/matrix-org/matrix-rust-sdk/pull/5939)) - [**breaking**]: The new Latest Event API replaces the old API. `Room::new_latest_event` overwrites the `Room::latest_event` method. See the documentation of `matrix_sdk::latest_event` to learn about the new API. - [#5624](https://github.com/matrix-org/matrix-rust-sdk/pull/5624/) + ([#5624](https://github.com/matrix-org/matrix-rust-sdk/pull/5624/)) ## [0.16.0] - 2025-12-04 diff --git a/bindings/matrix-sdk-ffi/src/room/mod.rs b/bindings/matrix-sdk-ffi/src/room/mod.rs index 6487ee086cd..66a61c7fa4b 100644 --- a/bindings/matrix-sdk-ffi/src/room/mod.rs +++ b/bindings/matrix-sdk-ffi/src/room/mod.rs @@ -5,7 +5,8 @@ use futures_util::{pin_mut, StreamExt}; use matrix_sdk::{ encryption::LocalTrust, room::{ - edit::EditedContent, power_levels::RoomPowerLevelChanges, Room as SdkRoom, RoomMemberRole, + edit::EditedContent, power_levels::RoomPowerLevelChanges, + ListThreadsOptions as SdkListThreadsOptions, Room as SdkRoom, RoomMemberRole, TryFromReportedContentScoreError, }, send_queue::RoomSendQueueUpdate as SdkRoomSendQueueUpdate, @@ -21,6 +22,7 @@ use matrix_sdk_ui::{ }; use mime::Mime; use ruma::{ + api::client::threads::get_threads::v1::IncludeThreads as SdkIncludeThreads, assign, events::{ receipt::ReceiptThread, @@ -1228,6 +1230,41 @@ impl Room { .map(|sub| ThreadSubscription { automatic: sub.automatic })) } + /// Retrieve a list of all the threads for the current room. + /// + /// Since this client-server API is paginated, the return type may include a + /// token used to resuming back-pagination into the list of results, in + /// [`ThreadRoots::prev_batch_token`]. This token can be passed to the next + /// call to this function, through the `from` field of + /// [`ListThreadsOptions`]. + pub async fn list_threads(&self, opts: ListThreadsOptions) -> Result { + let inner_opts = SdkListThreadsOptions { + include_threads: match opts.include_threads { + IncludeThreads::All => SdkIncludeThreads::All, + IncludeThreads::Participated => SdkIncludeThreads::Participated, + }, + from: opts.from, + limit: opts.limit.and_then(ruma::UInt::new), + }; + + let roots = self.inner.list_threads(inner_opts).await?; + + Ok(ThreadRoots { + chunk: roots + .chunk + .into_iter() + .filter_map(|timeline_event| { + timeline_event + .raw() + .deserialize() + .ok() + .map(|any_timeline_event| TimelineEvent(Box::new(any_timeline_event))) + }) + .collect(), + prev_batch_token: roots.prev_batch_token, + }) + } + /// Either loads the event associated with the `event_id` from the event /// cache or fetches it from the homeserver. pub async fn load_or_fetch_event( @@ -1253,6 +1290,60 @@ pub struct ThreadSubscription { automatic: bool, } +/// Options for [Room::list_threads]. +#[derive(Debug, Clone, uniffi::Record)] +pub struct ListThreadsOptions { + /// An extra filter to select which threads should be returned. + pub include_threads: IncludeThreads, + + /// The token to start returning events from. + /// + /// This token can be obtained from a [`ThreadRoots::prev_batch_token`] + /// returned by a previous call to [`Room::list_threads()`]. + /// + /// If `from` isn't provided the homeserver shall return a list of thread + /// roots from end of the timeline history. + pub from: Option, + + /// The maximum number of events to return. + /// + /// Default: 10. + pub limit: Option, +} + +/// Which threads to include in the response. +#[derive(Debug, Clone, uniffi::Enum)] +pub enum IncludeThreads { + /// `all` + /// + /// Include all thread roots found in the room. + /// + /// This is the default. + All, + + /// `participated` + /// + /// Only include thread roots for threads where + /// [`current_user_participated`] is `true`. + /// + /// [`current_user_participated`]: https://spec.matrix.org/latest/client-server-api/#server-side-aggregation-of-mthread-relationships + Participated, +} + +/// The result of a [`Room::list_threads`] query. +/// +/// This is a wrapper around the Ruma equivalent, with events decrypted if needs +/// be. +#[derive(uniffi::Object)] +pub struct ThreadRoots { + /// The events that are thread roots in the current batch. + pub chunk: Vec, + + /// Token to paginate backwards in a subsequent query to + /// [`Room::list_threads`]. + pub prev_batch_token: Option, +} + /// A listener for receiving new live location shares in a room. #[matrix_sdk_ffi_macros::export(callback_interface)] pub trait LiveLocationShareListener: SyncOutsideWasm + SendOutsideWasm {