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.

fix(email-search): drop discarded drafts from email search results (#9283)

teo55af223a5195
diff --git a/.sqlx/query-d47de9234302e9f1e022748731c4bc4400c9aa390e0e020f8674c63861917e7e.json b/.sqlx/query-d47de9234302e9f1e022748731c4bc4400c9aa390e0e020f8674c63861917e7e.json
new file mode 100644
index 0000000000..9ac553fedc
--- /dev/null
+++ b/.sqlx/query-d47de9234302e9f1e022748731c4bc4400c9aa390e0e020f8674c63861917e7e.json
@@ -0,0 +1,107 @@
+{
+ "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 t.link_id,\n latest_msg.sender as sender,\n latest_msg.pretty_sender as \"pretty_sender!\",\n latest_msg.trash_label as trash_label,\n (\n SELECT m_latest.is_draft\n FROM email_messages m_latest\n WHERE m_latest.thread_id = t.id\n AND m_latest.link_id = t.link_id\n AND NOT (m_latest.is_draft AND COALESCE((m_latest.mailbox_state->>'provider_missing')::boolean, false))\n ORDER BY m_latest.internal_date_ts DESC NULLS LAST\n LIMIT 1\n ) AS \"is_draft!\",\n t.is_read,\n t.inbox_visible,\n -- Gmail's IMPORTANT label — a different notion from the\n -- Importance filter / email_threads.is_signal heuristic.\n (\n SELECT EXISTS (\n SELECT 1\n FROM email_messages m_imp\n JOIN email_message_labels ml ON m_imp.id = ml.message_id\n JOIN email_labels l ON ml.label_id = l.id\n WHERE m_imp.thread_id = t.id\n AND l.link_id = t.link_id\n AND l.name = 'IMPORTANT'\n )\n ) AS \"is_important!\"\n FROM email_threads t\n LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = t.link_id\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 = t.link_id\n AND NOT (m2.is_draft AND COALESCE((m2.mailbox_state->>'provider_missing')::boolean, false))\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 = t.link_id\n AND NOT (m3.is_draft AND COALESCE((m3.mailbox_state->>'provider_missing')::boolean, false))\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 = ANY($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": "link_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 8,
+ "name": "sender",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 9,
+ "name": "pretty_sender!",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 10,
+ "name": "trash_label",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 11,
+ "name": "is_draft!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "is_read",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 13,
+ "name": "inbox_visible",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 14,
+ "name": "is_important!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ null,
+ null,
+ false,
+ true,
+ true,
+ false,
+ false,
+ false,
+ null,
+ null,
+ null,
+ false,
+ false,
+ null
+ ]
+ },
+ "hash": "d47de9234302e9f1e022748731c4bc4400c9aa390e0e020f8674c63861917e7e"
+}
diff --git a/.sqlx/query-e3effaf90fa2b1bf1dc615ce0c74c1afc8d0e6774e71862ee0d39b5381647308.json b/.sqlx/query-e3effaf90fa2b1bf1dc615ce0c74c1afc8d0e6774e71862ee0d39b5381647308.json
deleted file mode 100644
index 8919fd97da..0000000000
--- a/.sqlx/query-e3effaf90fa2b1bf1dc615ce0c74c1afc8d0e6774e71862ee0d39b5381647308.json
+++ /dev/null
@@ -1,107 +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 t.link_id,\n latest_msg.sender as sender,\n latest_msg.pretty_sender as \"pretty_sender!\",\n latest_msg.trash_label as trash_label,\n (\n SELECT m_latest.is_draft\n FROM email_messages m_latest\n WHERE m_latest.thread_id = t.id\n AND m_latest.link_id = t.link_id\n ORDER BY m_latest.internal_date_ts DESC NULLS LAST\n LIMIT 1\n ) AS \"is_draft!\",\n t.is_read,\n t.inbox_visible,\n -- Gmail's IMPORTANT label — a different notion from the\n -- Importance filter / email_threads.is_signal heuristic.\n (\n SELECT EXISTS (\n SELECT 1\n FROM email_messages m_imp\n JOIN email_message_labels ml ON m_imp.id = ml.message_id\n JOIN email_labels l ON ml.label_id = l.id\n WHERE m_imp.thread_id = t.id\n AND l.link_id = t.link_id\n AND l.name = 'IMPORTANT'\n )\n ) AS \"is_important!\"\n FROM email_threads t\n LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = t.link_id\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 = t.link_id\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 = t.link_id\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 = ANY($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": "link_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 8,
- "name": "sender",
- "type_info": "Varchar"
- },
- {
- "ordinal": 9,
- "name": "pretty_sender!",
- "type_info": "Varchar"
- },
- {
- "ordinal": 10,
- "name": "trash_label",
- "type_info": "Bool"
- },
- {
- "ordinal": 11,
- "name": "is_draft!",
- "type_info": "Bool"
- },
- {
- "ordinal": 12,
- "name": "is_read",
- "type_info": "Bool"
- },
- {
- "ordinal": 13,
- "name": "inbox_visible",
- "type_info": "Bool"
- },
- {
- "ordinal": 14,
- "name": "is_important!",
- "type_info": "Bool"
- }
- ],
- "parameters": {
- "Left": [
- "UuidArray",
- "UuidArray"
- ]
- },
- "nullable": [
- false,
- null,
- null,
- false,
- true,
- true,
- false,
- false,
- false,
- null,
- null,
- null,
- false,
- false,
- null
- ]
- },
- "hash": "e3effaf90fa2b1bf1dc615ce0c74c1afc8d0e6774e71862ee0d39b5381647308"
-}
diff --git a/crates/email_db_client/src/user_history.rs b/crates/email_db_client/src/user_history.rs
index 1eabed36ef..fb3e695ae7 100644
--- a/crates/email_db_client/src/user_history.rs
+++ b/crates/email_db_client/src/user_history.rs
@@ -49,6 +49,9 @@ where
/// 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
+///
+/// Discarded drafts (`provider_missing` tombstones) are skipped, as the thread
+/// view skips them, so a thread left with only one is excluded.
#[tracing::instrument(skip(pool), err)]
pub async fn get_thread_summary_info(
pool: &PgPool,
@@ -80,6 +83,7 @@ pub async fn get_thread_summary_info(
FROM email_messages m_latest
WHERE m_latest.thread_id = t.id
AND m_latest.link_id = t.link_id
+ AND NOT (m_latest.is_draft AND COALESCE((m_latest.mailbox_state->>'provider_missing')::boolean, false))
ORDER BY m_latest.internal_date_ts DESC NULLS LAST
LIMIT 1
) AS "is_draft!",
@@ -121,6 +125,7 @@ pub async fn get_thread_summary_info(
LEFT JOIN email_contacts c ON c.id = m2.from_contact_id
WHERE m2.thread_id = t.id
AND m2.link_id = t.link_id
+ AND NOT (m2.is_draft AND COALESCE((m2.mailbox_state->>'provider_missing')::boolean, false))
ORDER BY
(CASE WHEN m2.is_draft = false AND m2.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,
COALESCE(m2.sent_at, m2.updated_at) DESC NULLS LAST
@@ -135,6 +140,7 @@ pub async fn get_thread_summary_info(
FROM email_messages m3
WHERE m3.thread_id = t.id
AND m3.link_id = t.link_id
+ AND NOT (m3.is_draft AND COALESCE((m3.mailbox_state->>'provider_missing')::boolean, false))
ORDER BY
-- 1. Priority: Non-drafts with valid sent_at come first (0), everything else is fallback (1)
(CASE WHEN m3.is_draft = false AND m3.sent_at IS NOT NULL THEN 0 ELSE 1 END) ASC,
diff --git a/crates/email_db_client/src/user_history/test.rs b/crates/email_db_client/src/user_history/test.rs
index a102132e08..afb9d1051b 100644
--- a/crates/email_db_client/src/user_history/test.rs
+++ b/crates/email_db_client/src/user_history/test.rs
@@ -271,3 +271,62 @@ async fn get_thread_summary_info_filter_out_trash(pool: Pool<Postgres>) -> Resul
Ok(())
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../fixtures", scripts("get_thread_summary_info"))
+)]
+async fn get_thread_summary_info_skips_discarded_drafts(pool: Pool<Postgres>) -> Result<()> {
+ const _: &sqlx::migrate::Migrator = &MACRO_DB_MIGRATIONS;
+
+ let link_id = Uuid::parse_str("00000000-0000-0000-0000-00000000001c")?;
+ let replied_thread_id = Uuid::parse_str("00000000-0000-0000-0000-000000000301")?;
+ let draft_only_thread_id = Uuid::parse_str("00000000-0000-0000-0000-000000000309")?;
+
+ // Discarding an Outlook draft keeps its row as a `provider_missing`
+ // tombstone: a newer reply draft in thread 301, and the only message of
+ // thread 309.
+ sqlx::query!(
+ "INSERT INTO email_threads (id, link_id) VALUES ($1, $2)",
+ draft_only_thread_id,
+ link_id
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_messages (id, thread_id, link_id, is_draft, is_sent, from_contact_id,
+ internal_date_ts, mailbox_state)
+ VALUES ('00000000-0000-0000-0000-0000000e0622', $1, $3, true, false,
+ '00000000-0000-0000-0000-0000000c0020', '2025-01-11 10:00:00 +00:00',
+ '{"provider_missing": true}'),
+ ('00000000-0000-0000-0000-0000000e0623', $2, $3, true, false,
+ '00000000-0000-0000-0000-0000000c0020', '2025-01-11 10:00:00 +00:00',
+ '{"provider_missing": true}')
+ "#,
+ replied_thread_id,
+ draft_only_thread_id,
+ link_id
+ )
+ .execute(&pool)
+ .await?;
+
+ let result = get_thread_summary_info(
+ &pool,
+ &[link_id],
+ &[replied_thread_id, draft_only_thread_id],
+ )
+ .await?;
+
+ assert!(
+ !result.contains_key(&draft_only_thread_id),
+ "a thread holding only a discarded draft opens as not found, so search must drop it"
+ );
+ let replied = result.get(&replied_thread_id).unwrap();
+ assert!(
+ !replied.is_draft,
+ "a discarded reply draft must not mark its thread as a draft"
+ );
+
+ Ok(())
+}