From 42b673e2a60491bcdec3bd80b2616c4e23df0e7b Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 29 Apr 2026 13:20:35 +0200 Subject: [PATCH] feat(sdk): Add `EventCache::thread`. This patch adds `EventCache::thread`, sibling of `EventCache::room`. Finally! --- .../matrix-sdk/src/event_cache/caches/mod.rs | 2 +- .../src/event_cache/caches/thread/mod.rs | 3 ++ crates/matrix-sdk/src/event_cache/mod.rs | 48 +++++++++++++------ 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/caches/mod.rs b/crates/matrix-sdk/src/event_cache/caches/mod.rs index ac4292449..9a938f4f9 100644 --- a/crates/matrix-sdk/src/event_cache/caches/mod.rs +++ b/crates/matrix-sdk/src/event_cache/caches/mod.rs @@ -131,7 +131,7 @@ impl Caches { /// Get the [`RoomEventCache`]. /// /// [`RoomEventCache`]: room::RoomEventCache - pub async fn room(&self) -> &room::RoomEventCache { + pub fn room(&self) -> &room::RoomEventCache { &self.room } diff --git a/crates/matrix-sdk/src/event_cache/caches/thread/mod.rs b/crates/matrix-sdk/src/event_cache/caches/thread/mod.rs index 51f00d16b..20d6f2d62 100644 --- a/crates/matrix-sdk/src/event_cache/caches/thread/mod.rs +++ b/crates/matrix-sdk/src/event_cache/caches/thread/mod.rs @@ -47,6 +47,9 @@ use super::{ use crate::room::WeakRoom; /// All the information related to a single thread. +/// +/// Cloning is shallow, and thus is cheap to do. +#[derive(Clone)] pub struct ThreadEventCache { inner: Arc, } diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index c55c9a9a0..9167a1f84 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -30,6 +30,7 @@ use std::{ collections::HashMap, fmt, + ops::Deref, sync::{Arc, OnceLock, RwLock as StdRwLock, RwLockReadGuard, RwLockWriteGuard}, }; @@ -42,7 +43,7 @@ use matrix_sdk_base::{ task_monitor::BackgroundTaskHandle, timer, }; -use ruma::{OwnedRoomId, RoomId}; +use ruma::{EventId, OwnedRoomId, RoomId}; use tokio::sync::{ Mutex, OwnedRwLockReadGuard, OwnedRwLockWriteGuard, RwLock, broadcast::{Receiver, Sender, channel}, @@ -64,21 +65,23 @@ mod persistence; mod redecryptor; mod tasks; -use caches::{Caches, room::RoomEventCacheLinkedChunkUpdate}; -pub use caches::{ - TimelineVectorDiffs, - event_focused::EventFocusThreadMode, - pagination::{BackPaginationOutcome, PaginationStatus}, - room::{ - RoomEventCache, RoomEventCacheGenericUpdate, RoomEventCacheSubscriber, - RoomEventCacheUpdate, pagination::RoomPagination, - }, - thread::pagination::ThreadPagination, -}; #[cfg(feature = "e2e-encryption")] pub use redecryptor::{DecryptionRetryRequest, RedecryptorReport}; -pub use crate::event_cache::automatic_pagination::AutomaticPagination; +use self::caches::{Caches, room::RoomEventCacheLinkedChunkUpdate}; +pub use self::{ + automatic_pagination::AutomaticPagination, + caches::{ + TimelineVectorDiffs, + event_focused::EventFocusThreadMode, + pagination::{BackPaginationOutcome, PaginationStatus}, + room::{ + RoomEventCache, RoomEventCacheGenericUpdate, RoomEventCacheSubscriber, + RoomEventCacheUpdate, pagination::RoomPagination, + }, + thread::{ThreadEventCache, pagination::ThreadPagination}, + }, +}; /// An error observed in the [`EventCache`]. #[derive(thiserror::Error, Clone, Debug)] @@ -365,9 +368,24 @@ impl EventCache { return Err(EventCacheError::NotSubscribedYet); }; - let room = self.inner.all_caches_for_room(room_id).await?.room.clone(); + let caches_for_room = self.inner.all_caches_for_room(room_id).await?; - Ok((room, drop_handles)) + Ok((caches_for_room.room().clone(), drop_handles)) + } + + /// Return a thread-specific view over the [`EventCache`]. + pub async fn thread( + &self, + room_id: &RoomId, + thread_id: &EventId, + ) -> Result<(ThreadEventCache, Arc)> { + let Some(drop_handles) = self.inner.drop_handles.get().cloned() else { + return Err(EventCacheError::NotSubscribedYet); + }; + + let caches_for_room = self.inner.all_caches_for_room(room_id).await?; + + Ok((caches_for_room.thread(thread_id.to_owned()).await?.deref().clone(), drop_handles)) } /// Cleanly clear all the rooms' event caches.