diff --git a/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs b/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs index 3568b3fe0..45efa9bc6 100644 --- a/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs +++ b/crates/matrix-sdk-base/src/event_cache/store/media/media_service.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::fmt; +use std::{fmt, sync::Arc}; use async_trait::async_trait; use matrix_sdk_common::{locks::Mutex, AsyncTraitDeps}; @@ -28,6 +28,11 @@ use crate::{event_cache::store::EventCacheStoreError, media::MediaRequestParamet /// [`EventCacheStore`]: crate::event_cache::store::EventCacheStore #[derive(Debug)] pub struct MediaService { + inner: Arc>, +} + +#[derive(Debug)] +struct MediaServiceInner { /// The time provider. time_provider: Time, @@ -61,11 +66,13 @@ where /// Construct a new `MediaService` with the given `TimeProvider` and an /// empty `MediaRetentionPolicy`. fn with_time_provider(time_provider: Time) -> Self { - Self { + let inner = MediaServiceInner { time_provider, policy: Mutex::new(MediaRetentionPolicy::empty()), cleanup_guard: AsyncMutex::new(()), - } + }; + + Self { inner: Arc::new(inner) } } /// Restore the previous state of the [`MediaRetentionPolicy`] from data @@ -78,10 +85,15 @@ where /// * `policy` - The `MediaRetentionPolicy` that was persisted in the store. pub fn restore(&self, policy: Option) { if let Some(policy) = policy { - *self.policy.lock() = policy; + *self.inner.policy.lock() = policy; } } + /// Get the current time from the inner [`TimeProvider`]. + fn now(&self) -> SystemTime { + self.inner.time_provider.now() + } + /// Set the `MediaRetentionPolicy` of this service. /// /// # Arguments @@ -96,14 +108,14 @@ where ) -> Result<(), Store::Error> { store.set_media_retention_policy_inner(policy).await?; - *self.policy.lock() = policy; + *self.inner.policy.lock() = policy; Ok(()) } /// Get the `MediaRetentionPolicy` of this service. pub fn media_retention_policy(&self) -> MediaRetentionPolicy { - *self.policy.lock() + *self.inner.policy.lock() } /// Add a media file's content in the media store. @@ -134,15 +146,7 @@ where return Ok(()); } - store - .add_media_content_inner( - request, - content, - self.time_provider.now(), - policy, - ignore_policy, - ) - .await + store.add_media_content_inner(request, content, self.now(), policy, ignore_policy).await } /// Set whether the current [`MediaRetentionPolicy`] should be ignored for @@ -179,7 +183,7 @@ where store: &Store, request: &MediaRequestParameters, ) -> Result>, Store::Error> { - store.get_media_content_inner(request, self.time_provider.now()).await + store.get_media_content_inner(request, self.now()).await } /// Get a media file's content associated to an `MxcUri` from the @@ -195,7 +199,7 @@ where store: &Store, uri: &MxcUri, ) -> Result>, Store::Error> { - store.get_media_content_for_uri_inner(uri, self.time_provider.now()).await + store.get_media_content_for_uri_inner(uri, self.now()).await } /// Clean up the media cache with the current `MediaRetentionPolicy`. @@ -209,7 +213,7 @@ where &self, store: &Store, ) -> Result<(), Store::Error> { - let Ok(_guard) = self.cleanup_guard.try_lock() else { + let Ok(_guard) = self.inner.cleanup_guard.try_lock() else { // There is another ongoing cleanup. return Ok(()); }; @@ -221,7 +225,16 @@ where return Ok(()); } - store.clean_up_media_cache_inner(policy, self.time_provider.now()).await + store.clean_up_media_cache_inner(policy, self.now()).await + } +} + +impl