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(notifs): Filter Macro's own digest emails out of Signal and email notifications (#7039)

teo2bdbeae35890
diff --git a/.sqlx/query-3b9b441b224f60a4832dee29e4151a2bc6a87fe87544bf4fdfeee20aeec7c5bf.json b/.sqlx/query-3b9b441b224f60a4832dee29e4151a2bc6a87fe87544bf4fdfeee20aeec7c5bf.json
deleted file mode 100644
index 3a34936eb7..0000000000
--- a/.sqlx/query-3b9b441b224f60a4832dee29e4151a2bc6a87fe87544bf4fdfeee20aeec7c5bf.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT count(*) AS \"mismatched!\"\n FROM email_threads t\n WHERE t.link_id = $1\n AND t.is_signal IS DISTINCT FROM EXISTS (\n SELECT 1\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 JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n )\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "mismatched!",
- "type_info": "Int8"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- null
- ]
- },
- "hash": "3b9b441b224f60a4832dee29e4151a2bc6a87fe87544bf4fdfeee20aeec7c5bf"
-}
diff --git a/.sqlx/query-3ceaed9a7629ff2d14a7d31034c4218c1fbae705dfcd75053f50abceccb3f958.json b/.sqlx/query-3ceaed9a7629ff2d14a7d31034c4218c1fbae705dfcd75053f50abceccb3f958.json
new file mode 100644
index 0000000000..5b8cd43b62
--- /dev/null
+++ b/.sqlx/query-3ceaed9a7629ff2d14a7d31034c4218c1fbae705dfcd75053f50abceccb3f958.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_threads t\n SET is_signal = calc.sig\n FROM (\n SELECT EXISTS (\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = $1\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND NOT EXISTS (\n SELECT 1 FROM email_contacts sender_c\n WHERE sender_c.id = m.from_contact_id\n AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) AS sig\n ) calc\n WHERE t.id = $1\n AND t.is_signal IS DISTINCT FROM calc.sig\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "3ceaed9a7629ff2d14a7d31034c4218c1fbae705dfcd75053f50abceccb3f958"
+}
diff --git a/.sqlx/query-53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f.json b/.sqlx/query-53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f.json
deleted file mode 100644
index 330f58a2b0..0000000000
--- a/.sqlx/query-53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_threads t\n SET is_signal = calc.sig\n FROM (\n SELECT EXISTS (\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = $1\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) AS sig\n ) calc\n WHERE t.id = $1\n AND t.is_signal IS DISTINCT FROM calc.sig\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "53d295231b271d328fab23120f45812ae613e1761ae5dfbc5199221f72c91b8f"
-}
diff --git a/.sqlx/query-54db04e4adf431e837ba8b7ae28e8a5b8827bcef21d0eed94432b57c82692db3.json b/.sqlx/query-54db04e4adf431e837ba8b7ae28e8a5b8827bcef21d0eed94432b57c82692db3.json
new file mode 100644
index 0000000000..716b2ebf7e
--- /dev/null
+++ b/.sqlx/query-54db04e4adf431e837ba8b7ae28e8a5b8827bcef21d0eed94432b57c82692db3.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH affected AS MATERIALIZED (\n -- Contact-first candidate scan, MATERIALIZED so the planner can't\n -- hoist the expensive per-thread sig predicate above this\n -- selective filter and evaluate it for the whole link.\n SELECT DISTINCT m.thread_id\n FROM email_contacts c\n JOIN email_messages m ON m.from_contact_id = c.id\n WHERE c.link_id = $1\n AND m.link_id = $1\n AND ($2::text IS NULL OR LOWER(c.email_address) = LOWER($2))\n AND ($3::text IS NULL OR LOWER(SPLIT_PART(c.email_address, '@', 2)) = LOWER($3))\n ),\n -- MATERIALIZED so sig is computed once per thread; inlined, both the\n -- SET and the IS DISTINCT FROM would re-evaluate the predicate.\n calc AS MATERIALIZED (\n SELECT a.thread_id,\n EXISTS (\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = a.thread_id\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND NOT EXISTS (\n SELECT 1 FROM email_contacts sender_c\n WHERE sender_c.id = m.from_contact_id\n AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $4\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) AS sig\n FROM affected a\n )\n UPDATE email_threads t\n SET is_signal = calc.sig\n FROM calc\n WHERE t.id = calc.thread_id\n AND t.link_id = $1\n AND t.is_signal IS DISTINCT FROM calc.sig\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "54db04e4adf431e837ba8b7ae28e8a5b8827bcef21d0eed94432b57c82692db3"
+}
diff --git a/.sqlx/query-7b2035b69e7697ba7771a81d389d8c105174a92256e6baa0624d81534839b5e5.json b/.sqlx/query-7b2035b69e7697ba7771a81d389d8c105174a92256e6baa0624d81534839b5e5.json
new file mode 100644
index 0000000000..d349088520
--- /dev/null
+++ b/.sqlx/query-7b2035b69e7697ba7771a81d389d8c105174a92256e6baa0624d81534839b5e5.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_threads t\n SET is_signal = true\n FROM (\n SELECT DISTINCT m.thread_id\n FROM email_messages m\n WHERE m.link_id = $1\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND NOT EXISTS (\n SELECT 1 FROM email_contacts sender_c\n WHERE sender_c.id = m.from_contact_id\n AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) sig\n WHERE t.id = sig.thread_id\n AND NOT t.is_signal\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7b2035b69e7697ba7771a81d389d8c105174a92256e6baa0624d81534839b5e5"
+}
diff --git a/.sqlx/query-841f93ea132a969f964f70ba9df76f73ca6a64b74c840164ddb1ca863c37154e.json b/.sqlx/query-841f93ea132a969f964f70ba9df76f73ca6a64b74c840164ddb1ca863c37154e.json
new file mode 100644
index 0000000000..4de8589ee6
--- /dev/null
+++ b/.sqlx/query-841f93ea132a969f964f70ba9df76f73ca6a64b74c840164ddb1ca863c37154e.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT count(*) AS \"mismatched!\"\n FROM email_threads t\n WHERE t.link_id = $1\n AND t.is_signal IS DISTINCT FROM EXISTS (\n SELECT 1\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 JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND NOT EXISTS (\n SELECT 1 FROM email_contacts sender_c\n WHERE sender_c.id = m.from_contact_id\n AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "mismatched!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "841f93ea132a969f964f70ba9df76f73ca6a64b74c840164ddb1ca863c37154e"
+}
diff --git a/.sqlx/query-93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6.json b/.sqlx/query-93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6.json
deleted file mode 100644
index 80405fb98b..0000000000
--- a/.sqlx/query-93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_threads t\n SET is_signal = true\n FROM (\n SELECT DISTINCT m.thread_id\n FROM email_messages m\n WHERE m.link_id = $1\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) sig\n WHERE t.id = sig.thread_id\n AND NOT t.is_signal\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "93b0b51a1671eadaab74ba589a0cdb546316dc51e386490f35ce9aaaa9af0ea6"
-}
diff --git a/.sqlx/query-d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3.json b/.sqlx/query-d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3.json
deleted file mode 100644
index 9e6d95a9b6..0000000000
--- a/.sqlx/query-d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH affected AS MATERIALIZED (\n -- Contact-first candidate scan, MATERIALIZED so the planner can't\n -- hoist the expensive per-thread sig predicate above this\n -- selective filter and evaluate it for the whole link.\n SELECT DISTINCT m.thread_id\n FROM email_contacts c\n JOIN email_messages m ON m.from_contact_id = c.id\n WHERE c.link_id = $1\n AND m.link_id = $1\n AND ($2::text IS NULL OR LOWER(c.email_address) = LOWER($2))\n AND ($3::text IS NULL OR LOWER(SPLIT_PART(c.email_address, '@', 2)) = LOWER($3))\n ),\n -- MATERIALIZED so sig is computed once per thread; inlined, both the\n -- SET and the IS DISTINCT FROM would re-evaluate the predicate.\n calc AS MATERIALIZED (\n SELECT a.thread_id,\n EXISTS (\n SELECT 1\n FROM email_messages m\n WHERE m.thread_id = a.thread_id\n AND NOT EXISTS (\n SELECT 1 FROM email_message_labels ml\n JOIN email_labels l ON ml.label_id = l.id\n WHERE ml.message_id = m.id AND l.name = 'TRASH'\n )\n AND (\n (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = TRUE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = FALSE\n )\n )\n )\n OR (\n NOT (\n EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_address IS NOT NULL\n AND LOWER(ef.email_address) = LOWER(sender_c.email_address)\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n )\n OR EXISTS (\n SELECT 1\n FROM email_contacts sender_c\n JOIN email_filters ef\n ON ef.link_id = m.link_id\n AND ef.email_domain IS NOT NULL\n AND LOWER(ef.email_domain) = LOWER(SPLIT_PART(sender_c.email_address, '@', 2))\n WHERE sender_c.id = m.from_contact_id\n AND ef.is_important = FALSE\n AND NOT EXISTS (\n SELECT 1 FROM email_filters ef_addr\n WHERE ef_addr.link_id = m.link_id\n AND ef_addr.email_address IS NOT NULL\n AND LOWER(ef_addr.email_address) = LOWER(sender_c.email_address)\n AND ef_addr.is_important = TRUE\n )\n )\n )\n AND (\n m.is_draft = TRUE\n OR EXISTS (\n SELECT 1 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.name IN ('CATEGORY_PERSONAL', 'SENT', 'DRAFT')\n )\n OR NOT EXISTS (\n SELECT 1 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.name IN ('CATEGORY_UPDATES', 'CATEGORY_PROMOTIONS', 'CATEGORY_SOCIAL', 'CATEGORY_FORUMS')\n )\n )\n )\n )\n ) AS sig\n FROM affected a\n )\n UPDATE email_threads t\n SET is_signal = calc.sig\n FROM calc\n WHERE t.id = calc.thread_id\n AND t.link_id = $1\n AND t.is_signal IS DISTINCT FROM calc.sig\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Text",
- "Text"
- ]
- },
- "nullable": []
- },
- "hash": "d52e4f6426d32b333ee46a07031a91f5670946f87411ef0679dd3bf067c1ffb3"
-}
diff --git a/crates/email/fixtures/email_signal_flag.sql b/crates/email/fixtures/email_signal_flag.sql
index f3f671370b..98ef24c461 100644
--- a/crates/email/fixtures/email_signal_flag.sql
+++ b/crates/email/fixtures/email_signal_flag.sql
@@ -17,6 +17,10 @@
-- a done sent thread the draft resurfaced into the inbox; discarding the
-- draft must send it back out of the inbox (is_signal stays true via the
-- SENT label, but inbox_visible/latest_inbound_message_ts reset).
+-- Thread e207 (is_signal=true, stale on purpose): unlabeled message from
+-- Macro's own digest sender ([email protected]) — Macro
+-- notification emails are never signal, so update_thread_metadata must
+-- clear it, and marking the sender important must not promote it.
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
VALUES ('00000000-0000-0000-0000-000000000e01', 'macro|[email protected]', '00000000-0000-0000-0000-000000000e01',
@@ -25,7 +29,8 @@ VALUES ('00000000-0000-0000-0000-000000000e01', 'macro|[email protected]
INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)
VALUES ('00000000-0000-0000-0000-0000000ce001', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW()),
('00000000-0000-0000-0000-0000000ce002', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW()),
- ('00000000-0000-0000-0000-0000000ce003', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW());
+ ('00000000-0000-0000-0000-0000000ce003', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW()),
+ ('00000000-0000-0000-0000-0000000ce004', '00000000-0000-0000-0000-000000000e01', '[email protected]', NOW(), NOW());
INSERT INTO email_labels (id, link_id, provider_label_id, name, created_at)
VALUES ('00000000-0000-0000-0000-0000000be001', '00000000-0000-0000-0000-000000000e01', 'CATEGORY_PROMOTIONS', 'CATEGORY_PROMOTIONS', NOW()),
@@ -37,7 +42,8 @@ VALUES ('00000000-0000-0000-0000-00000000e201', '00000000-0000-0000-0000-0000000
('00000000-0000-0000-0000-00000000e203', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW()),
('00000000-0000-0000-0000-00000000e204', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW()),
('00000000-0000-0000-0000-00000000e205', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW()),
- ('00000000-0000-0000-0000-00000000e206', '00000000-0000-0000-0000-000000000e01', true, true, true, NOW(), NOW());
+ ('00000000-0000-0000-0000-00000000e206', '00000000-0000-0000-0000-000000000e01', true, true, true, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000e207', '00000000-0000-0000-0000-000000000e01', true, false, true, NOW(), NOW());
INSERT INTO email_messages (id, thread_id, link_id, provider_id, global_id, is_sent, from_contact_id, internal_date_ts,
has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
@@ -64,7 +70,10 @@ VALUES ('00000000-0000-0000-0000-00000000e501', '00000000-0000-0000-0000-0000000
false, true, false, false, NOW(), NOW()),
('00000000-0000-0000-0000-00000000e508', '00000000-0000-0000-0000-00000000e206', '00000000-0000-0000-0000-000000000e01',
NULL, NULL, FALSE, '00000000-0000-0000-0000-0000000ce001', '2025-01-05 17:00:00 +00:00',
- false, true, false, true, NOW(), NOW());
+ false, true, false, true, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000e509', '00000000-0000-0000-0000-00000000e207', '00000000-0000-0000-0000-000000000e01',
+ 'provider-msg-e509', 'gid-e509', FALSE, '00000000-0000-0000-0000-0000000ce004', '2025-01-05 18:00:00 +00:00',
+ false, false, false, false, NOW(), NOW());
INSERT INTO email_message_labels (message_id, label_id)
VALUES ('00000000-0000-0000-0000-00000000e502', '00000000-0000-0000-0000-0000000be001'),
diff --git a/crates/email/src/outbound/email_pg_repo/email_filter.rs b/crates/email/src/outbound/email_pg_repo/email_filter.rs
index c4ccf75f43..2d7ab55ab5 100644
--- a/crates/email/src/outbound/email_pg_repo/email_filter.rs
+++ b/crates/email/src/outbound/email_pg_repo/email_filter.rs
@@ -113,7 +113,8 @@ pub async fn delete_email_filter(
/// message from the given sender address or domain. Called after an
/// email_filters change, since the override feeds the signal heuristic.
/// The recompute mirrors sync_thread_signal_flag (thread.rs) / the
-/// Importance(true) predicate in the dynamic query builder.
+/// Importance(true) predicate in the dynamic query builder, including the
+/// unconditional exclusion of Macro's own notification sender (`$4` domain).
#[tracing::instrument(skip(conn), err)]
pub async fn resync_signal_flags_for_sender(
conn: &mut sqlx::PgConnection,
@@ -155,6 +156,11 @@ pub async fn resync_signal_flags_for_sender(
JOIN email_labels l ON ml.label_id = l.id
WHERE ml.message_id = m.id AND l.name = 'TRASH'
)
+ AND NOT EXISTS (
+ SELECT 1 FROM email_contacts sender_c
+ WHERE sender_c.id = m.from_contact_id
+ AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $4
+ )
AND (
(
EXISTS (
@@ -245,6 +251,7 @@ pub async fn resync_signal_flags_for_sender(
link_id,
email_address,
email_domain,
+ email_utils::MACRO_NOTIFICATION_SENDER_DOMAIN,
)
.execute(conn)
.await?;
diff --git a/crates/email/src/outbound/email_pg_repo/test/signal_flag.rs b/crates/email/src/outbound/email_pg_repo/test/signal_flag.rs
index 8aec7ec89e..df036efa24 100644
--- a/crates/email/src/outbound/email_pg_repo/test/signal_flag.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/signal_flag.rs
@@ -93,6 +93,55 @@ async fn update_thread_metadata_syncs_signal_flag(pool: Pool<Postgres>) -> anyho
Ok(())
}
+const THREAD_MACRO_DIGEST: &str = "00000000-0000-0000-0000-00000000e207";
+
+// Macro's own notification emails are never signal: update_thread_metadata
+// clears a stale flag, and neither an address- nor a domain-important
+// override can promote the thread back.
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../fixtures", scripts("email_signal_flag"))
+)]
+async fn macro_notification_sender_is_never_signal(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ assert!(fetch_signal(&pool, THREAD_MACRO_DIGEST).await?);
+
+ let mut tx = pool.begin().await?;
+ super::super::thread::update_thread_metadata(
+ &mut tx,
+ Uuid::parse_str(THREAD_MACRO_DIGEST)?,
+ Uuid::parse_str(LINK_ID)?,
+ )
+ .await?;
+ tx.commit().await?;
+ assert!(!fetch_signal(&pool, THREAD_MACRO_DIGEST).await?);
+
+ let repo = EmailPgRepo::new(pool.clone());
+ let link_id = Uuid::parse_str(LINK_ID)?;
+ repo.upsert_email_filter(
+ link_id,
+ UpsertEmailFilterInput {
+ email_address: Some("[email protected]".to_string()),
+ email_domain: None,
+ is_important: true,
+ },
+ )
+ .await?;
+ assert!(!fetch_signal(&pool, THREAD_MACRO_DIGEST).await?);
+
+ repo.upsert_email_filter(
+ link_id,
+ UpsertEmailFilterInput {
+ email_address: None,
+ email_domain: Some("notification.macro.com".to_string()),
+ is_important: true,
+ },
+ )
+ .await?;
+ assert!(!fetch_signal(&pool, THREAD_MACRO_DIGEST).await?);
+
+ Ok(())
+}
+
const THREAD_DRAFT_SIGNAL: &str = "00000000-0000-0000-0000-00000000e204";
const DRAFT_MSG: &str = "00000000-0000-0000-0000-00000000e505";
diff --git a/crates/email/src/outbound/email_pg_repo/thread.rs b/crates/email/src/outbound/email_pg_repo/thread.rs
index 71554308cc..60384fb3d0 100644
--- a/crates/email/src/outbound/email_pg_repo/thread.rs
+++ b/crates/email/src/outbound/email_pg_repo/thread.rs
@@ -542,6 +542,7 @@ pub(super) async fn update_thread_metadata(
/// thread has a non-TRASH message matching the importance heuristic. Exact
/// copy of `email_db_client::threads::update::sync_thread_signal_flag`,
/// mirroring the Importance(true) predicate in the dynamic query builder.
+/// Macro's own notification emails (`$2` domain) never count as signal.
pub(super) async fn sync_thread_signal_flag(
tx: &mut sqlx::PgConnection,
thread_db_id: Uuid,
@@ -560,6 +561,11 @@ pub(super) async fn sync_thread_signal_flag(
JOIN email_labels l ON ml.label_id = l.id
WHERE ml.message_id = m.id AND l.name = 'TRASH'
)
+ AND NOT EXISTS (
+ SELECT 1 FROM email_contacts sender_c
+ WHERE sender_c.id = m.from_contact_id
+ AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2
+ )
AND (
(
EXISTS (
@@ -642,7 +648,8 @@ pub(super) async fn sync_thread_signal_flag(
WHERE t.id = $1
AND t.is_signal IS DISTINCT FROM calc.sig
"#,
- thread_db_id
+ thread_db_id,
+ email_utils::MACRO_NOTIFICATION_SENDER_DOMAIN,
)
.execute(tx)
.await?;
diff --git a/crates/email_db_client/fixtures/sync_thread_signal_flag.sql b/crates/email_db_client/fixtures/sync_thread_signal_flag.sql
index fdcdecb33e..8820e746b5 100644
--- a/crates/email_db_client/fixtures/sync_thread_signal_flag.sql
+++ b/crates/email_db_client/fixtures/sync_thread_signal_flag.sql
@@ -18,6 +18,11 @@
-- draft is the only signal message; discarding it must flip the flag and
-- deflate the draft-inflated metadata (inbox_visible,
-- latest_inbound_message_ts — see the UPDATE at the bottom).
+-- Thread d209 (flag=true, stale on purpose): unlabeled message from Macro's
+-- own digest sender ([email protected]), which is
+-- explicitly marked important by address — Macro notification emails are
+-- never signal, so sync must clear it despite the label heuristic and the
+-- sender override both saying signal.
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
VALUES ('00000000-0000-0000-0000-000000000d01', 'macro|[email protected]', '00000000-0000-0000-0000-000000000d01',
@@ -27,11 +32,13 @@ INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)
VALUES ('00000000-0000-0000-0000-0000000cd001', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),
('00000000-0000-0000-0000-0000000cd002', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),
('00000000-0000-0000-0000-0000000cd003', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),
- ('00000000-0000-0000-0000-0000000cd004', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW());
+ ('00000000-0000-0000-0000-0000000cd004', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW()),
+ ('00000000-0000-0000-0000-0000000cd005', '00000000-0000-0000-0000-000000000d01', '[email protected]', NOW(), NOW());
INSERT INTO email_filters (id, link_id, email_address, email_domain, is_important, created_at)
VALUES ('00000000-0000-0000-0000-0000000fd001', '00000000-0000-0000-0000-000000000d01', '[email protected]', NULL, true, NOW()),
- ('00000000-0000-0000-0000-0000000fd002', '00000000-0000-0000-0000-000000000d01', NULL, 'corp.com', false, NOW());
+ ('00000000-0000-0000-0000-0000000fd002', '00000000-0000-0000-0000-000000000d01', NULL, 'corp.com', false, NOW()),
+ ('00000000-0000-0000-0000-0000000fd003', '00000000-0000-0000-0000-000000000d01', '[email protected]', NULL, true, NOW());
INSERT INTO email_labels (id, link_id, provider_label_id, name, created_at)
VALUES ('00000000-0000-0000-0000-0000000bd001', '00000000-0000-0000-0000-000000000d01', 'TRASH', 'TRASH', NOW()),
@@ -45,7 +52,8 @@ VALUES ('00000000-0000-0000-0000-00000000d201', '00000000-0000-0000-0000-0000000
('00000000-0000-0000-0000-00000000d205', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),
('00000000-0000-0000-0000-00000000d206', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),
('00000000-0000-0000-0000-00000000d207', '00000000-0000-0000-0000-000000000d01', true, false, false, NOW(), NOW()),
- ('00000000-0000-0000-0000-00000000d208', '00000000-0000-0000-0000-000000000d01', true, false, true, NOW(), NOW());
+ ('00000000-0000-0000-0000-00000000d208', '00000000-0000-0000-0000-000000000d01', true, false, true, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000d209', '00000000-0000-0000-0000-000000000d01', true, false, true, NOW(), NOW());
INSERT INTO email_messages (id, thread_id, link_id, provider_id, global_id, is_sent, from_contact_id, internal_date_ts,
has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
@@ -78,7 +86,10 @@ VALUES ('00000000-0000-0000-0000-00000000d501', '00000000-0000-0000-0000-0000000
false, false, false, false, NOW(), NOW()),
('00000000-0000-0000-0000-00000000d510', '00000000-0000-0000-0000-00000000d208', '00000000-0000-0000-0000-000000000d01',
NULL, NULL, FALSE, '00000000-0000-0000-0000-0000000cd001', '2025-01-05 19:00:00 +00:00',
- false, false, false, true, NOW(), NOW());
+ false, false, false, true, NOW(), NOW()),
+ ('00000000-0000-0000-0000-00000000d511', '00000000-0000-0000-0000-00000000d209', '00000000-0000-0000-0000-000000000d01',
+ 'provider-msg-d511', 'gid-d511', FALSE, '00000000-0000-0000-0000-0000000cd005', '2025-01-05 20:00:00 +00:00',
+ false, false, false, false, NOW(), NOW());
INSERT INTO email_message_labels (message_id, label_id)
VALUES ('00000000-0000-0000-0000-00000000d502', '00000000-0000-0000-0000-0000000bd002'),
diff --git a/crates/email_db_client/src/threads/update.rs b/crates/email_db_client/src/threads/update.rs
index f4386c54e3..c815297ae5 100644
--- a/crates/email_db_client/src/threads/update.rs
+++ b/crates/email_db_client/src/threads/update.rs
@@ -179,6 +179,8 @@ pub async fn sync_thread_calendar_flag(
/// Recomputes the denormalized `email_threads.is_signal` flag: true iff the
/// thread has a non-TRASH message matching the importance heuristic. Mirrors
/// the Importance(true) predicate in the email crate's dynamic query builder.
+/// Macro's own notification emails (`$2` domain) never count as signal,
+/// regardless of labels or sender overrides.
#[tracing::instrument(skip(tx), err)]
pub async fn sync_thread_signal_flag(
tx: &mut sqlx::PgConnection,
@@ -198,6 +200,11 @@ pub async fn sync_thread_signal_flag(
JOIN email_labels l ON ml.label_id = l.id
WHERE ml.message_id = m.id AND l.name = 'TRASH'
)
+ AND NOT EXISTS (
+ SELECT 1 FROM email_contacts sender_c
+ WHERE sender_c.id = m.from_contact_id
+ AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2
+ )
AND (
(
EXISTS (
@@ -280,7 +287,8 @@ pub async fn sync_thread_signal_flag(
WHERE t.id = $1
AND t.is_signal IS DISTINCT FROM calc.sig
"#,
- thread_db_id
+ thread_db_id,
+ email_utils::MACRO_NOTIFICATION_SENDER_DOMAIN,
)
.execute(tx)
.await?;
diff --git a/crates/email_db_client/src/threads/update/test.rs b/crates/email_db_client/src/threads/update/test.rs
index 8f250a1949..19ffbbb6d0 100644
--- a/crates/email_db_client/src/threads/update/test.rs
+++ b/crates/email_db_client/src/threads/update/test.rs
@@ -74,6 +74,7 @@ const SIG_THREAD_TRASH_ONLY: &str = "00000000-0000-0000-0000-00000000d204";
const SIG_THREAD_VIP_ADDRESS: &str = "00000000-0000-0000-0000-00000000d205";
const SIG_THREAD_DOMAIN_MUTED: &str = "00000000-0000-0000-0000-00000000d206";
const SIG_THREAD_DRAFT: &str = "00000000-0000-0000-0000-00000000d207";
+const SIG_THREAD_MACRO_DIGEST: &str = "00000000-0000-0000-0000-00000000d209";
async fn fetch_signal(pool: &Pool<Postgres>, thread_id: &str) -> anyhow::Result<bool> {
Ok(sqlx::query_scalar!(
@@ -171,6 +172,20 @@ async fn signal_domain_override_mutes_unlabeled_message(
Ok(())
}
+// Macro's own notification emails are never signal: the exclusion is
+// case-insensitive on the sender domain and beats both the unlabeled
+// default and an explicit address-important override.
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("sync_thread_signal_flag"))
+)]
+async fn signal_cleared_for_macro_notification_sender(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ assert!(fetch_signal(&pool, SIG_THREAD_MACRO_DIGEST).await?);
+ sync_signal(&pool, SIG_THREAD_MACRO_DIGEST).await?;
+ assert!(!fetch_signal(&pool, SIG_THREAD_MACRO_DIGEST).await?);
+ Ok(())
+}
+
// Drafts are always signal, even when labeled with a depriority category.
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
diff --git a/crates/email_utils/src/lib.rs b/crates/email_utils/src/lib.rs
index aaccf707d5..1225310fe1 100644
--- a/crates/email_utils/src/lib.rs
+++ b/crates/email_utils/src/lib.rs
@@ -1,10 +1,12 @@
pub mod body_parsed;
pub mod body_replyless;
pub mod generic_email;
+pub mod macro_sender;
pub mod normalize_contact;
pub mod sanitizer;
pub mod token_cache_key;
pub use generic_email::{dedupe_emails, is_generic_email};
+pub use macro_sender::{MACRO_NOTIFICATION_SENDER_DOMAIN, is_macro_notification_sender};
pub use normalize_contact::normalize_contact_name;
pub use sanitizer::{sanitize_authored_html, sanitize_email_html, sanitize_html_fragment};
diff --git a/crates/email_utils/src/macro_sender.rs b/crates/email_utils/src/macro_sender.rs
new file mode 100644
index 0000000000..971f770453
--- /dev/null
+++ b/crates/email_utils/src/macro_sender.rs
@@ -0,0 +1,23 @@
+//! Recognition of Macro's own outbound notification sender.
+//!
+//! Macro's notification service sends digest, invite, and other system
+//! emails from `no-reply[-<env>]@notification.macro.com` (the SES identity
+//! the notification service is allowed to send from). Users receive those
+//! about activity they already see in-app, so the email pipeline treats
+//! them as noise and never raises a new-email notification for them.
+
+#[cfg(test)]
+mod test;
+
+/// Domain every Macro notification email is sent from, across environments
+/// (`no-reply@`, `no-reply-dev@`, ...). Lowercase; compare case-insensitively.
+pub const MACRO_NOTIFICATION_SENDER_DOMAIN: &str = "notification.macro.com";
+
+/// Whether `email` is one of Macro's own notification senders, i.e. any
+/// address at [`MACRO_NOTIFICATION_SENDER_DOMAIN`].
+pub fn is_macro_notification_sender(email: &str) -> bool {
+ email
+ .trim()
+ .rsplit_once('@')
+ .is_some_and(|(_, domain)| domain.eq_ignore_ascii_case(MACRO_NOTIFICATION_SENDER_DOMAIN))
+}
diff --git a/crates/email_utils/src/macro_sender/test.rs b/crates/email_utils/src/macro_sender/test.rs
new file mode 100644
index 0000000000..93f2ddf0fd
--- /dev/null
+++ b/crates/email_utils/src/macro_sender/test.rs
@@ -0,0 +1,41 @@
+use super::is_macro_notification_sender;
+
+#[test]
+fn matches_every_environment_prefix() {
+ assert!(is_macro_notification_sender(
+ ));
+ assert!(is_macro_notification_sender(
+ ));
+ assert!(is_macro_notification_sender(
+ ));
+}
+
+#[test]
+fn is_case_and_whitespace_insensitive() {
+ assert!(is_macro_notification_sender(
+ ));
+ assert!(is_macro_notification_sender(
+ ));
+}
+
+#[test]
+fn rejects_other_macro_and_lookalike_domains() {
+ assert!(!is_macro_notification_sender("[email protected]"));
+ assert!(!is_macro_notification_sender("[email protected]"));
+ assert!(!is_macro_notification_sender(
+ ));
+ assert!(!is_macro_notification_sender(
+ ));
+ assert!(!is_macro_notification_sender(
+ ));
+ assert!(!is_macro_notification_sender("notification.macro.com"));
+ assert!(!is_macro_notification_sender(""));
+}
diff --git a/crates/opensearch_client/src/search/emails.rs b/crates/opensearch_client/src/search/emails.rs
index 32c885d36a..228684d5bd 100644
--- a/crates/opensearch_client/src/search/emails.rs
+++ b/crates/opensearch_client/src/search/emails.rs
@@ -8,7 +8,9 @@ use crate::{
};
use models_opensearch::OpenSearchEntityType;
-use opensearch_query_builder::{BoolQuery, BoolQueryBuilder, QueryType, SimpleQueryStringQuery};
+use opensearch_query_builder::{
+ BoolQuery, BoolQueryBuilder, QueryType, SimpleQueryStringQuery, WildcardQuery,
+};
pub(crate) struct EmailSearchConfig;
@@ -39,6 +41,23 @@ const EMAIL_KEYWORD_FIELDS: &[&str] = &[
"bcc.parts",
];
+/// Domain Macro's own notification/digest emails are sent from. Those are
+/// never signal, mirroring the Postgres `is_signal` heuristic. Kept in sync
+/// with `email_utils::MACRO_NOTIFICATION_SENDER_DOMAIN`, which is not imported
+/// so this crate stays free of the email HTML tooling that crate pulls in.
+const MACRO_NOTIFICATION_SENDER_DOMAIN: &str = "notification.macro.com";
+
+/// Matches messages whose `sender` is any address at
+/// [`MACRO_NOTIFICATION_SENDER_DOMAIN`].
+fn macro_notification_sender_query<'a>() -> QueryType<'a> {
+ QueryType::WildCard(WildcardQuery::new(
+ "sender",
+ format!("*@{MACRO_NOTIFICATION_SENDER_DOMAIN}"),
+ true,
+ None,
+ ))
+}
+
/// Text-analyzed fields. Matched as a prefix so `scri` matches `script`.
const EMAIL_TEXT_FIELDS: &[&str] = &[
"subject",
@@ -304,6 +323,7 @@ impl EmailQueryBuilder {
// Importance filter. Source of truth for the label logic is in
// email/src/outbound/email_pg_repo/dynamic.rs (EmailLiteral::Importance).
+ // Macro's own notification emails are always noise on top of that.
match self.importance {
Some(true) => {
// Exclude emails that have depriority labels UNLESS they also have a priority label.
@@ -322,10 +342,12 @@ impl EmailQueryBuilder {
["CATEGORY_PERSONAL", "SENT", "DRAFT"],
));
content_bool_query.must_not(QueryType::Bool(importance_exclude));
+ content_bool_query.must_not(macro_notification_sender_query());
}
Some(false) => {
// Only show deprioritized emails: must have a depriority label
- // AND must not have a priority label.
+ // AND must not have a priority label, or come from Macro's
+ // own notification sender.
let depriority_filter = BoolQuery::new()
.filter(QueryType::terms(
"labels",
@@ -340,7 +362,11 @@ impl EmailQueryBuilder {
"labels",
["CATEGORY_PERSONAL", "SENT", "DRAFT"],
));
- content_bool_query.filter(QueryType::Bool(depriority_filter));
+ let noise_filter = BoolQuery::new()
+ .should(QueryType::Bool(depriority_filter))
+ .should(macro_notification_sender_query())
+ .minimum_should_match(1);
+ content_bool_query.filter(QueryType::Bool(noise_filter));
}
None => {}
}
diff --git a/crates/opensearch_client/src/search/emails/test.rs b/crates/opensearch_client/src/search/emails/test.rs
index 91649d0698..2699b1afed 100644
--- a/crates/opensearch_client/src/search/emails/test.rs
+++ b/crates/opensearch_client/src/search/emails/test.rs
@@ -1,5 +1,6 @@
use super::*;
use opensearch_query_builder::ToOpenSearchJson;
+use serde_json::json;
#[test]
fn test_build_keyword_query_string_single_term() {
@@ -331,7 +332,7 @@ fn test_importance_true_excludes_depriority_unless_priority() -> anyhow::Result<
let json = bool_query.build().to_json();
let must_not = json["bool"]["must_not"].as_array().unwrap();
- assert_eq!(must_not.len(), 1);
+ assert_eq!(must_not.len(), 2);
let nested_bool = &must_not[0]["bool"];
let filter = nested_bool["filter"].as_array().unwrap();
@@ -344,6 +345,16 @@ fn test_importance_true_excludes_depriority_unless_priority() -> anyhow::Result<
let priority = inner_must_not[0]["terms"]["labels"].as_array().unwrap();
assert_eq!(priority.len(), 3);
+ // Macro's own notification sender is excluded from signal outright.
+ assert_eq!(
+ must_not[1],
+ json!({
+ "wildcard": {
+ "sender": { "value": "*@notification.macro.com", "case_insensitive": true }
+ }
+ })
+ );
+
Ok(())
}
@@ -361,21 +372,36 @@ fn test_importance_false_filters_to_depriority_only() -> anyhow::Result<()> {
let json = bool_query.build().to_json();
let filters = json["bool"]["filter"].as_array().unwrap();
- let nested_bool = filters
+ let noise_bool = filters
.iter()
- .find(|f| f["bool"]["filter"].is_array() && f["bool"]["must_not"].is_array())
+ .find(|f| f["bool"]["should"].is_array())
.expect("should have nested importance filter");
+ assert_eq!(noise_bool["bool"]["minimum_should_match"], 1);
+
+ let should = noise_bool["bool"]["should"].as_array().unwrap();
+ assert_eq!(should.len(), 2);
- let depriority = nested_bool["bool"]["filter"][0]["terms"]["labels"]
+ // Depriority-labeled without a priority label...
+ let depriority_bool = &should[0]["bool"];
+ let depriority = depriority_bool["filter"][0]["terms"]["labels"]
.as_array()
.unwrap();
assert_eq!(depriority.len(), 4);
-
- let priority = nested_bool["bool"]["must_not"][0]["terms"]["labels"]
+ let priority = depriority_bool["must_not"][0]["terms"]["labels"]
.as_array()
.unwrap();
assert_eq!(priority.len(), 3);
+ // ...or from Macro's own notification sender.
+ assert_eq!(
+ should[1],
+ json!({
+ "wildcard": {
+ "sender": { "value": "*@notification.macro.com", "case_insensitive": true }
+ }
+ })
+ );
+
Ok(())
}
@@ -412,8 +438,9 @@ fn test_importance_true_with_exclude_labels_both_apply() -> anyhow::Result<()> {
let json = bool_query.build().to_json();
let must_not = json["bool"]["must_not"].as_array().unwrap();
- // Should have both: the explicit exclude_label AND the importance nested bool
- assert_eq!(must_not.len(), 2);
+ // Should have all three: the explicit exclude_label, the importance
+ // nested bool, and the Macro notification sender exclusion.
+ assert_eq!(must_not.len(), 3);
// One should be the INBOX term exclusion
let has_inbox_exclusion = must_not.iter().any(|q| q["term"]["labels"] == "INBOX");
@@ -449,9 +476,7 @@ fn test_importance_false_with_exclude_labels_both_apply() -> anyhow::Result<()>
// Should have the importance filter in filter array
let filters = json["bool"]["filter"].as_array().unwrap();
- let has_importance_filter = filters
- .iter()
- .any(|f| f["bool"]["filter"].is_array() && f["bool"]["must_not"].is_array());
+ let has_importance_filter = filters.iter().any(|f| f["bool"]["should"].is_array());
assert!(has_importance_filter);
Ok(())
diff --git a/services/email_service/src/bin/backfill_signal_flags/process.rs b/services/email_service/src/bin/backfill_signal_flags/process.rs
index 67932e1e99..f43e61d3d9 100644
--- a/services/email_service/src/bin/backfill_signal_flags/process.rs
+++ b/services/email_service/src/bin/backfill_signal_flags/process.rs
@@ -52,7 +52,8 @@ pub async fn process_macro_id(
}
// Mirrors sync_thread_signal_flag (email_db_client/threads/update.rs)
- // and the Importance(true) predicate in the dynamic query builder.
+ // and the Importance(true) predicate in the dynamic query builder,
+ // including the exclusion of Macro's own notification sender ($2).
let flagged = sqlx::query!(
r#"
UPDATE email_threads t
@@ -66,6 +67,11 @@ pub async fn process_macro_id(
JOIN email_labels l ON ml.label_id = l.id
WHERE ml.message_id = m.id AND l.name = 'TRASH'
)
+ AND NOT EXISTS (
+ SELECT 1 FROM email_contacts sender_c
+ WHERE sender_c.id = m.from_contact_id
+ AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2
+ )
AND (
(
EXISTS (
@@ -147,7 +153,8 @@ pub async fn process_macro_id(
WHERE t.id = sig.thread_id
AND NOT t.is_signal
"#,
- link_id
+ link_id,
+ email_utils::MACRO_NOTIFICATION_SENDER_DOMAIN,
)
.execute(&mut *tx)
.await?
@@ -187,6 +194,11 @@ async fn verify_link(pool: &sqlx::PgPool, link_id: Uuid) -> anyhow::Result<u64>
JOIN email_labels l ON ml.label_id = l.id
WHERE ml.message_id = m.id AND l.name = 'TRASH'
)
+ AND NOT EXISTS (
+ SELECT 1 FROM email_contacts sender_c
+ WHERE sender_c.id = m.from_contact_id
+ AND LOWER(SPLIT_PART(sender_c.email_address, '@', 2)) = $2
+ )
AND (
(
EXISTS (
@@ -266,7 +278,8 @@ async fn verify_link(pool: &sqlx::PgPool, link_id: Uuid) -> anyhow::Result<u64>
)
)
"#,
- link_id
+ link_id,
+ email_utils::MACRO_NOTIFICATION_SENDER_DOMAIN,
)
.fetch_one(pool)
.await?;
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
index cf84afcb8f..4bb8475d7a 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
@@ -19,7 +19,7 @@ use email_db_client::attachments::provider::upload_filters::{
attachment_is_document, attachment_is_media,
};
use email_db_client::threads;
-use email_utils::dedupe_emails;
+use email_utils::{dedupe_emails, is_macro_notification_sender};
use filter_ast::Expr;
use item_filters::{SharedEmailFilter, ast::email::EmailLiteral};
use macro_user_id::user_id::MacroUserIdStr;
@@ -678,9 +678,7 @@ async fn send_notifications(
return Ok(());
}
- let Some((message, tier)) =
- filter_notifiable_message(ctx, link, new_message_provider_id).await?
- else {
+ let Some(message) = filter_notifiable_message(ctx, link, new_message_provider_id).await? else {
return Ok(());
};
@@ -700,6 +698,21 @@ async fn send_notifications(
.from_contact_id
.and_then(|from_id| sender_contacts.get(&from_id));
+ // Macro's own digest / system emails describe activity the user already
+ // has in-app notifications for; never raise a new-email notification
+ // (in-app row or push) on top of them.
+ if sender_contact.is_some_and(|contact| is_macro_notification_sender(&contact.email)) {
+ tracing::debug!(
+ thread_id = %message.thread_db_id,
+ "skipping new-email notification for Macro's own notification sender"
+ );
+ return Ok(());
+ }
+
+ let Some(tier) = new_email_tier(ctx, link, message.thread_db_id).await? else {
+ return Ok(());
+ };
+
let sender = sender_contact.map(|contact| {
contact
.name
@@ -865,12 +878,15 @@ async fn new_email_tier(
Ok(staff_inbox.then_some(NewEmailTier::StaffInbox))
}
+/// Loads the newly upserted message, returning `None` for messages that never
+/// notify (sent or draft). Sender- and tier-based gating happens in
+/// `send_notifications`.
#[tracing::instrument(skip(ctx, link))]
async fn filter_notifiable_message(
ctx: &PubSubContext,
link: &link::Link,
new_message_provider_id: &str,
-) -> result::Result<Option<(SimpleMessage, NewEmailTier)>, ProcessingError> {
+) -> result::Result<Option<SimpleMessage>, ProcessingError> {
let new_message =
email_db_client::messages::get_simple_messages::get_simple_message_by_provider_and_link(
&ctx.db,
@@ -893,6 +909,5 @@ async fn filter_notifiable_message(
return Ok(None);
}
- let tier = new_email_tier(ctx, link, new_message.thread_db_id).await?;
- Ok(tier.map(|tier| (new_message, tier)))
+ Ok(Some(new_message))
}