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.

5.8 KB
Raw Blame
{
  "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                    JOIN email_message_mailbox_facts facts ON facts.id = m.id\n                    WHERE m.thread_id = a.thread_id AND facts.is_present\n                      AND NOT facts.in_trash\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 facts.provider_is_primary\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": "f60e892eefe6adcfe3ed4bf72226f307c4ab9f108a02df5ba741904e698c363b"
}