| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use std::time::{Duration, SystemTime}; |
| 14 |
|
| 15 |
use crate::error::ChatError; |
| 16 |
|
| 17 |
|
| 18 |
pub const MAX_AGE_CEILING: Duration = Duration::from_hours(720); |
| 19 |
|
| 20 |
|
| 21 |
pub const MAX_MESSAGES_CEILING: usize = 20_000; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 29 |
pub struct Retention { |
| 30 |
max_age: Duration, |
| 31 |
max_messages: usize, |
| 32 |
} |
| 33 |
|
| 34 |
impl Retention { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub fn new(max_age: Duration, max_messages: usize) -> Result<Self, ChatError> { |
| 45 |
if max_age.is_zero() || max_messages == 0 { |
| 46 |
return Err(ChatError::RetentionTooShort); |
| 47 |
} |
| 48 |
if max_age > MAX_AGE_CEILING || max_messages > MAX_MESSAGES_CEILING { |
| 49 |
return Err(ChatError::RetentionTooLong { |
| 50 |
max_age, |
| 51 |
max_messages, |
| 52 |
}); |
| 53 |
} |
| 54 |
Ok(Self { |
| 55 |
max_age, |
| 56 |
max_messages, |
| 57 |
}) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
pub fn forum_default() -> Self { |
| 63 |
Self { |
| 64 |
max_age: Duration::from_hours(168), |
| 65 |
max_messages: 5_000, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub fn stream_default() -> Self { |
| 72 |
Self { |
| 73 |
max_age: Duration::from_hours(24), |
| 74 |
max_messages: 5_000, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
pub fn max_age(&self) -> Duration { |
| 79 |
self.max_age |
| 80 |
} |
| 81 |
|
| 82 |
pub fn max_messages(&self) -> usize { |
| 83 |
self.max_messages |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub fn expires_at(&self, now: SystemTime) -> SystemTime { |
| 93 |
now + self.max_age |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
#[cfg(test)] |
| 98 |
mod tests { |
| 99 |
use super::*; |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn rejects_anything_above_the_ceilings() { |
| 103 |
assert!(matches!( |
| 104 |
Retention::new(MAX_AGE_CEILING + Duration::from_secs(1), 100), |
| 105 |
Err(ChatError::RetentionTooLong { .. }) |
| 106 |
)); |
| 107 |
assert!(matches!( |
| 108 |
Retention::new(Duration::from_mins(1), MAX_MESSAGES_CEILING + 1), |
| 109 |
Err(ChatError::RetentionTooLong { .. }) |
| 110 |
)); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn accepts_exactly_the_ceilings() { |
| 115 |
assert!(Retention::new(MAX_AGE_CEILING, MAX_MESSAGES_CEILING).is_ok()); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn rejects_zero_bounds() { |
| 120 |
assert!(matches!( |
| 121 |
Retention::new(Duration::ZERO, 100), |
| 122 |
Err(ChatError::RetentionTooShort) |
| 123 |
)); |
| 124 |
assert!(matches!( |
| 125 |
Retention::new(Duration::from_mins(1), 0), |
| 126 |
Err(ChatError::RetentionTooShort) |
| 127 |
)); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn defaults_are_within_the_ceilings() { |
| 132 |
for r in [Retention::forum_default(), Retention::stream_default()] { |
| 133 |
assert!(Retention::new(r.max_age(), r.max_messages()).is_ok()); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn expiry_is_insert_time_plus_max_age() { |
| 139 |
let r = Retention::forum_default(); |
| 140 |
let now = SystemTime::UNIX_EPOCH + Duration::from_secs(1_000_000); |
| 141 |
assert_eq!(r.expires_at(now), now + r.max_age()); |
| 142 |
} |
| 143 |
} |
| 144 |
|