Skip to content

Commit

Permalink
feat(sdk): Add methods to Media to interact with MediaRetentionPolicy
Browse files Browse the repository at this point in the history
Signed-off-by: Kévin Commaille <[email protected]>
  • Loading branch information
zecakeh committed Jan 22, 2025
1 parent 0d06313 commit 966a3e9
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion crates/matrix-sdk/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{fmt, fs::File, path::Path};
use eyeball::SharedObservable;
use futures_util::future::try_join;
use matrix_sdk_base::event_cache::store::media::IgnoreMediaRetentionPolicy;
pub use matrix_sdk_base::media::*;
pub use matrix_sdk_base::{event_cache::store::media::MediaRetentionPolicy, media::*};
use mime::Mime;
use ruma::{
api::{
Expand Down Expand Up @@ -672,6 +672,44 @@ impl Media {
Ok(())
}

/// Set the [`MediaRetentionPolicy`] to use for deciding whether to store or
/// keep media content.
///
/// It is used:
///
/// * When a media needs to be cached, to check that it does not exceed the
/// max file size.
///
/// * When [`Media::clean_up_media_cache()`], to check that all media
/// content in the store fits those criterias.

Check warning on line 684 in crates/matrix-sdk/src/media.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"criterias" should be "criteria".
///
/// To apply the new policy to the media cache right away,
/// [`Media::clean_up_media_cache()`] should be called after this.
///
/// By default, an empty `MediaRetentionPolicy` is used, which means that no
/// criterias are applied.

Check warning on line 690 in crates/matrix-sdk/src/media.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"criterias" should be "criteria".
///
/// # Arguments
///
/// * `policy` - The `MediaRetentionPolicy` to use.
pub async fn set_media_retention_policy(&self, policy: MediaRetentionPolicy) -> Result<()> {
self.client.event_cache_store().lock().await?.set_media_retention_policy(policy).await?;
Ok(())
}

/// Get the current `MediaRetentionPolicy`.
pub async fn media_retention_policy(&self) -> Result<MediaRetentionPolicy> {
Ok(self.client.event_cache_store().lock().await?.media_retention_policy())
}

/// Clean up the media cache with the current [`MediaRetentionPolicy`].
///
/// If there is already an ongoing cleanup, this is a noop.
pub async fn clean_up_media_cache(&self) -> Result<()> {
self.client.event_cache_store().lock().await?.clean_up_media_cache().await?;
Ok(())
}

/// Upload the file bytes in `data` and return the source information.
pub(crate) async fn upload_plain_media_and_thumbnail(
&self,
Expand Down

0 comments on commit 966a3e9

Please sign in to comment.