use ruma::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
#[cfg(doc)]
use crate::media::store::MediaStore;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[non_exhaustive]
pub struct MediaRetentionPolicy {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_cache_size: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_file_size: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_access_expiry: Option<Duration>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cleanup_frequency: Option<Duration>,
}
impl MediaRetentionPolicy {
pub fn new() -> Self {
Self::default()
}
pub fn empty() -> Self {
Self {
max_cache_size: None,
max_file_size: None,
last_access_expiry: None,
cleanup_frequency: None,
}
}
pub fn with_max_cache_size(mut self, size: Option<u64>) -> Self {
self.max_cache_size = size;
self
}
pub fn with_max_file_size(mut self, size: Option<u64>) -> Self {
self.max_file_size = size;
self
}
pub fn with_last_access_expiry(mut self, duration: Option<Duration>) -> Self {
self.last_access_expiry = duration;
self
}
pub fn with_cleanup_frequency(mut self, duration: Option<Duration>) -> Self {
self.cleanup_frequency = duration;
self
}
pub fn has_limitations(&self) -> bool {
self.max_cache_size.is_some()
|| self.max_file_size.is_some()
|| self.last_access_expiry.is_some()
}
pub fn exceeds_max_cache_size(&self, size: u64) -> bool {
self.max_cache_size.is_some_and(|max_size| size > max_size)
}
pub fn computed_max_file_size(&self) -> Option<u64> {
match (self.max_cache_size, self.max_file_size) {
(None, None) => None,
(None, Some(size)) => Some(size),
(Some(size), None) => Some(size),
(Some(max_cache_size), Some(max_file_size)) => Some(max_cache_size.min(max_file_size)),
}
}
pub fn exceeds_max_file_size(&self, size: u64) -> bool {
self.computed_max_file_size().is_some_and(|max_size| size > max_size)
}
pub fn has_content_expired(
&self,
current_time: SystemTime,
last_access_time: SystemTime,
) -> bool {
self.last_access_expiry.is_some_and(|max_duration| {
current_time
.duration_since(last_access_time)
.is_ok_and(|elapsed| elapsed >= max_duration)
})
}
pub fn should_clean_up(&self, current_time: SystemTime, last_cleanup_time: SystemTime) -> bool {
self.cleanup_frequency.is_some_and(|max_duration| {
current_time
.duration_since(last_cleanup_time)
.is_ok_and(|elapsed| elapsed >= max_duration)
})
}
}
impl Default for MediaRetentionPolicy {
fn default() -> Self {
Self {
max_cache_size: Some(400 * 1024 * 1024),
max_file_size: Some(20 * 1024 * 1024),
last_access_expiry: Some(Duration::from_secs(60 * 24 * 60 * 60)),
cleanup_frequency: Some(Duration::from_secs(24 * 60 * 60)),
}
}
}
#[cfg(test)]
mod tests {
use ruma::time::{Duration, SystemTime};
use super::MediaRetentionPolicy;
#[test]
fn test_media_retention_policy_has_limitations() {
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.has_limitations());
policy = policy.with_last_access_expiry(Some(Duration::from_secs(60)));
assert!(policy.has_limitations());
policy = policy.with_last_access_expiry(None);
assert!(!policy.has_limitations());
policy = policy.with_max_cache_size(Some(1_024));
assert!(policy.has_limitations());
policy = policy.with_max_cache_size(None);
assert!(!policy.has_limitations());
policy = policy.with_max_file_size(Some(1_024));
assert!(policy.has_limitations());
policy = policy.with_max_file_size(None);
assert!(!policy.has_limitations());
assert!(MediaRetentionPolicy::new().has_limitations());
}
#[test]
fn test_media_retention_policy_max_cache_size() {
let file_size = 2_048;
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), None);
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(4_096));
assert!(!policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), Some(4_096));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(2_048));
assert!(!policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(1_024));
assert!(policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
}
#[test]
fn test_media_retention_policy_max_file_size() {
let file_size = 2_048;
let mut policy = MediaRetentionPolicy::empty();
assert_eq!(policy.computed_max_file_size(), None);
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(4_096));
assert_eq!(policy.computed_max_file_size(), Some(4_096));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(2_048));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(1_024));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(2_048));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(2_048));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(4_096));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(1_024));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
}
#[test]
fn test_media_retention_policy_has_content_expired() {
let epoch = SystemTime::UNIX_EPOCH;
let last_access_time = epoch + Duration::from_secs(30);
let epoch_plus_60 = epoch + Duration::from_secs(60);
let epoch_plus_120 = epoch + Duration::from_secs(120);
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(120)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(60)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(30)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(0)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(policy.has_content_expired(last_access_time, last_access_time));
assert!(policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(policy.has_content_expired(epoch_plus_120, last_access_time));
}
#[test]
fn test_media_retention_policy_cleanup_frequency() {
let epoch = SystemTime::UNIX_EPOCH;
let epoch_plus_60 = epoch + Duration::from_secs(60);
let epoch_plus_120 = epoch + Duration::from_secs(120);
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.should_clean_up(epoch_plus_60, epoch));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_60));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_120));
policy = policy.with_cleanup_frequency(Some(Duration::from_secs(0)));
assert!(policy.should_clean_up(epoch_plus_60, epoch));
assert!(policy.should_clean_up(epoch_plus_60, epoch_plus_60));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_120));
policy = policy.with_cleanup_frequency(Some(Duration::from_secs(30)));
assert!(policy.should_clean_up(epoch_plus_60, epoch));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_60));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_120));
policy = policy.with_cleanup_frequency(Some(Duration::from_secs(60)));
assert!(policy.should_clean_up(epoch_plus_60, epoch));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_60));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_120));
policy = policy.with_cleanup_frequency(Some(Duration::from_secs(90)));
assert!(!policy.should_clean_up(epoch_plus_60, epoch));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_60));
assert!(!policy.should_clean_up(epoch_plus_60, epoch_plus_120));
}
}