Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(email): backend date range support for emails (#3198)
dev-rbafad79b996ee
diff --git a/rust/cloud-storage/email/fixtures/email_dynamic_query_date_range.sql b/rust/cloud-storage/email/fixtures/email_dynamic_query_date_range.sqlnew file mode 100644index 0000000000..52895cdb8a--- /dev/null+++ b/rust/cloud-storage/email/fixtures/email_dynamic_query_date_range.sql@@ -0,0 +1,27 @@+-- Fixture for testing date range filters on email threads+-- Updates threads with explicit created_at timestamps to allow date filtering tests+--+-- Timeline for threads (created_at):+-- Thread 1: 2024-01-15 10:00:00+00 (newest)+-- Thread 2: 2024-01-14 09:00:00+00+-- Thread 3: 2024-01-13 08:00:00+00+-- Thread 4: 2024-01-12 07:00:00+00+-- Thread 5: 2024-01-11 06:00:00+00+-- Thread 6: 2024-01-10 05:00:00+00+-- Thread 7: 2024-01-09 04:00:00+00+-- Thread 8: 2024-01-08 03:00:00+00+-- Thread 9: 2024-01-07 02:00:00+00+-- Thread 10: 2024-01-06 01:00:00+00+-- Thread 11: 2024-01-05 00:00:00+00 (oldest)++UPDATE email_threads SET created_at = '2024-01-15 10:00:00+00' WHERE id = '20000001-0000-0000-0000-000000000001';+UPDATE email_threads SET created_at = '2024-01-14 09:00:00+00' WHERE id = '20000002-0000-0000-0000-000000000002';+UPDATE email_threads SET created_at = '2024-01-13 08:00:00+00' WHERE id = '20000003-0000-0000-0000-000000000003';+UPDATE email_threads SET created_at = '2024-01-12 07:00:00+00' WHERE id = '20000004-0000-0000-0000-000000000004';+UPDATE email_threads SET created_at = '2024-01-11 06:00:00+00' WHERE id = '20000005-0000-0000-0000-000000000005';+UPDATE email_threads SET created_at = '2024-01-10 05:00:00+00' WHERE id = '20000006-0000-0000-0000-000000000006';+UPDATE email_threads SET created_at = '2024-01-09 04:00:00+00' WHERE id = '20000007-0000-0000-0000-000000000007';+UPDATE email_threads SET created_at = '2024-01-08 03:00:00+00' WHERE id = '20000008-0000-0000-0000-000000000008';+UPDATE email_threads SET created_at = '2024-01-07 02:00:00+00' WHERE id = '20000009-0000-0000-0000-000000000009';+UPDATE email_threads SET created_at = '2024-01-06 01:00:00+00' WHERE id = '20000010-0000-0000-0000-000000000010';+UPDATE email_threads SET created_at = '2024-01-05 00:00:00+00' WHERE id = '20000011-0000-0000-0000-000000000011';diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/filters.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/filters.rsindex 22749755f5..38bc4e16f1 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/filters.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/filters.rs@@ -2,15 +2,38 @@ use super::SqlFragment;use super::resolve::ResolvedFilters;use crate::domain::models::{PreviewView, PreviewViewStandardLabel};use filter_ast::Expr;+use item_filters::ast::date::DateLiteral;use item_filters::ast::email::{Email, EmailLiteral};use recursion::CollapsibleExt;+fn date_predicate(col: &str, lit: &DateLiteral) -> SqlFragment {+ let sql = match lit {+ DateLiteral::GreaterThan(dt) => {+ format!("{col} > '{}'::timestamptz", dt.to_rfc3339())+ }+ DateLiteral::LessThan(dt) => {+ format!("{col} < '{}'::timestamptz", dt.to_rfc3339())+ }+ DateLiteral::GreaterThanOrEqual(dt) => {+ format!("{col} >= '{}'::timestamptz", dt.to_rfc3339())+ }+ DateLiteral::LessThanOrEqual(dt) => {+ format!("{col} <= '{}'::timestamptz", dt.to_rfc3339())+ }+ };+ SqlFragment::raw(sql)+}+pub(super) fn has_thread_literals(ast: &Expr<EmailLiteral>) -> bool {ast.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) | filter_ast::ExprFrame::Or(a, b) => a || b,filter_ast::ExprFrame::Not(a) => a,filter_ast::ExprFrame::Literal(- EmailLiteral::ThreadId(_) | EmailLiteral::ProjectId(_) | EmailLiteral::CalendarOnly(_),+ EmailLiteral::ThreadId(_)+ | EmailLiteral::ProjectId(_)+ | EmailLiteral::CalendarOnly(_)+ | EmailLiteral::CreatedAt(_)+ | EmailLiteral::UpdatedAt(_),) => true,filter_ast::ExprFrame::Literal(EmailLiteral::Shared(_)) => false,filter_ast::ExprFrame::Literal(_) => false,@@ -25,7 +48,9 @@ pub(super) fn has_message_literals(ast: &Expr<EmailLiteral>) -> bool {EmailLiteral::ThreadId(_)| EmailLiteral::ProjectId(_)| EmailLiteral::Shared(_)- | EmailLiteral::CalendarOnly(_),+ | EmailLiteral::CalendarOnly(_)+ | EmailLiteral::CreatedAt(_)+ | EmailLiteral::UpdatedAt(_),) => false,filter_ast::ExprFrame::Literal(_) => true,})@@ -263,6 +288,8 @@ pub(super) fn build_message_email_filter(}filter_ast::ExprFrame::Literal(EmailLiteral::Shared(_)) => SqlFragment::raw("TRUE"),filter_ast::ExprFrame::Literal(EmailLiteral::CalendarOnly(_)) => SqlFragment::raw("TRUE"),+ filter_ast::ExprFrame::Literal(EmailLiteral::CreatedAt(_)) => SqlFragment::raw("TRUE"),+ filter_ast::ExprFrame::Literal(EmailLiteral::UpdatedAt(_)) => SqlFragment::raw("TRUE"),});fragment.with_and_prefix()@@ -569,7 +596,10 @@ pub(super) fn build_matching_threads_cte_body(}/// Builds thread-level SQL WHERE conditions. Message-level literals map to TRUE.-pub(super) fn build_thread_email_filter(ast: &Expr<EmailLiteral>) -> SqlFragment {+pub(super) fn build_thread_email_filter(+ ast: &Expr<EmailLiteral>,+ sort_ts_field: &str,+) -> SqlFragment {let fragment = ast.collapse_frames(|frame| match frame {filter_ast::ExprFrame::And(a, b) => SqlFragment::and(a, b),filter_ast::ExprFrame::Or(a, b) => SqlFragment::or(a, b),@@ -605,6 +635,14 @@ pub(super) fn build_thread_email_filter(ast: &Expr<EmailLiteral>) -> SqlFragmentSqlFragment::raw("TRUE")}+ filter_ast::ExprFrame::Literal(EmailLiteral::CreatedAt(ref lit)) => {+ date_predicate("t.created_at", lit)+ }++ filter_ast::ExprFrame::Literal(EmailLiteral::UpdatedAt(ref lit)) => {+ date_predicate(sort_ts_field, lit)+ }+filter_ast::ExprFrame::Literal(EmailLiteral::Sender(_)| EmailLiteral::Cc(_)diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rsindex 7e81ebc91d..31b5161d42 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs@@ -125,7 +125,7 @@ fn push_thread_candidate_select(}if has_thread_literals(email_filter) {- build_thread_email_filter(email_filter).push_into(builder);+ build_thread_email_filter(email_filter, sort_ts_field).push_into(builder);}// Push address (Sender/Cc/Bcc/Recipient) constraints into the candidatediff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rsindex d29566c71a..7aa2f931f9 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs@@ -406,11 +406,13 @@ fn test_build_query_orders_by_id_to_match_cursor_tiebreak() {assert!(!sql.contains("ORDER BY t.effective_ts DESC, t.updated_at DESC"));}+const DEFAULT_SORT_TS: &str = "t.updated_at";+#[test]fn test_build_thread_email_filter_single_thread_id() {let id = Uuid::new_v4();let expr = Expr::Literal(EmailLiteral::ThreadId(id));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(debug.contains("t.id = "));@@ -426,7 +428,7 @@ fn test_build_thread_email_filter_multiple_thread_ids() {Expr::Literal(EmailLiteral::ThreadId(id1)),Expr::Literal(EmailLiteral::ThreadId(id2)),);- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(result.has_bind_uuid(&id1));@@ -439,7 +441,7 @@ fn test_build_thread_email_filter_multiple_thread_ids() {#[test]fn test_build_thread_email_filter_maps_sender_to_true() {let expr = Expr::Literal(EmailLiteral::Sender(complete("[email protected]")));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(debug.contains("TRUE"));@@ -466,7 +468,7 @@ fn test_combined_thread_id_and_sender_splits_correctly() {Expr::Literal(EmailLiteral::Sender(complete("[email protected]"))),);- let thread_result = build_thread_email_filter(&expr);+ let thread_result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let thread_debug = thread_result.to_debug_sql();assert!(thread_result.has_bind_uuid(&id));assert!(!thread_debug.contains("from_contact_id"));@@ -518,7 +520,7 @@ fn test_has_both_literals_in_combined_ast() {#[test]fn test_build_thread_email_filter_single_project_id() {let expr = Expr::Literal(EmailLiteral::ProjectId("project-123".to_string()));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(debug.contains("t.project_id = "));@@ -532,7 +534,7 @@ fn test_build_thread_email_filter_multiple_project_ids() {Expr::Literal(EmailLiteral::ProjectId("project-1".to_string())),Expr::Literal(EmailLiteral::ProjectId("project-2".to_string())),);- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(debug.contains("OR"));@@ -572,7 +574,7 @@ fn test_combined_project_id_and_sender_splits_correctly() {Expr::Literal(EmailLiteral::Sender(complete("[email protected]"))),);- let thread_result = build_thread_email_filter(&expr);+ let thread_result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let thread_debug = thread_result.to_debug_sql();assert!(thread_debug.contains("t.project_id = "));assert!(thread_result.has_bind_string("project-123"));@@ -589,7 +591,7 @@ fn test_combined_project_id_and_sender_splits_correctly() {#[test]fn test_sql_injection_project_id_not_in_raw_sql() {let expr = Expr::Literal(EmailLiteral::ProjectId("'; DROP TABLE--".to_string()));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);assert!(result.has_bind_string("'; DROP TABLE--"));assert!(result.has_no_raw_containing("DROP"));@@ -635,7 +637,7 @@ fn test_sql_injection_user_label_not_in_raw_sql() {fn test_sql_injection_thread_id_not_in_raw_sql() {let id = Uuid::new_v4();let expr = Expr::Literal(EmailLiteral::ThreadId(id));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);assert!(result.has_bind_uuid(&id));assert!(result.has_no_raw_containing(&id.to_string()));@@ -842,7 +844,7 @@ fn test_full_query_emits_matching_threads_cte_and_in_reference() {#[test]fn test_build_thread_email_filter_calendar_only_true_emits_ics_exists() {let expr = Expr::Literal(EmailLiteral::CalendarOnly(true));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(debug.contains("EXISTS"));@@ -856,7 +858,7 @@ fn test_build_thread_email_filter_calendar_only_true_emits_ics_exists() {#[test]fn test_build_thread_email_filter_calendar_only_false_maps_to_true() {let expr = Expr::Literal(EmailLiteral::CalendarOnly(false));- let result = build_thread_email_filter(&expr);+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);let debug = result.to_debug_sql();assert!(debug.contains("TRUE"));@@ -884,3 +886,100 @@ fn test_has_message_literals_false_when_only_calendar_only() {let expr = Expr::Literal(EmailLiteral::CalendarOnly(true));assert!(!has_message_literals(&expr));}++#[test]+fn test_build_thread_email_filter_created_at_greater_than() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc.with_ymd_and_hms(2024, 1, 15, 12, 0, 0).unwrap();+ let expr = Expr::Literal(EmailLiteral::CreatedAt(DateLiteral::GreaterThan(dt)));+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);+ let debug = result.to_debug_sql();++ assert!(debug.contains("t.created_at >"));+ assert!(debug.contains("2024-01-15"));+}++#[test]+fn test_build_thread_email_filter_created_at_less_than_or_equal() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc+ .with_ymd_and_hms(2024, 6, 30, 23, 59, 59)+ .unwrap();+ let expr = Expr::Literal(EmailLiteral::CreatedAt(DateLiteral::LessThanOrEqual(dt)));+ let result = build_thread_email_filter(&expr, DEFAULT_SORT_TS);+ let debug = result.to_debug_sql();++ assert!(debug.contains("t.created_at <="));+ assert!(debug.contains("2024-06-30"));+}++#[test]+fn test_build_thread_email_filter_updated_at_uses_sort_ts_field() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc.with_ymd_and_hms(2024, 3, 1, 0, 0, 0).unwrap();+ let expr = Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::GreaterThanOrEqual(dt)));++ // Inbox view uses latest_inbound_message_ts+ let inbox_sort_ts = "t.latest_inbound_message_ts";+ let result = build_thread_email_filter(&expr, inbox_sort_ts);+ let debug = result.to_debug_sql();++ assert!(debug.contains("t.latest_inbound_message_ts >="));+ assert!(debug.contains("2024-03-01"));+}++#[test]+fn test_build_thread_email_filter_updated_at_with_different_sort_fields() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc.with_ymd_and_hms(2024, 5, 15, 0, 0, 0).unwrap();+ let expr = Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::LessThan(dt)));++ // Sent view uses latest_outbound_message_ts+ let sent_sort_ts = "t.latest_outbound_message_ts";+ let result = build_thread_email_filter(&expr, sent_sort_ts);+ let debug = result.to_debug_sql();++ assert!(debug.contains("t.latest_outbound_message_ts <"));+ assert!(!debug.contains("t.updated_at <"));+}++#[test]+fn test_has_thread_literals_true_when_created_at_present() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();+ let expr = Expr::Literal(EmailLiteral::CreatedAt(DateLiteral::GreaterThan(dt)));+ assert!(has_thread_literals(&expr));+}++#[test]+fn test_has_thread_literals_true_when_updated_at_present() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();+ let expr = Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::LessThan(dt)));+ assert!(has_thread_literals(&expr));+}++#[test]+fn test_has_message_literals_false_when_only_date_filters() {+ use chrono::TimeZone;+ use item_filters::ast::date::DateLiteral;++ let dt = chrono::Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();+ let expr = Expr::and(+ Expr::Literal(EmailLiteral::CreatedAt(DateLiteral::GreaterThan(dt))),+ Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::LessThan(dt))),+ );+ assert!(!has_message_literals(&expr));+}diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rsindex 38f6871c91..ed481fe693 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs@@ -1810,3 +1810,562 @@ async fn test_dynamic_query_and_filter_uses_single_message_semantics(Ok(())}++// ── Date range filter tests ─────────────────────────────────────────+//+// The email_dynamic_query_date_range fixture sets explicit created_at timestamps+// for threads 1-11, aligning with their latest_non_spam_message_ts. The timeline:+// Thread 1: 2024-01-15 10:00:00+00 (newest)+// Thread 2: 2024-01-14 09:00:00+00+// Thread 3: 2024-01-13 08:00:00+00+// Thread 4: 2024-01-12 07:00:00+00+// Thread 5: 2024-01-11 06:00:00+00+// Thread 6: 2024-01-10 05:00:00+00+// Thread 7: 2024-01-09 04:00:00+00+// Thread 8: 2024-01-08 03:00:00+00+// Thread 9: 2024-01-07 02:00:00+00+// Thread 10: 2024-01-06 01:00:00+00+// Thread 11: 2024-01-05 00:00:00+00 (oldest)++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_updated_at_greater_than(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads updated after 2024-01-12 (should match threads 1, 2, 3)+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 12, 12, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::UpdatedAt(+ DateLiteral::GreaterThan(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 (2024-01-15) should match"+ );+ assert!(+ result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 (2024-01-14) should match"+ );+ assert!(+ result_ids.contains("20000003-0000-0000-0000-000000000003"),+ "Thread 3 (2024-01-13) should match"+ );+ assert!(+ !result_ids.contains("20000004-0000-0000-0000-000000000004"),+ "Thread 4 (2024-01-12 07:00) should not match (before cutoff)"+ );+ assert!(+ !result_ids.contains("20000005-0000-0000-0000-000000000005"),+ "Thread 5 (2024-01-11) should not match"+ );++ assert_eq!(results.len(), 3, "Should return exactly 3 threads");++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_updated_at_less_than(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads updated before 2024-01-09 (should match thread 8)+ // Note: threads 9, 10, 11 are trashed, so they're excluded from "All" view+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 9, 0, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::UpdatedAt(+ DateLiteral::LessThan(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ // Thread 8 (2024-01-08 03:00) should match - it's before the cutoff+ assert!(+ result_ids.contains("20000008-0000-0000-0000-000000000008"),+ "Thread 8 (2024-01-08) should match"+ );++ // Threads after the cutoff should not match+ assert!(+ !result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 (2024-01-15) should not match"+ );+ assert!(+ !result_ids.contains("20000007-0000-0000-0000-000000000007"),+ "Thread 7 (2024-01-09) should not match (at or after cutoff)"+ );++ // Trashed threads should also not appear even if they match the date filter+ assert!(+ !result_ids.contains("20000009-0000-0000-0000-000000000009"),+ "Thread 9 (trashed) should not match"+ );+ assert!(+ !result_ids.contains("20000011-0000-0000-0000-000000000011"),+ "Thread 11 (trashed) should not match"+ );++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_updated_at_greater_than_or_equal(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads updated at or after exactly 2024-01-12 07:00:00 (thread 4's timestamp)+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 12, 7, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::UpdatedAt(+ DateLiteral::GreaterThanOrEqual(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 should match"+ );+ assert!(+ result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 should match"+ );+ assert!(+ result_ids.contains("20000003-0000-0000-0000-000000000003"),+ "Thread 3 should match"+ );+ assert!(+ result_ids.contains("20000004-0000-0000-0000-000000000004"),+ "Thread 4 (exact match) should match"+ );+ assert!(+ !result_ids.contains("20000005-0000-0000-0000-000000000005"),+ "Thread 5 (2024-01-11) should not match"+ );++ assert_eq!(results.len(), 4, "Should return exactly 4 threads");++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_updated_at_less_than_or_equal(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads updated at or before exactly 2024-01-09 04:00:00 (thread 7's timestamp)+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 9, 4, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::UpdatedAt(+ DateLiteral::LessThanOrEqual(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000007-0000-0000-0000-000000000007"),+ "Thread 7 (exact match) should match"+ );+ assert!(+ result_ids.contains("20000008-0000-0000-0000-000000000008"),+ "Thread 8 should match"+ );+ assert!(+ !result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 should not match"+ );+ assert!(+ !result_ids.contains("20000006-0000-0000-0000-000000000006"),+ "Thread 6 (2024-01-10) should not match"+ );++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_created_at_greater_than(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads created after 2024-01-13 (should match threads 1, 2)+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 13, 12, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::CreatedAt(+ DateLiteral::GreaterThan(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 (2024-01-15) should match"+ );+ assert!(+ result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 (2024-01-14) should match"+ );+ assert!(+ !result_ids.contains("20000003-0000-0000-0000-000000000003"),+ "Thread 3 (2024-01-13 08:00) should not match (before cutoff)"+ );++ assert_eq!(results.len(), 2, "Should return exactly 2 threads");++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_created_at_less_than(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads created before 2024-01-09 (should match thread 8)+ // Note: threads 9, 10, 11 are trashed, so they're excluded from "All" view+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 9, 0, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::CreatedAt(+ DateLiteral::LessThan(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ // Thread 8 (2024-01-08) should match+ assert!(+ result_ids.contains("20000008-0000-0000-0000-000000000008"),+ "Thread 8 (2024-01-08) should match"+ );++ // Newer threads should not match+ assert!(+ !result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 should not match"+ );+ assert!(+ !result_ids.contains("20000007-0000-0000-0000-000000000007"),+ "Thread 7 (2024-01-09) should not match (at or after cutoff)"+ );++ // Trashed threads should also not appear even if they match the date filter+ assert!(+ !result_ids.contains("20000009-0000-0000-0000-000000000009"),+ "Thread 9 (trashed) should not match"+ );+ assert!(+ !result_ids.contains("20000011-0000-0000-0000-000000000011"),+ "Thread 11 (trashed) should not match"+ );++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_date_range_combined(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads updated between 2024-01-10 and 2024-01-13 (inclusive)+ // Should match threads 3, 4, 5, 6+ let start = Utc.with_ymd_and_hms(2024, 1, 10, 0, 0, 0).unwrap();+ let end = Utc.with_ymd_and_hms(2024, 1, 14, 0, 0, 0).unwrap();+ let filter = Arc::new(Expr::and(+ Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::GreaterThanOrEqual(+ start,+ ))),+ Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::LessThan(end))),+ ));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000003-0000-0000-0000-000000000003"),+ "Thread 3 (2024-01-13) should match"+ );+ assert!(+ result_ids.contains("20000004-0000-0000-0000-000000000004"),+ "Thread 4 (2024-01-12) should match"+ );+ assert!(+ result_ids.contains("20000005-0000-0000-0000-000000000005"),+ "Thread 5 (2024-01-11) should match"+ );+ assert!(+ result_ids.contains("20000006-0000-0000-0000-000000000006"),+ "Thread 6 (2024-01-10) should match"+ );+ assert!(+ !result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 (2024-01-15) should not match (after end)"+ );+ assert!(+ !result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 (2024-01-14) should not match (at end, using LessThan)"+ );+ assert!(+ !result_ids.contains("20000007-0000-0000-0000-000000000007"),+ "Thread 7 (2024-01-09) should not match (before start)"+ );++ assert_eq!(results.len(), 4, "Should return exactly 4 threads");++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_date_with_sender_filter(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads from [email protected] AND updated after 2024-01-12+ // John's threads are 1, 2, 5; after 2024-01-12 are 1, 2, 3, 4+ // Intersection should be threads 1, 2+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 12, 12, 0, 0).unwrap();+ let filter = Arc::new(Expr::and(+ Expr::Literal(EmailLiteral::Sender(Email::Complete(+ EmailStr::parse_from_str("[email protected]")?.into_owned(),+ ))),+ Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::GreaterThan(cutoff))),+ ));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 (john, 2024-01-15) should match"+ );+ assert!(+ result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 (john, 2024-01-14) should match"+ );+ assert!(+ !result_ids.contains("20000005-0000-0000-0000-000000000005"),+ "Thread 5 (john, 2024-01-11) should not match (before cutoff)"+ );+ assert!(+ !result_ids.contains("20000003-0000-0000-0000-000000000003"),+ "Thread 3 (bob, 2024-01-13) should not match (wrong sender)"+ );++ assert_eq!(results.len(), 2, "Should return exactly 2 threads");++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_date_with_inbox_view(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Inbox);+ let limit = 50;++ // Filter for inbox threads updated after 2024-01-10+ // Inbox threads are 1, 4, 5, 7; after 2024-01-10 are 1, 2, 3, 4, 5, 6+ // Intersection should be threads 1, 4, 5+ let cutoff = Utc.with_ymd_and_hms(2024, 1, 10, 12, 0, 0).unwrap();+ let filter = Arc::new(Expr::Literal(EmailLiteral::UpdatedAt(+ DateLiteral::GreaterThan(cutoff),+ )));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 (inbox, 2024-01-15) should match"+ );+ assert!(+ result_ids.contains("20000004-0000-0000-0000-000000000004"),+ "Thread 4 (inbox, 2024-01-12) should match"+ );+ assert!(+ result_ids.contains("20000005-0000-0000-0000-000000000005"),+ "Thread 5 (inbox, 2024-01-11) should match"+ );+ assert!(+ !result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 (sent, not inbox) should not match"+ );+ assert!(+ !result_ids.contains("20000007-0000-0000-0000-000000000007"),+ "Thread 7 (inbox but 2024-01-09) should not match (before cutoff)"+ );++ assert_eq!(results.len(), 3, "Should return exactly 3 threads");++ Ok(())+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(+ path = "../../../../fixtures",+ scripts("email_dynamic_query", "email_dynamic_query_date_range")+ )+)]+async fn test_dynamic_query_created_at_and_updated_at_combined(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);+ let limit = 50;++ // Filter for threads created after 2024-01-09 AND updated before 2024-01-14+ // Created after 2024-01-09: threads 1, 2, 3, 4, 5, 6+ // Updated before 2024-01-14: threads 3, 4, 5, 6, 7, 8, 9, 10, 11+ // Intersection: threads 3, 4, 5, 6+ let created_cutoff = Utc.with_ymd_and_hms(2024, 1, 9, 12, 0, 0).unwrap();+ let updated_cutoff = Utc.with_ymd_and_hms(2024, 1, 14, 0, 0, 0).unwrap();+ let filter = Arc::new(Expr::and(+ Expr::Literal(EmailLiteral::CreatedAt(DateLiteral::GreaterThan(+ created_cutoff,+ ))),+ Expr::Literal(EmailLiteral::UpdatedAt(DateLiteral::LessThan(+ updated_cutoff,+ ))),+ ));+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);++ let results =+ dynamic::dynamic_email_thread_cursor(&pool, &link_id, limit, &view, query, "").await?;++ let result_ids: std::collections::HashSet<String> =+ results.iter().map(|r| r.id.to_string()).collect();++ assert!(+ result_ids.contains("20000003-0000-0000-0000-000000000003"),+ "Thread 3 should match"+ );+ assert!(+ result_ids.contains("20000004-0000-0000-0000-000000000004"),+ "Thread 4 should match"+ );+ assert!(+ result_ids.contains("20000005-0000-0000-0000-000000000005"),+ "Thread 5 should match"+ );+ assert!(+ result_ids.contains("20000006-0000-0000-0000-000000000006"),+ "Thread 6 should match"+ );+ assert!(+ !result_ids.contains("20000001-0000-0000-0000-000000000001"),+ "Thread 1 should not match (updated after cutoff)"+ );+ assert!(+ !result_ids.contains("20000002-0000-0000-0000-000000000002"),+ "Thread 2 should not match (updated at cutoff)"+ );+ assert!(+ !result_ids.contains("20000007-0000-0000-0000-000000000007"),+ "Thread 7 should not match (created before cutoff)"+ );++ assert_eq!(results.len(), 4, "Should return exactly 4 threads");++ Ok(())+}diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rsindex efcea2dd13..da85c9aa16 100644--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rs+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/mod.rs@@ -12,7 +12,9 @@ use std::sync::Arc;use super::*;use crate::domain::models::{LabelType, PreviewView, PreviewViewStandardLabel};use crate::domain::ports::EmailRepo;+use chrono::{TimeZone, Utc};use filter_ast::Expr;+use item_filters::ast::date::DateLiteral;use item_filters::ast::email::{Email, EmailLiteral};use macro_db_migrator::MACRO_DB_MIGRATIONS;use macro_user_id::email::EmailStr;diff --git a/rust/cloud-storage/item_filters/src/ast/email.rs b/rust/cloud-storage/item_filters/src/ast/email.rsindex f17e6dff2c..92f8968498 100644--- a/rust/cloud-storage/item_filters/src/ast/email.rs+++ b/rust/cloud-storage/item_filters/src/ast/email.rs@@ -3,7 +3,7 @@ use macro_user_id::{cowlike::CowLike, email::EmailStr};use serde::{Deserialize, Serialize};use uuid::Uuid;-use crate::{EmailFilters, SharedEmailFilter, ast::ExpandErr};+use crate::{EmailFilters, SharedEmailFilter, ast::ExpandErr, ast::date::DateLiteral};/// Possible email values in the ast#[derive(Debug, Clone, Serialize, Deserialize)]@@ -41,6 +41,12 @@ pub enum EmailLiteral {/// `.ics` calendar attachment (filename or `application/ics` mime type)./// When false, no constraint is applied.CalendarOnly(bool),+ /// Filter by thread created_at timestamp+ #[serde(rename = "ca")]+ CreatedAt(DateLiteral),+ /// Filter by thread updated_at timestamp (view-dependent field)+ #[serde(rename = "ua")]+ UpdatedAt(DateLiteral),}impl ExpandFrame<EmailLiteral> for EmailFilters {