Skip to content

Commit ff37e93

Browse files
committed
feat(sdk): Add methods to Media to interact with MediaRetentionPolicy
Signed-off-by: Kévin Commaille <[email protected]>
1 parent 0d06313 commit ff37e93

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

crates/matrix-sdk/src/media.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::{fmt, fs::File, path::Path};
2424
use eyeball::SharedObservable;
2525
use futures_util::future::try_join;
2626
use matrix_sdk_base::event_cache::store::media::IgnoreMediaRetentionPolicy;
27-
pub use matrix_sdk_base::media::*;
27+
pub use matrix_sdk_base::{event_cache::store::media::MediaRetentionPolicy, media::*};
2828
use mime::Mime;
2929
use ruma::{
3030
api::{
@@ -672,6 +672,44 @@ impl Media {
672672
Ok(())
673673
}
674674

675+
/// Set the [`MediaRetentionPolicy`] to use for deciding whether to store or
676+
/// keep media content.
677+
///
678+
/// It is used:
679+
///
680+
/// * When a media needs to be cached, to check that it does not exceed the
681+
/// max file size.
682+
///
683+
/// * When [`Media::clean_up_media_cache()`], to check that all media
684+
/// content in the store fits those criteria.
685+
///
686+
/// To apply the new policy to the media cache right away,
687+
/// [`Media::clean_up_media_cache()`] should be called after this.
688+
///
689+
/// By default, an empty `MediaRetentionPolicy` is used, which means that no
690+
/// criteria are applied.
691+
///
692+
/// # Arguments
693+
///
694+
/// * `policy` - The `MediaRetentionPolicy` to use.
695+
pub async fn set_media_retention_policy(&self, policy: MediaRetentionPolicy) -> Result<()> {
696+
self.client.event_cache_store().lock().await?.set_media_retention_policy(policy).await?;
697+
Ok(())
698+
}
699+
700+
/// Get the current `MediaRetentionPolicy`.
701+
pub async fn media_retention_policy(&self) -> Result<MediaRetentionPolicy> {
702+
Ok(self.client.event_cache_store().lock().await?.media_retention_policy())
703+
}
704+
705+
/// Clean up the media cache with the current [`MediaRetentionPolicy`].
706+
///
707+
/// If there is already an ongoing cleanup, this is a noop.
708+
pub async fn clean_up_media_cache(&self) -> Result<()> {
709+
self.client.event_cache_store().lock().await?.clean_up_media_cache().await?;
710+
Ok(())
711+
}
712+
675713
/// Upload the file bytes in `data` and return the source information.
676714
pub(crate) async fn upload_plain_media_and_thumbnail(
677715
&self,

0 commit comments

Comments
 (0)