Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(search): ignore trash when performing email search (#689)

Will Hutchinson50fb2b66a581
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-3b49f494bd77e2c216db219d991337821164f685bfd1d1041df06a6aa2902fd4.json b/rust/cloud-storage/email_db_client/.sqlx/query-3b49f494bd77e2c216db219d991337821164f685bfd1d1041df06a6aa2902fd4.json
new file mode 100644
index 0000000000..a8661cf1ec
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-3b49f494bd77e2c216db219d991337821164f685bfd1d1041df06a6aa2902fd4.json
@@ -0,0 +1,77 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n t.id as thread_id,\n -- Return timestamps from the chosen messages directly\n COALESCE(earliest_msg.sent_at, earliest_msg.updated_at) as \"first_message_ts!\",\n COALESCE(latest_msg.sent_at, latest_msg.updated_at) as \"last_message_ts!\",\n -- Last time user viewed this thread\n uh.updated_at as \"viewed_at?\",\n latest_msg.snippet,\n earliest_msg.subject as \"subject?\",\n l.macro_id,\n latest_msg.sender as sender,\n latest_msg.pretty_sender as \"pretty_sender!\",\n latest_msg.trash_label as trash_label\n FROM email_threads t\n LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = $1\n LEFT JOIN email_links l ON l.id = t.link_id\n -- LATERAL join for LATEST message\n -- JOIN (Inner) acts as a filter to ensure we only return threads that actually have messages\n JOIN LATERAL (\n SELECT\n m2.snippet,\n m2.sent_at,\n m2.updated_at,\n c.email_address as sender,\n COALESCE(c.name, c.email_address) as pretty_sender,\n EXISTS(\n SELECT 1 \n FROM email_message_labels eml \n JOIN email_labels el ON el.id = eml.label_id \n WHERE eml.message_id = m2.id \n AND el.provider_label_id = 'TRASH'\n ) as trash_label\n FROM email_messages m2\n LEFT JOIN email_contacts c ON c.id = m2.from_contact_id\n WHERE m2.thread_id = t.id\n AND m2.link_id = $1\n ORDER BY\n (CASE WHEN m2.is_draft = false AND m2.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,\n COALESCE(m2.sent_at, m2.updated_at) DESC NULLS LAST\n LIMIT 1\n ) latest_msg ON true\n -- LATERAL join for EARLIEST message\n LEFT JOIN LATERAL (\n SELECT\n m3.subject,\n m3.sent_at,\n m3.updated_at\n FROM email_messages m3\n WHERE m3.thread_id = t.id\n AND m3.link_id = $1\n ORDER BY\n -- 1. Priority: Non-drafts with valid sent_at come first (0), everything else is fallback (1)\n (CASE WHEN m3.is_draft = false AND m3.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,\n -- 2. Sort by time (Oldest first)\n COALESCE(m3.sent_at, m3.updated_at) ASC NULLS LAST\n LIMIT 1\n ) earliest_msg ON true\n WHERE t.id = ANY($2)\n AND t.link_id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "thread_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "first_message_ts!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 2,
+ "name": "last_message_ts!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 3,
+ "name": "viewed_at?",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 4,
+ "name": "snippet",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "subject?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 6,
+ "name": "macro_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "sender",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 8,
+ "name": "pretty_sender!",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 9,
+ "name": "trash_label",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ null,
+ null,
+ false,
+ true,
+ true,
+ false,
+ false,
+ null,
+ null
+ ]
+ },
+ "hash": "3b49f494bd77e2c216db219d991337821164f685bfd1d1041df06a6aa2902fd4"
+}
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-b14a0b4df422aeacd17d6d4652f01696e684b2c10fbc4cfe92e07db8fb8ef1c4.json b/rust/cloud-storage/email_db_client/.sqlx/query-b14a0b4df422aeacd17d6d4652f01696e684b2c10fbc4cfe92e07db8fb8ef1c4.json
deleted file mode 100644
index 3d0f4988f9..0000000000
--- a/rust/cloud-storage/email_db_client/.sqlx/query-b14a0b4df422aeacd17d6d4652f01696e684b2c10fbc4cfe92e07db8fb8ef1c4.json
+++ /dev/null
@@ -1,71 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n t.id as thread_id,\n -- Return timestamps from the chosen messages directly\n COALESCE(earliest_msg.sent_at, earliest_msg.updated_at) as \"first_message_ts!\",\n COALESCE(latest_msg.sent_at, latest_msg.updated_at) as \"last_message_ts!\",\n -- Last time user viewed this thread\n uh.updated_at as \"viewed_at?\",\n latest_msg.snippet,\n earliest_msg.subject as \"subject?\",\n l.macro_id,\n latest_msg.sender as sender,\n latest_msg.pretty_sender as \"pretty_sender!\"\n FROM email_threads t\n LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = $1\n LEFT JOIN email_links l ON l.id = t.link_id\n -- LATERAL join for LATEST message\n -- JOIN (Inner) acts as a filter to ensure we only return threads that actually have messages\n JOIN LATERAL (\n SELECT\n m2.snippet,\n m2.sent_at,\n m2.updated_at,\n c.email_address as sender,\n COALESCE(c.name, c.email_address) as pretty_sender\n FROM email_messages m2\n LEFT JOIN email_contacts c ON c.id = m2.from_contact_id\n WHERE m2.thread_id = t.id\n AND m2.link_id = $1\n ORDER BY\n -- 1. Priority: Non-drafts with valid sent_at come first (0), everything else is fallback (1)\n (CASE WHEN m2.is_draft = false AND m2.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,\n -- 2. Sort by time (Newest first)\n COALESCE(m2.sent_at, m2.updated_at) DESC NULLS LAST\n LIMIT 1\n ) latest_msg ON true\n -- LATERAL join for EARLIEST message\n LEFT JOIN LATERAL (\n SELECT\n m3.subject,\n m3.sent_at,\n m3.updated_at\n FROM email_messages m3\n WHERE m3.thread_id = t.id\n AND m3.link_id = $1\n ORDER BY\n -- 1. Priority: Non-drafts with valid sent_at come first (0), everything else is fallback (1)\n (CASE WHEN m3.is_draft = false AND m3.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,\n -- 2. Sort by time (Oldest first)\n COALESCE(m3.sent_at, m3.updated_at) ASC NULLS LAST\n LIMIT 1\n ) earliest_msg ON true\n WHERE t.id = ANY($2)\n AND t.link_id = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "thread_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "first_message_ts!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 2,
- "name": "last_message_ts!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 3,
- "name": "viewed_at?",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 4,
- "name": "snippet",
- "type_info": "Text"
- },
- {
- "ordinal": 5,
- "name": "subject?",
- "type_info": "Text"
- },
- {
- "ordinal": 6,
- "name": "macro_id",
- "type_info": "Text"
- },
- {
- "ordinal": 7,
- "name": "sender",
- "type_info": "Varchar"
- },
- {
- "ordinal": 8,
- "name": "pretty_sender!",
- "type_info": "Varchar"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "UuidArray"
- ]
- },
- "nullable": [
- false,
- null,
- null,
- false,
- true,
- true,
- false,
- false,
- null
- ]
- },
- "hash": "b14a0b4df422aeacd17d6d4652f01696e684b2c10fbc4cfe92e07db8fb8ef1c4"
-}
diff --git a/rust/cloud-storage/email_db_client/fixtures/get_thread_summary_info.sql b/rust/cloud-storage/email_db_client/fixtures/get_thread_summary_info.sql
index 99a2918b11..e21e5d78d1 100644
--- a/rust/cloud-storage/email_db_client/fixtures/get_thread_summary_info.sql
+++ b/rust/cloud-storage/email_db_client/fixtures/get_thread_summary_info.sql
@@ -29,6 +29,19 @@ VALUES ('00000000-0000-0000-0000-0000000c0020',
'Bob Jones',
NOW(), NOW());
+------------------------------------------------------------
+-- Labels
+------------------------------------------------------------
+
+-- Create the TRASH label for this user
+INSERT INTO email_labels (id, link_id, provider_label_id, name, created_at, type)
+VALUES ('00000000-0000-0000-0000-0000000a0001',
+ '00000000-0000-0000-0000-00000000001c',
+ 'TRASH',
+ 'TRASH',
+ NOW(),
+ 'System');
+
------------------------------------------------------------
-- Thread 1: All non-draft messages with sent_at
-- Should use MIN(sent_at) and MAX(sent_at)
@@ -392,4 +405,71 @@ VALUES ('00000000-0000-0000-0000-0000000e0618',
'Reply message',
'Reply Subject',
'2025-01-16 11:00:00 +00:00',
- '2025-01-16 11:30:00 +00:00');
\ No newline at end of file
+ '2025-01-16 11:30:00 +00:00');
+
+------------------------------------------------------------
+-- Thread 8: Latest message has TRASH label
+-- Tests that threads with latest message in trash are handled correctly
+------------------------------------------------------------
+
+INSERT INTO email_threads (id, link_id, inbox_visible, is_read, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-000000000308',
+ '00000000-0000-0000-0000-00000000001c',
+ true, false, NOW(), NOW());
+
+-- First message (not in trash)
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ sent_at, is_draft, snippet, subject, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-0000000e0619',
+ '00000000-0000-0000-0000-000000000308',
+ '00000000-0000-0000-0000-00000000001c',
+ 'provider-msg-619',
+ FALSE,
+ '00000000-0000-0000-0000-0000000c0020',
+ '2025-01-17 10:00:00 +00:00',
+ '2025-01-17 10:00:00 +00:00',
+ false,
+ 'First message in thread',
+ 'Trash Test Subject',
+ '2025-01-17 09:00:00 +00:00',
+ '2025-01-17 09:30:00 +00:00');
+
+-- Middle message (not in trash)
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ sent_at, is_draft, snippet, subject, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-0000000e0620',
+ '00000000-0000-0000-0000-000000000308',
+ '00000000-0000-0000-0000-00000000001c',
+ 'provider-msg-620',
+ FALSE,
+ '00000000-0000-0000-0000-0000000c0021',
+ '2025-01-17 11:00:00 +00:00',
+ '2025-01-17 11:00:00 +00:00',
+ false,
+ 'Middle message in thread',
+ 'Trash Test Subject',
+ '2025-01-17 10:00:00 +00:00',
+ '2025-01-17 10:30:00 +00:00');
+
+-- Latest message (in trash)
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ sent_at, is_draft, snippet, subject, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-0000000e0621',
+ '00000000-0000-0000-0000-000000000308',
+ '00000000-0000-0000-0000-00000000001c',
+ 'provider-msg-621',
+ FALSE,
+ '00000000-0000-0000-0000-0000000c0020',
+ '2025-01-17 12:00:00 +00:00',
+ '2025-01-17 12:00:00 +00:00',
+ false,
+ 'Latest message - in trash',
+ 'Trash Test Subject',
+ '2025-01-17 11:00:00 +00:00',
+ '2025-01-17 11:30:00 +00:00');
+
+-- Add TRASH label to the latest message
+INSERT INTO email_message_labels (message_id, label_id)
+VALUES ('00000000-0000-0000-0000-0000000e0621',
+ '00000000-0000-0000-0000-0000000a0001');
+
diff --git a/rust/cloud-storage/email_db_client/src/user_history/mod.rs b/rust/cloud-storage/email_db_client/src/user_history/mod.rs
index d9e8c12483..b9a8f168ce 100644
--- a/rust/cloud-storage/email_db_client/src/user_history/mod.rs
+++ b/rust/cloud-storage/email_db_client/src/user_history/mod.rs
@@ -44,6 +44,8 @@ pub async fn upsert_user_history(
/// The "earliest" and "latest" messages are determined by:
/// 1. Priority: Non-drafts with a valid `sent_at` take precedence over drafts (or messages without `sent_at`).
/// 2. Sort: Chronological order of `sent_at` (or `updated_at` fallback).
+///
+/// Note: This will exclude any threads where the latest message is marked as TRASH
#[tracing::instrument(skip(pool), err)]
pub async fn get_thread_summary_info(
pool: &PgPool,
@@ -67,7 +69,8 @@ pub async fn get_thread_summary_info(
earliest_msg.subject as "subject?",
l.macro_id,
latest_msg.sender as sender,
- latest_msg.pretty_sender as "pretty_sender!"
+ latest_msg.pretty_sender as "pretty_sender!",
+ latest_msg.trash_label as trash_label
FROM email_threads t
LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = $1
LEFT JOIN email_links l ON l.id = t.link_id
@@ -79,15 +82,20 @@ pub async fn get_thread_summary_info(
m2.sent_at,
m2.updated_at,
c.email_address as sender,
- COALESCE(c.name, c.email_address) as pretty_sender
+ COALESCE(c.name, c.email_address) as pretty_sender,
+ EXISTS(
+ SELECT 1
+ FROM email_message_labels eml
+ JOIN email_labels el ON el.id = eml.label_id
+ WHERE eml.message_id = m2.id
+ AND el.provider_label_id = 'TRASH'
+ ) as trash_label
FROM email_messages m2
LEFT JOIN email_contacts c ON c.id = m2.from_contact_id
WHERE m2.thread_id = t.id
AND m2.link_id = $1
ORDER BY
- -- 1. Priority: Non-drafts with valid sent_at come first (0), everything else is fallback (1)
(CASE WHEN m2.is_draft = false AND m2.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,
- -- 2. Sort by time (Newest first)
COALESCE(m2.sent_at, m2.updated_at) DESC NULLS LAST
LIMIT 1
) latest_msg ON true
@@ -120,6 +128,11 @@ pub async fn get_thread_summary_info(
let mut result = HashMap::new();
for row in rows {
+ // If the latest message of the thread is in the trash, do not insert ThreadHistoryInfo.
+ // We don't want to include threads that are most likely in trash in the search results.
+ if row.trash_label.unwrap_or_default() {
+ continue;
+ }
let summary_info = ThreadHistoryInfo {
item_id: row.thread_id,
user_id: row.macro_id,
diff --git a/rust/cloud-storage/email_db_client/src/user_history/test.rs b/rust/cloud-storage/email_db_client/src/user_history/test.rs
index f0d1ad85ef..9136d7404c 100644
--- a/rust/cloud-storage/email_db_client/src/user_history/test.rs
+++ b/rust/cloud-storage/email_db_client/src/user_history/test.rs
@@ -254,3 +254,20 @@ async fn get_thread_summary_info_subject_is_from_earliest_message(
Ok(())
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../fixtures", scripts("get_thread_summary_info"))
+)]
+async fn get_thread_summary_info_filter_out_trash(pool: Pool<Postgres>) -> Result<()> {
+ const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS;
+
+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-00000000001c")?;
+ let thread_id = Uuid::parse_str("00000000-0000-0000-0000-000000000308")?;
+
+ let result = get_thread_summary_info(&pool, link_id, &[thread_id]).await?;
+
+ assert!(result.is_empty());
+
+ Ok(())
+}
diff --git a/rust/cloud-storage/opensearch_client/src/search/emails.rs b/rust/cloud-storage/opensearch_client/src/search/emails.rs
index 008fdf697b..47d30c30c6 100644
--- a/rust/cloud-storage/opensearch_client/src/search/emails.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/emails.rs
@@ -119,6 +119,9 @@ impl EmailQueryBuilder {
.content_bool_query
.ok_or(OpensearchClientError::BoolQueryNotBuilt)?;
+ // We don't want to include trash items in your email search
+ bool_query.must_not(QueryType::term("labels", "TRASH"));
+
// If link_ids are provided, add them to the query
if !self.link_ids.is_empty() {
bool_query.must(QueryType::terms("link_id", self.link_ids.clone()));
diff --git a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
index e6d8cf5a06..ccd5ed9fcf 100644
--- a/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/emails/test.rs
@@ -151,6 +151,13 @@ fn test_build_search_request() -> anyhow::Result<()> {
}
}
],
+ "must_not": [
+ {
+ "term": {
+ "labels": "TRASH",
+ }
+ }
+ ],
"should": [
{
"terms": {
diff --git a/rust/cloud-storage/opensearch_client/src/search/unified/test.rs b/rust/cloud-storage/opensearch_client/src/search/unified/test.rs
index 36a17fd6c9..94c0c3ba76 100644
--- a/rust/cloud-storage/opensearch_client/src/search/unified/test.rs
+++ b/rust/cloud-storage/opensearch_client/src/search/unified/test.rs
@@ -706,6 +706,13 @@ fn test_build_unified_search_request_content() -> anyhow::Result<()> {
}
}
],
+ "must_not": [
+ {
+ "term": {
+ "labels": "TRASH",
+ }
+ },
+ ],
"should": [
{
"terms": {
@@ -1474,6 +1481,13 @@ fn test_build_unified_search_request_name_content() -> anyhow::Result<()> {
}
}
],
+ "must_not": [
+ {
+ "term": {
+ "labels": "TRASH",
+ }
+ }
+ ],
"should": [
{
"terms": {