From d48abdc3ad0b65d0a2786efa0f9a07eefd9e6985 Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Fri, 10 Oct 2025 22:40:22 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(config):=20add=20retention=20p?= =?UTF-8?q?eriod=20to=20eol=20rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add retention functionality to EolRule - parse retention string to calculate EOL deadline - generate 'before:' query based on retention period --- src/config/eol_rule.rs | 50 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/config/eol_rule.rs b/src/config/eol_rule.rs index 72ea1d5..eff35a8 100644 --- a/src/config/eol_rule.rs +++ b/src/config/eol_rule.rs @@ -1,8 +1,9 @@ use std::{collections::BTreeSet, fmt}; +use chrono::{Datelike, Local, TimeDelta, TimeZone}; use serde::{Deserialize, Serialize}; -use crate::{Retention, eol_action::EolAction}; +use crate::{MessageAge, Retention, eol_action::EolAction}; /// End of life rules #[derive(Debug, Serialize, Deserialize, Default, Clone)] @@ -133,8 +134,51 @@ impl EolRule { ) } - pub(crate) fn eol_query(&self) -> String { - format!("before: {}", chrono::Local::now().format("%Y-%m-%d")) + pub(crate) fn eol_query(&self) -> Option { + let today = chrono::Local::now(); + let message_age = MessageAge::parse(&self.retention)?; + + let deadline = match message_age { + MessageAge::Days(c) => { + let delta = TimeDelta::days(c); + today.checked_sub_signed(delta).unwrap() + } + MessageAge::Weeks(c) => { + let delta = TimeDelta::weeks(c); + today.checked_sub_signed(delta).unwrap() + } + MessageAge::Months(c) => { + let day = today.day(); + let month = today.month(); + let year = today.year(); + let mut years = c as i32 / 12; + let months = c % 12; + let mut new_month = month - months as u32; + + if new_month < 1 { + years += 1; + new_month += 12; + } + + let new_year = year - years; + + Local + .with_ymd_and_hms(new_year, new_month, day, 0, 0, 0) + .unwrap() + } + MessageAge::Years(c) => { + let day = today.day(); + let month = today.month(); + let year = today.year(); + let new_year = year - c as i32; + + Local + .with_ymd_and_hms(new_year, month, day, 0, 0, 0) + .unwrap() + } + }; + + Some(format!("before: {}", deadline.format("%Y-%m-%d"))) } }