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.

refactor(email): Inbox and All Mail thread previews (#1083)

Evan Hutnik366bfdd232ff
diff --git a/rust/cloud-storage/email/.sqlx/query-16be1b3716f6b054c3714cfdac989426c3ac6cd0136655915d576d2318ae8682.json b/rust/cloud-storage/email/.sqlx/query-16be1b3716f6b054c3714cfdac989426c3ac6cd0136655915d576d2318ae8682.json
deleted file mode 100644
index 1c07f2bdd8..0000000000
--- a/rust/cloud-storage/email/.sqlx/query-16be1b3716f6b054c3714cfdac989426c3ac6cd0136655915d576d2318ae8682.json
+++ /dev/null
@@ -1,110 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n t.id,\n t.provider_id,\n t.inbox_visible,\n t.is_read,\n t.effective_ts AS \"sort_ts!\",\n t.created_at AS \"created_at!\",\n t.updated_at AS \"updated_at!\",\n t.viewed_at AS \"viewed_at?\",\n lmp.subject AS \"name?\",\n lmp.snippet AS \"snippet?\",\n lmp.is_draft,\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.name = 'IMPORTANT'\n AND l.link_id = t.link_id\n )\n ) AS \"is_important!\",\n c.email_address AS \"sender_email?\",\n COALESCE(lmp.from_name, c.name) AS \"sender_name?\",\n c.sfs_photo_url as \"sender_photo_url?\"\n FROM (\n -- Step 1: Efficiently find, sort, and limit the top N+1 threads.\n -- This subquery only touches `threads` and `user_history`, making it very fast.\n SELECT\n t.id,\n t.provider_id,\n t.link_id,\n t.inbox_visible,\n t.is_read,\n t.latest_non_spam_message_ts AS created_at,\n t.latest_non_spam_message_ts AS updated_at,\n uh.updated_at AS viewed_at,\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_non_spam_message_ts)\n ELSE t.latest_non_spam_message_ts\n END AS effective_ts\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 WHERE\n t.link_id = $1\n AND t.latest_non_spam_message_ts IS NOT NULL\n \n -- Cursor logic is moved inside for maximum efficiency\n AND (($3::timestamptz IS NULL) OR (\n -- This CASE must exactly match the one that defines `effective_ts`\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_non_spam_message_ts)\n ELSE t.latest_non_spam_message_ts\n END, t.id\n ) < ($3::timestamptz, $4::uuid))\n ORDER BY effective_ts DESC, t.updated_at DESC\n LIMIT $2\n ) AS t\n -- Step 2: For EACH of the limited threads from above, find its latest non-SPAM/TRASH message.\n CROSS JOIN LATERAL (\n SELECT\n m.subject,\n m.snippet,\n m.is_draft,\n m.from_contact_id,\n m.from_name\n FROM email_messages m\n WHERE m.thread_id = t.id\n AND m.is_draft = FALSE\n AND NOT EXISTS (\n SELECT 1\n FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id\n AND l.link_id = t.link_id\n AND l.name IN ('SPAM', 'TRASH')\n )\n ORDER BY m.internal_date_ts DESC\n LIMIT 1\n ) AS lmp\n -- Step 3: Join to get the sender's details for the final result set.\n LEFT JOIN email_contacts c ON lmp.from_contact_id = c.id\n ORDER BY t.effective_ts DESC, t.updated_at DESC\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "provider_id",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "inbox_visible",
- "type_info": "Bool"
- },
- {
- "ordinal": 3,
- "name": "is_read",
- "type_info": "Bool"
- },
- {
- "ordinal": 4,
- "name": "sort_ts!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 5,
- "name": "created_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 6,
- "name": "updated_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 7,
- "name": "viewed_at?",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 8,
- "name": "name?",
- "type_info": "Text"
- },
- {
- "ordinal": 9,
- "name": "snippet?",
- "type_info": "Text"
- },
- {
- "ordinal": 10,
- "name": "is_draft",
- "type_info": "Bool"
- },
- {
- "ordinal": 11,
- "name": "is_important!",
- "type_info": "Bool"
- },
- {
- "ordinal": 12,
- "name": "sender_email?",
- "type_info": "Varchar"
- },
- {
- "ordinal": 13,
- "name": "sender_name?",
- "type_info": "Varchar"
- },
- {
- "ordinal": 14,
- "name": "sender_photo_url?",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Int8",
- "Timestamptz",
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- false,
- true,
- false,
- false,
- null,
- true,
- true,
- false,
- true,
- true,
- false,
- null,
- false,
- null,
- true
- ]
- },
- "hash": "16be1b3716f6b054c3714cfdac989426c3ac6cd0136655915d576d2318ae8682"
-}
diff --git a/rust/cloud-storage/email/.sqlx/query-7794d9a18fedeb8c4bfe541f01e690007beef397f2a772d2e2a8ace54b04a321.json b/rust/cloud-storage/email/.sqlx/query-7794d9a18fedeb8c4bfe541f01e690007beef397f2a772d2e2a8ace54b04a321.json
new file mode 100644
index 0000000000..1778fc94e9
--- /dev/null
+++ b/rust/cloud-storage/email/.sqlx/query-7794d9a18fedeb8c4bfe541f01e690007beef397f2a772d2e2a8ace54b04a321.json
@@ -0,0 +1,110 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH spam_label AS (\n SELECT id FROM email_labels WHERE link_id = $1 AND name = 'SPAM'\n ),\n trash_label AS (\n SELECT id FROM email_labels WHERE link_id = $1 AND name = 'TRASH'\n ),\n important_label AS (\n SELECT id FROM email_labels WHERE link_id = $1 AND name = 'IMPORTANT'\n )\n SELECT\n t.id,\n t.provider_id,\n t.inbox_visible,\n t.is_read,\n t.effective_ts AS \"sort_ts!\",\n t.created_at AS \"created_at!\",\n t.updated_at AS \"updated_at!\",\n t.viewed_at AS \"viewed_at?\",\n lmp.subject AS \"name?\",\n lmp.snippet AS \"snippet?\",\n lmp.is_draft,\n EXISTS (\n SELECT 1\n FROM email_messages m_imp\n JOIN email_message_labels ml ON m_imp.id = ml.message_id\n WHERE m_imp.thread_id = t.id\n AND ml.label_id = (SELECT id FROM important_label)\n ) AS \"is_important!\",\n c.email_address AS \"sender_email?\",\n COALESCE(lmp.from_name, c.name) AS \"sender_name?\",\n c.sfs_photo_url as \"sender_photo_url?\"\n FROM (\n -- Step 1: Efficiently find, sort, and limit the top N+1 threads.\n -- This subquery only touches `threads` and `user_history`, making it very fast.\n SELECT\n t.id,\n t.provider_id,\n t.link_id,\n t.inbox_visible,\n t.is_read,\n t.latest_non_spam_message_ts AS created_at,\n t.latest_non_spam_message_ts AS updated_at,\n uh.updated_at AS viewed_at,\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_non_spam_message_ts)\n ELSE t.latest_non_spam_message_ts\n END AS effective_ts\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 WHERE\n t.link_id = $1\n AND t.latest_non_spam_message_ts IS NOT NULL\n \n -- Cursor logic is moved inside for maximum efficiency\n AND (($3::timestamptz IS NULL) OR (\n -- This CASE must exactly match the one that defines `effective_ts`\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_non_spam_message_ts)\n ELSE t.latest_non_spam_message_ts\n END, t.id\n ) < ($3::timestamptz, $4::uuid))\n ORDER BY effective_ts DESC, t.updated_at DESC\n LIMIT $2\n ) AS t\n -- Step 2: For EACH of the limited threads from above, find its latest non-SPAM/TRASH message.\n CROSS JOIN LATERAL (\n SELECT\n m.subject,\n m.snippet,\n m.is_draft,\n m.from_contact_id,\n m.from_name\n FROM email_messages m\n WHERE m.thread_id = t.id\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n WHERE ml.message_id = m.id\n AND ml.label_id = (SELECT id FROM spam_label)\n )\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n WHERE ml.message_id = m.id\n AND ml.label_id = (SELECT id FROM trash_label)\n )\n ORDER BY m.internal_date_ts DESC\n LIMIT 1\n ) AS lmp\n -- Step 3: Join to get the sender's details for the final result set.\n LEFT JOIN email_contacts c ON lmp.from_contact_id = c.id\n ORDER BY t.effective_ts DESC, t.updated_at DESC\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "provider_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "inbox_visible",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "is_read",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 4,
+ "name": "sort_ts!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 5,
+ "name": "created_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "updated_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "viewed_at?",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "name?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
+ "name": "snippet?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 10,
+ "name": "is_draft",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 11,
+ "name": "is_important!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "sender_email?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 13,
+ "name": "sender_name?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 14,
+ "name": "sender_photo_url?",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8",
+ "Timestamptz",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ false,
+ null,
+ true,
+ true,
+ false,
+ true,
+ true,
+ false,
+ null,
+ false,
+ null,
+ true
+ ]
+ },
+ "hash": "7794d9a18fedeb8c4bfe541f01e690007beef397f2a772d2e2a8ace54b04a321"
+}
diff --git a/rust/cloud-storage/email/.sqlx/query-eb79c7c4cd4be9a99f9e39309d0ed615c046243986a95696f09558cd71a7172a.json b/rust/cloud-storage/email/.sqlx/query-eb79c7c4cd4be9a99f9e39309d0ed615c046243986a95696f09558cd71a7172a.json
deleted file mode 100644
index 2ae7994934..0000000000
--- a/rust/cloud-storage/email/.sqlx/query-eb79c7c4cd4be9a99f9e39309d0ed615c046243986a95696f09558cd71a7172a.json
+++ /dev/null
@@ -1,110 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n t.id,\n t.provider_id,\n TRUE AS \"inbox_visible!\",\n t.is_read,\n t.effective_ts AS \"sort_ts!\",\n t.created_at AS \"created_at!\",\n t.updated_at AS \"updated_at!\",\n t.viewed_at AS \"viewed_at?\",\n lmp.subject AS \"name?\",\n lmp.snippet AS \"snippet?\",\n lmp.is_draft,\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.name = 'IMPORTANT'\n AND l.link_id = t.link_id\n )\n ) AS \"is_important!\",\n c.email_address AS \"sender_email?\",\n COALESCE(lmp.from_name, c.name) AS \"sender_name?\",\n c.sfs_photo_url as \"sender_photo_url?\"\n FROM (\n -- Step 1: Efficiently find and sort ONLY the top N+1 candidate threads.\n -- This subquery is fast as it only touches `threads` and `user_history`.\n SELECT\n t.id,\n t.provider_id,\n t.link_id,\n t.is_read,\n t.latest_inbound_message_ts AS created_at,\n t.latest_inbound_message_ts AS updated_at,\n uh.updated_at AS viewed_at,\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_inbound_message_ts)\n ELSE t.latest_inbound_message_ts\n END AS effective_ts\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 WHERE\n t.link_id = $1\n AND t.inbox_visible = TRUE\n AND t.latest_inbound_message_ts IS NOT NULL\n \n -- The cursor logic is moved inside this subquery for maximum efficiency.\n AND (($3::timestamptz IS NULL) OR (\n -- This CASE must exactly match the one that defines `effective_ts`\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_inbound_message_ts)\n ELSE t.latest_inbound_message_ts\n END, t.id\n ) < ($3::timestamptz, $4::uuid))\n ORDER BY effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same\n LIMIT $2\n ) AS t\n -- Step 2: For EACH of the limited threads from above, find its latest non-trashed message.\n CROSS JOIN LATERAL (\n SELECT\n m.subject,\n m.snippet,\n m.from_contact_id,\n m.from_name,\n m.is_draft\n FROM email_messages m\n WHERE m.thread_id = t.id\n AND m.is_draft = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH' AND l.link_id = t.link_id\n )\n ORDER BY m.internal_date_ts DESC\n LIMIT 1\n ) AS lmp\n -- Step 3: Join to get the sender's details for the final result set.\n LEFT JOIN email_contacts c ON lmp.from_contact_id = c.id\n -- Final ordering is preserved because the input `t` is already sorted.\n ORDER BY t.effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "provider_id",
- "type_info": "Text"
- },
- {
- "ordinal": 2,
- "name": "inbox_visible!",
- "type_info": "Bool"
- },
- {
- "ordinal": 3,
- "name": "is_read",
- "type_info": "Bool"
- },
- {
- "ordinal": 4,
- "name": "sort_ts!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 5,
- "name": "created_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 6,
- "name": "updated_at!",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 7,
- "name": "viewed_at?",
- "type_info": "Timestamptz"
- },
- {
- "ordinal": 8,
- "name": "name?",
- "type_info": "Text"
- },
- {
- "ordinal": 9,
- "name": "snippet?",
- "type_info": "Text"
- },
- {
- "ordinal": 10,
- "name": "is_draft",
- "type_info": "Bool"
- },
- {
- "ordinal": 11,
- "name": "is_important!",
- "type_info": "Bool"
- },
- {
- "ordinal": 12,
- "name": "sender_email?",
- "type_info": "Varchar"
- },
- {
- "ordinal": 13,
- "name": "sender_name?",
- "type_info": "Varchar"
- },
- {
- "ordinal": 14,
- "name": "sender_photo_url?",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Int8",
- "Timestamptz",
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- false,
- true,
- null,
- false,
- null,
- true,
- true,
- false,
- true,
- true,
- false,
- null,
- false,
- null,
- true
- ]
- },
- "hash": "eb79c7c4cd4be9a99f9e39309d0ed615c046243986a95696f09558cd71a7172a"
-}
diff --git a/rust/cloud-storage/email/.sqlx/query-fe3861aa924320e57d72dbd209973c71c70608af0e34c5c530c31d4b99286b15.json b/rust/cloud-storage/email/.sqlx/query-fe3861aa924320e57d72dbd209973c71c70608af0e34c5c530c31d4b99286b15.json
new file mode 100644
index 0000000000..096d4ee68e
--- /dev/null
+++ b/rust/cloud-storage/email/.sqlx/query-fe3861aa924320e57d72dbd209973c71c70608af0e34c5c530c31d4b99286b15.json
@@ -0,0 +1,110 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH trash_label AS (\n SELECT id FROM email_labels WHERE link_id = $1 AND name = 'TRASH'\n ),\n important_label AS (\n SELECT id FROM email_labels WHERE link_id = $1 AND name = 'IMPORTANT'\n )\n SELECT\n t.id,\n t.provider_id,\n TRUE AS \"inbox_visible!\",\n t.is_read,\n t.effective_ts AS \"sort_ts!\",\n t.created_at AS \"created_at!\",\n t.updated_at AS \"updated_at!\",\n t.viewed_at AS \"viewed_at?\",\n lmp.subject AS \"name?\",\n lmp.snippet AS \"snippet?\",\n lmp.is_draft,\n EXISTS (\n SELECT 1\n FROM email_messages m_imp\n JOIN email_message_labels ml ON m_imp.id = ml.message_id\n WHERE m_imp.thread_id = t.id\n AND ml.label_id = (SELECT id FROM important_label)\n ) AS \"is_important!\",\n c.email_address AS \"sender_email?\",\n COALESCE(lmp.from_name, c.name) AS \"sender_name?\",\n c.sfs_photo_url as \"sender_photo_url?\"\n FROM (\n -- Step 1: Efficiently find and sort ONLY the top N+1 candidate threads.\n -- This subquery is fast as it only touches `threads` and `user_history`.\n SELECT\n t.id,\n t.provider_id,\n t.link_id,\n t.is_read,\n t.latest_inbound_message_ts AS created_at,\n t.latest_inbound_message_ts AS updated_at,\n uh.updated_at AS viewed_at,\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_inbound_message_ts)\n ELSE t.latest_inbound_message_ts\n END AS effective_ts\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 WHERE\n t.link_id = $1\n AND t.inbox_visible = TRUE\n AND t.latest_inbound_message_ts IS NOT NULL\n \n -- The cursor logic is moved inside this subquery for maximum efficiency.\n AND (($3::timestamptz IS NULL) OR (\n -- This CASE must exactly match the one that defines `effective_ts`\n CASE $5 -- sort_method_str\n WHEN 'viewed_at' THEN COALESCE(uh.\"updated_at\", '1970-01-01 00:00:00+00')\n WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, t.latest_inbound_message_ts)\n ELSE t.latest_inbound_message_ts\n END, t.id\n ) < ($3::timestamptz, $4::uuid))\n ORDER BY effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same\n LIMIT $2\n ) AS t\n -- Step 2: For EACH of the limited threads from above, find its latest non-trashed message.\n CROSS JOIN LATERAL (\n SELECT\n m.subject,\n m.snippet,\n m.from_contact_id,\n m.from_name,\n m.is_draft\n FROM email_messages m\n WHERE m.thread_id = t.id\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n WHERE ml.message_id = m.id\n AND ml.label_id = (SELECT id FROM trash_label)\n )\n ORDER BY m.internal_date_ts DESC\n LIMIT 1\n ) AS lmp\n -- Step 3: Join to get the sender's details for the final result set.\n LEFT JOIN email_contacts c ON lmp.from_contact_id = c.id\n -- Final ordering is preserved because the input `t` is already sorted.\n ORDER BY t.effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "provider_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "inbox_visible!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 3,
+ "name": "is_read",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 4,
+ "name": "sort_ts!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 5,
+ "name": "created_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "updated_at!",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "viewed_at?",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 8,
+ "name": "name?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 9,
+ "name": "snippet?",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 10,
+ "name": "is_draft",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 11,
+ "name": "is_important!",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 12,
+ "name": "sender_email?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 13,
+ "name": "sender_name?",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 14,
+ "name": "sender_photo_url?",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8",
+ "Timestamptz",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ null,
+ false,
+ null,
+ true,
+ true,
+ false,
+ true,
+ true,
+ false,
+ null,
+ false,
+ null,
+ true
+ ]
+ },
+ "hash": "fe3861aa924320e57d72dbd209973c71c70608af0e34c5c530c31d4b99286b15"
+}
diff --git a/rust/cloud-storage/email/justfile b/rust/cloud-storage/email/justfile
index 298576ed00..d5a52bc432 100644
--- a/rust/cloud-storage/email/justfile
+++ b/rust/cloud-storage/email/justfile
@@ -3,4 +3,4 @@ DATABASE_URL := "postgres://user:password@localhost:5432/macrodb"
mod sqlx '../sqlx.just'
prepare_db:
- just sqlx::prepare_db {{ DATABASE_URL }} --all-features
+ just sqlx::prepare_db {{ DATABASE_URL }}
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/all_mail.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/all_mail.rs
index f8573652e7..1fc23b3c8d 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/all_mail.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/all_mail.rs
@@ -22,6 +22,15 @@ pub(crate) async fn all_mail_preview_cursor(
sqlx::query_as!(
ThreadPreviewCursorDbRow,
r#"
+ WITH spam_label AS (
+ SELECT id FROM email_labels WHERE link_id = $1 AND name = 'SPAM'
+ ),
+ trash_label AS (
+ SELECT id FROM email_labels WHERE link_id = $1 AND name = 'TRASH'
+ ),
+ important_label AS (
+ SELECT id FROM email_labels WHERE link_id = $1 AND name = 'IMPORTANT'
+ )
SELECT
t.id,
t.provider_id,
@@ -34,16 +43,12 @@ pub(crate) async fn all_mail_preview_cursor(
lmp.subject AS "name?",
lmp.snippet AS "snippet?",
lmp.is_draft,
- (
- SELECT EXISTS (
- SELECT 1
- FROM email_messages m_imp
- JOIN email_message_labels ml ON m_imp.id = ml.message_id
- JOIN email_labels l ON ml.label_id = l.id
- WHERE m_imp.thread_id = t.id
- AND l.name = 'IMPORTANT'
- AND l.link_id = t.link_id
- )
+ EXISTS (
+ SELECT 1
+ FROM email_messages m_imp
+ JOIN email_message_labels ml ON m_imp.id = ml.message_id
+ WHERE m_imp.thread_id = t.id
+ AND ml.label_id = (SELECT id FROM important_label)
) AS "is_important!",
c.email_address AS "sender_email?",
COALESCE(lmp.from_name, c.name) AS "sender_name?",
@@ -93,15 +98,16 @@ pub(crate) async fn all_mail_preview_cursor(
m.from_name
FROM email_messages m
WHERE m.thread_id = t.id
- AND m.is_draft = FALSE
- AND NOT EXISTS (
- SELECT 1
- FROM email_message_labels ml
- JOIN email_labels l ON ml.label_id = l.id
- WHERE ml.message_id = m.id
- AND l.link_id = t.link_id
- AND l.name IN ('SPAM', 'TRASH')
- )
+ AND NOT EXISTS (
+ SELECT 1 FROM email_message_labels ml
+ WHERE ml.message_id = m.id
+ AND ml.label_id = (SELECT id FROM spam_label)
+ )
+ AND NOT EXISTS (
+ SELECT 1 FROM email_message_labels ml
+ WHERE ml.message_id = m.id
+ AND ml.label_id = (SELECT id FROM trash_label)
+ )
ORDER BY m.internal_date_ts DESC
LIMIT 1
) AS lmp
@@ -110,10 +116,10 @@ pub(crate) async fn all_mail_preview_cursor(
ORDER BY t.effective_ts DESC, t.updated_at DESC
"#,
link_id, // $1
- query_limit, // $2
+ query_limit, // $2
cursor_timestamp, // $3
cursor_id, // $4
- sort_method_str, // $5
+ sort_method_str, // $5
)
.fetch_all(pool)
.await
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/new_inbox.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/new_inbox.rs
index bde33fdd01..460dcf7a9b 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/new_inbox.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/queries/new_inbox.rs
@@ -21,6 +21,12 @@ pub(crate) async fn new_inbox_preview_cursor(
sqlx::query_as!(
ThreadPreviewCursorDbRow,
r#"
+ WITH trash_label AS (
+ SELECT id FROM email_labels WHERE link_id = $1 AND name = 'TRASH'
+ ),
+ important_label AS (
+ SELECT id FROM email_labels WHERE link_id = $1 AND name = 'IMPORTANT'
+ )
SELECT
t.id,
t.provider_id,
@@ -33,16 +39,12 @@ pub(crate) async fn new_inbox_preview_cursor(
lmp.subject AS "name?",
lmp.snippet AS "snippet?",
lmp.is_draft,
- (
- SELECT EXISTS (
- SELECT 1
- FROM email_messages m_imp
- JOIN email_message_labels ml ON m_imp.id = ml.message_id
- JOIN email_labels l ON ml.label_id = l.id
- WHERE m_imp.thread_id = t.id
- AND l.name = 'IMPORTANT'
- AND l.link_id = t.link_id
- )
+ EXISTS (
+ SELECT 1
+ FROM email_messages m_imp
+ JOIN email_message_labels ml ON m_imp.id = ml.message_id
+ WHERE m_imp.thread_id = t.id
+ AND ml.label_id = (SELECT id FROM important_label)
) AS "is_important!",
c.email_address AS "sender_email?",
COALESCE(lmp.from_name, c.name) AS "sender_name?",
@@ -92,11 +94,11 @@ pub(crate) async fn new_inbox_preview_cursor(
m.is_draft
FROM email_messages m
WHERE m.thread_id = t.id
- AND m.is_draft = FALSE
AND NOT EXISTS (
- SELECT 1 FROM email_message_labels ml JOIN email_labels l ON ml.label_id = l.id
- WHERE ml.message_id = m.id AND l.name = 'TRASH' AND l.link_id = t.link_id
- )
+ SELECT 1 FROM email_message_labels ml
+ WHERE ml.message_id = m.id
+ AND ml.label_id = (SELECT id FROM trash_label)
+ )
ORDER BY m.internal_date_ts DESC
LIMIT 1
) AS lmp
@@ -106,9 +108,9 @@ pub(crate) async fn new_inbox_preview_cursor(
ORDER BY t.effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same
"#,
link_id, // $1
- query_limit, // $2
+ query_limit, // $2
cursor_timestamp, // $3
cursor_id, // $4
- sort_method_str, // $5
+ sort_method_str, // $5
).fetch_all(pool).await
}
diff --git a/rust/cloud-storage/email_db_client/src/messages/insert.rs b/rust/cloud-storage/email_db_client/src/messages/insert.rs
index dfed651813..dea682bdd3 100644
--- a/rust/cloud-storage/email_db_client/src/messages/insert.rs
+++ b/rust/cloud-storage/email_db_client/src/messages/insert.rs
@@ -304,6 +304,8 @@ pub async fn insert_message_to_send_db(
contacts::upsert_message::upsert_message_recipients(tx, message_db_id, &recipients).await?;
+ threads::update::update_thread_metadata(tx, thread_id, db_message_to_send.link_id).await?;
+
Ok(())
}
diff --git a/rust/cloud-storage/email_db_client/src/threads/update.rs b/rust/cloud-storage/email_db_client/src/threads/update.rs
index 70a84dd76b..d3d7fb42dd 100644
--- a/rust/cloud-storage/email_db_client/src/threads/update.rs
+++ b/rust/cloud-storage/email_db_client/src/threads/update.rs
@@ -3,6 +3,7 @@ use anyhow::Context;
use chrono::{DateTime, Utc};
use models_email::email::service::message::{is_inbound, is_outbound, is_spam_or_trash};
use models_email::service;
+use models_email::service::message::is_macro_draft;
use sqlx::types::Uuid;
/// Updates a thread's metadata
@@ -161,36 +162,54 @@ pub async fn update_thread_metadata(
.labels
.iter()
.any(|label| label.provider_label_id == service::label::system_labels::INBOX)
+ || (is_macro_draft(message))
});
// if any message in the thread is unread, the thread is considered unread in the FE
let is_read = !messages.iter().any(|message| !message.is_read);
+ let latest_draft_ts = messages
+ .iter()
+ .filter(|msg| is_macro_draft(msg))
+ .map(|msg| msg.updated_at)
+ .max();
+
let latest_inbound_timestamp_ts = messages
.iter()
.find(|msg| is_inbound(msg))
.map(|msg| msg.internal_date_ts)
.unwrap_or_else(|| None);
+ let latest_inbound_or_draft_ts = [latest_inbound_timestamp_ts, latest_draft_ts]
+ .into_iter()
+ .flatten()
+ .max();
+
let latest_outbound_message_ts = messages
.iter()
.find(|msg| is_outbound(msg))
.map(|msg| msg.internal_date_ts)
.unwrap_or_else(|| None);
- let latest_non_spam_message_ts = messages
+ // latest non-spam message timestamp is the latest macro draft or provider message
+ let latest_provider_message_ts = messages
.iter()
.find(|msg| !is_spam_or_trash(msg))
.map(|msg| msg.internal_date_ts)
.unwrap_or_else(|| None);
+ let latest_non_spam_message_ts = [latest_provider_message_ts, latest_draft_ts]
+ .into_iter()
+ .flatten()
+ .max();
+
update_db_thread_metadata(
&mut *tx,
thread_db_id,
link_id,
inbox_visible,
is_read,
- latest_inbound_timestamp_ts,
+ latest_inbound_or_draft_ts,
latest_outbound_message_ts,
latest_non_spam_message_ts,
)
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260107190319_email_preview_indices.sql b/rust/cloud-storage/macro_db_client/migrations/20260107190319_email_preview_indices.sql
new file mode 100644
index 0000000000..674af34138
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260107190319_email_preview_indices.sql
@@ -0,0 +1,5 @@
+-- no-transaction
+
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_email_messages_thread_date_not_draft
+ ON public.email_messages (thread_id, internal_date_ts DESC)
+ WHERE (is_draft = FALSE);
\ No newline at end of file
diff --git a/rust/cloud-storage/models_email/src/email/service/message.rs b/rust/cloud-storage/models_email/src/email/service/message.rs
index c6c5826eab..629fe3bb79 100644
--- a/rust/cloud-storage/models_email/src/email/service/message.rs
+++ b/rust/cloud-storage/models_email/src/email/service/message.rs
@@ -212,6 +212,12 @@ pub fn is_outbound(msg: &Message) -> bool {
msg.is_sent
}
+/// determine if a message is a draft created in macro
+pub fn is_macro_draft(msg: &Message) -> bool {
+ // we don't send drafts to the provider before sending the message, so it won't have a provider id
+ msg.is_draft && msg.provider_id.is_none()
+}
+
/// determine if a message is spam/trash for use in latest_non_spam_message_ts
/// - for thread ordering purposes in the FE. (all mail view)
pub fn is_spam_or_trash(msg: &Message) -> bool {