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.

8.2 KB
Raw Blame
{
  "db_name": "PostgreSQL",
  "query": "\n        SELECT\n            t.id AS \"id!\",\n            t.provider_id,\n            TRUE AS \"inbox_visible!\",\n            t.is_read AS \"is_read!\",\n            t.is_signal AS \"is_signal!\",\n            t.effective_ts AS \"sort_ts!\",\n            t.created_at AS \"created_at!\",\n            t.updated_at AS \"updated_at!\",\n            t.project_id,\n            uh.updated_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_mailbox_facts mf ON mf.id = m_imp.id\n                    WHERE m_imp.thread_id = t.id AND mf.is_present AND NOT mf.in_trash AND mf.provider_is_important\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            el.macro_id AS \"owner_id!\",\n            el.id AS \"link_id!\"\n        FROM (\n            -- Step 1: Find the top N+1 candidate threads. $5 picks exactly one\n            -- of the two branches below.\n            SELECT *\n            FROM (\n                -- Recency sorts: each link walks idx_email_threads_inbox_reminder_sort\n                -- in order and stops at LIMIT. Before Postgres 17, `link_id = ANY(...)`\n                -- cannot return index order, so it would read and sort every inbox\n                -- thread of every link.\n                SELECT per_link.*\n                FROM unnest($1::uuid[]) AS links(link_id)\n                CROSS JOIN LATERAL (\n                    SELECT\n                        t.id,\n                        t.provider_id,\n                        t.link_id,\n                        t.is_read,\n                        t.is_signal,\n                        t.project_id,\n                        COALESCE(t.latest_inbound_message_ts, t.latest_outbound_message_ts, t.created_at) AS created_at,\n                        GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS updated_at,\n                        GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS effective_ts\n                    FROM email_threads t\n                    WHERE\n                        t.link_id = links.link_id\n                      AND t.inbox_visible = TRUE\n                      AND GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) IS NOT NULL\n                      AND (($3::timestamptz IS NULL) OR (\n                          GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at), t.id\n                      ) < ($3::timestamptz, $4::uuid))\n                    ORDER BY GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) DESC, t.id DESC\n                    LIMIT $2\n                ) AS per_link\n                WHERE $5 NOT IN ('viewed_at', 'viewed_updated')\n\n                UNION ALL\n\n                -- Viewed sorts order by the caller's history, so every inbox thread\n                -- is a candidate.\n                SELECT\n                    t.id,\n                    t.provider_id,\n                    t.link_id,\n                    t.is_read,\n                    t.is_signal,\n                    t.project_id,\n                    COALESCE(t.latest_inbound_message_ts, t.latest_outbound_message_ts, t.created_at) AS created_at,\n                    GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS updated_at,\n                    CASE $5 -- sort_method_str\n                        WHEN 'viewed_at' THEN COALESCE(uh.updated_at, '1970-01-01 00:00:00+00')\n                        ELSE COALESCE(uh.updated_at, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))\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 = ANY($1)\n                  AND t.inbox_visible = TRUE\n                  AND GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) IS NOT NULL\n                  AND $5 IN ('viewed_at', 'viewed_updated')\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                          ELSE COALESCE(uh.updated_at, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))\n                      END, t.id\n                  ) < ($3::timestamptz, $4::uuid))\n            ) AS candidates\n            ORDER BY effective_ts DESC, id DESC\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 EXISTS (SELECT 1 FROM email_message_mailbox_facts mf WHERE mf.id = m.id AND mf.is_present AND NOT mf.in_trash)\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        LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = t.link_id\n        JOIN email_links el ON t.link_id = el.id\n        -- Final ordering is preserved because the input `t` is already sorted.\n        ORDER BY t.effective_ts DESC, t.id 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": "is_signal!",
        "type_info": "Bool"
      },
      {
        "ordinal": 5,
        "name": "sort_ts!",
        "type_info": "Timestamptz"
      },
      {
        "ordinal": 6,
        "name": "created_at!",
        "type_info": "Timestamptz"
      },
      {
        "ordinal": 7,
        "name": "updated_at!",
        "type_info": "Timestamptz"
      },
      {
        "ordinal": 8,
        "name": "project_id",
        "type_info": "Text"
      },
      {
        "ordinal": 9,
        "name": "viewed_at?",
        "type_info": "Timestamptz"
      },
      {
        "ordinal": 10,
        "name": "name?",
        "type_info": "Text"
      },
      {
        "ordinal": 11,
        "name": "snippet?",
        "type_info": "Text"
      },
      {
        "ordinal": 12,
        "name": "is_draft",
        "type_info": "Bool"
      },
      {
        "ordinal": 13,
        "name": "is_important!",
        "type_info": "Bool"
      },
      {
        "ordinal": 14,
        "name": "sender_email?",
        "type_info": "Varchar"
      },
      {
        "ordinal": 15,
        "name": "sender_name?",
        "type_info": "Varchar"
      },
      {
        "ordinal": 16,
        "name": "sender_photo_url?",
        "type_info": "Text"
      },
      {
        "ordinal": 17,
        "name": "owner_id!",
        "type_info": "Text"
      },
      {
        "ordinal": 18,
        "name": "link_id!",
        "type_info": "Uuid"
      }
    ],
    "parameters": {
      "Left": [
        "UuidArray",
        "Int8",
        "Timestamptz",
        "Uuid",
        "Text"
      ]
    },
    "nullable": [
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      false,
      true,
      true,
      false,
      null,
      false,
      null,
      true,
      false,
      false
    ]
  },
  "hash": "9a310b8cb7f562fdb5990bb86887deea2edc59705399cd442b6707d43da34eef"
}