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.

perf(email): walk each inbox in index order for recency previews (#8453)

teoc98962d1d1d8
diff --git a/.sqlx/query-9a310b8cb7f562fdb5990bb86887deea2edc59705399cd442b6707d43da34eef.json b/.sqlx/query-9a310b8cb7f562fdb5990bb86887deea2edc59705399cd442b6707d43da34eef.json
new file mode 100644
index 0000000000..a5d67af9e6
--- /dev/null
+++ b/.sqlx/query-9a310b8cb7f562fdb5990bb86887deea2edc59705399cd442b6707d43da34eef.json
@@ -0,0 +1,134 @@
+{
+ "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"
+}
diff --git a/.sqlx/query-f25e2549042b4afb2de5d96fd417e94e0e4b1ba340d652343f3a0cad4cecebb6.json b/.sqlx/query-f25e2549042b4afb2de5d96fd417e94e0e4b1ba340d652343f3a0cad4cecebb6.json
deleted file mode 100644
index f5ece97b12..0000000000
--- a/.sqlx/query-f25e2549042b4afb2de5d96fd417e94e0e4b1ba340d652343f3a0cad4cecebb6.json
+++ /dev/null
@@ -1,134 +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.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 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_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: 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.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 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, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))\n ELSE 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 (t.latest_inbound_message_ts IS NOT NULL OR t.reminder_returned_at 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, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))\n ELSE GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at)\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 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 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.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": "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": [
- false,
- true,
- null,
- false,
- false,
- null,
- null,
- null,
- true,
- false,
- true,
- true,
- false,
- null,
- false,
- null,
- true,
- false,
- false
- ]
- },
- "hash": "f25e2549042b4afb2de5d96fd417e94e0e4b1ba340d652343f3a0cad4cecebb6"
-}
diff --git a/crates/email/src/outbound/email_pg_repo/preview_views/new_inbox.rs b/crates/email/src/outbound/email_pg_repo/preview_views/new_inbox.rs
index 2527e210fd..b2d63830fc 100644
--- a/crates/email/src/outbound/email_pg_repo/preview_views/new_inbox.rs
+++ b/crates/email/src/outbound/email_pg_repo/preview_views/new_inbox.rs
@@ -22,16 +22,16 @@ pub(crate) async fn new_inbox_preview_cursor(
ThreadPreviewCursorDbRow,
r#"
SELECT
- t.id,
+ t.id AS "id!",
t.provider_id,
TRUE AS "inbox_visible!",
- t.is_read,
- t.is_signal,
+ t.is_read AS "is_read!",
+ t.is_signal AS "is_signal!",
t.effective_ts AS "sort_ts!",
t.created_at AS "created_at!",
t.updated_at AS "updated_at!",
t.project_id,
- t.viewed_at AS "viewed_at?",
+ uh.updated_at AS "viewed_at?",
lmp.subject AS "name?",
lmp.snippet AS "snippet?",
lmp.is_draft,
@@ -47,40 +47,73 @@ pub(crate) async fn new_inbox_preview_cursor(
el.macro_id AS "owner_id!",
el.id AS "link_id!"
FROM (
- -- Step 1: Efficiently find and sort ONLY the top N+1 candidate threads.
- -- This subquery is fast as it only touches `threads` and `user_history`.
- SELECT
- t.id,
- t.provider_id,
- t.link_id,
- t.is_read,
- t.is_signal,
- t.project_id,
- COALESCE(t.latest_inbound_message_ts, t.latest_outbound_message_ts, t.created_at) AS created_at,
- GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS updated_at,
- uh.updated_at AS viewed_at,
- CASE $5 -- sort_method_str
- WHEN 'viewed_at' THEN COALESCE(uh."updated_at", '1970-01-01 00:00:00+00')
- WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))
- ELSE GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at)
- END AS effective_ts
- FROM email_threads t
- LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = t.link_id
- WHERE
- t.link_id = ANY($1)
- AND t.inbox_visible = TRUE
- AND (t.latest_inbound_message_ts IS NOT NULL OR t.reminder_returned_at IS NOT NULL)
+ -- Step 1: Find the top N+1 candidate threads. $5 picks exactly one
+ -- of the two branches below.
+ SELECT *
+ FROM (
+ -- Recency sorts: each link walks idx_email_threads_inbox_reminder_sort
+ -- in order and stops at LIMIT. Before Postgres 17, `link_id = ANY(...)`
+ -- cannot return index order, so it would read and sort every inbox
+ -- thread of every link.
+ SELECT per_link.*
+ FROM unnest($1::uuid[]) AS links(link_id)
+ CROSS JOIN LATERAL (
+ SELECT
+ t.id,
+ t.provider_id,
+ t.link_id,
+ t.is_read,
+ t.is_signal,
+ t.project_id,
+ COALESCE(t.latest_inbound_message_ts, t.latest_outbound_message_ts, t.created_at) AS created_at,
+ GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS updated_at,
+ GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS effective_ts
+ FROM email_threads t
+ WHERE
+ t.link_id = links.link_id
+ AND t.inbox_visible = TRUE
+ AND GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) IS NOT NULL
+ AND (($3::timestamptz IS NULL) OR (
+ GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at), t.id
+ ) < ($3::timestamptz, $4::uuid))
+ ORDER BY GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) DESC, t.id DESC
+ LIMIT $2
+ ) AS per_link
+ WHERE $5 NOT IN ('viewed_at', 'viewed_updated')
+
+ UNION ALL
- -- The cursor logic is moved inside this subquery for maximum efficiency.
- AND (($3::timestamptz IS NULL) OR (
- -- This CASE must exactly match the one that defines `effective_ts`
- CASE $5 -- sort_method_str
- WHEN 'viewed_at' THEN COALESCE(uh."updated_at", '1970-01-01 00:00:00+00')
- WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))
- ELSE GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at)
- END, t.id
- ) < ($3::timestamptz, $4::uuid))
- ORDER BY effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same
+ -- Viewed sorts order by the caller's history, so every inbox thread
+ -- is a candidate.
+ SELECT
+ t.id,
+ t.provider_id,
+ t.link_id,
+ t.is_read,
+ t.is_signal,
+ t.project_id,
+ COALESCE(t.latest_inbound_message_ts, t.latest_outbound_message_ts, t.created_at) AS created_at,
+ GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) AS updated_at,
+ CASE $5 -- sort_method_str
+ WHEN 'viewed_at' THEN COALESCE(uh.updated_at, '1970-01-01 00:00:00+00')
+ ELSE COALESCE(uh.updated_at, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))
+ END AS effective_ts
+ FROM email_threads t
+ LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = t.link_id
+ WHERE
+ t.link_id = ANY($1)
+ AND t.inbox_visible = TRUE
+ AND GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at) IS NOT NULL
+ AND $5 IN ('viewed_at', 'viewed_updated')
+ AND (($3::timestamptz IS NULL) OR (
+ -- This CASE must exactly match the one that defines `effective_ts`
+ CASE $5 -- sort_method_str
+ WHEN 'viewed_at' THEN COALESCE(uh.updated_at, '1970-01-01 00:00:00+00')
+ ELSE COALESCE(uh.updated_at, GREATEST(t.latest_inbound_message_ts, t.reminder_returned_at))
+ END, t.id
+ ) < ($3::timestamptz, $4::uuid))
+ ) AS candidates
+ ORDER BY effective_ts DESC, id DESC
LIMIT $2
) AS t
-- Step 2: For EACH of the limited threads from above, find its latest non-trashed message.
@@ -99,9 +132,10 @@ pub(crate) async fn new_inbox_preview_cursor(
) AS lmp
-- Step 3: Join to get the sender's details for the final result set.
LEFT JOIN email_contacts c ON lmp.from_contact_id = c.id
+ LEFT JOIN email_user_history uh ON uh.thread_id = t.id AND uh.link_id = t.link_id
JOIN email_links el ON t.link_id = el.id
-- Final ordering is preserved because the input `t` is already sorted.
- ORDER BY t.effective_ts DESC, t.updated_at DESC -- fall back to updated_at if effective_ts is the same
+ ORDER BY t.effective_ts DESC, t.id DESC
"#,
link_ids, // $1
query_limit, // $2
diff --git a/crates/email/src/outbound/email_pg_repo/test/preview.rs b/crates/email/src/outbound/email_pg_repo/test/preview.rs
index 1b5a66cf40..a653a1666d 100644
--- a/crates/email/src/outbound/email_pg_repo/test/preview.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/preview.rs
@@ -263,3 +263,74 @@ async fn returned_thread_without_message_timestamps_has_created_at(
assert_eq!(returned.created_at, created);
Ok(())
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../../fixtures",
+ scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
+ )
+)]
+async fn inbox_pages_through_tied_threads_across_links(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ // Threads 4 and 5 (first inbox) and 201 (second inbox) share a timestamp,
+ // and their updated_at runs opposite to their ids.
+ sqlx::query(
+ "UPDATE email_threads
+ SET inbox_visible = true,
+ latest_inbound_message_ts = '2024-01-12 07:00:00+00',
+ updated_at = CASE id
+ WHEN '20000004-0000-0000-0000-000000000004' THEN '2024-03-03 00:00:00+00'::timestamptz
+ WHEN '20000005-0000-0000-0000-000000000005' THEN '2024-03-02 00:00:00+00'::timestamptz
+ ELSE '2024-03-01 00:00:00+00'::timestamptz
+ END
+ WHERE id IN (
+ '20000004-0000-0000-0000-000000000004',
+ '20000005-0000-0000-0000-000000000005',
+ '20000201-0000-0000-0000-000000000201'
+ )",
+ )
+ .execute(&pool)
+ .await?;
+ let link_ids = [
+ Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
+ Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
+ ];
+
+ let mut ids = Vec::new();
+ let mut query = Query::new(None, SimpleSortMethod::UpdatedAt, ());
+ loop {
+ let page =
+ crate::outbound::email_pg_repo::preview_views::new_inbox::new_inbox_preview_cursor(
+ &pool, &link_ids, 2, &query,
+ )
+ .await?;
+ let Some(last) = page.last() else { break };
+ query = Query::new(
+ Some(Cursor {
+ id: last.id,
+ limit: 2,
+ val: CursorVal {
+ sort_type: SimpleSortMethod::UpdatedAt,
+ last_val: last.sort_ts,
+ },
+ filter: (),
+ }),
+ SimpleSortMethod::UpdatedAt,
+ (),
+ );
+ ids.extend(page.iter().map(|row| row.id.to_string()));
+ }
+
+ assert_eq!(
+ ids,
+ vec![
+ "20000001-0000-0000-0000-000000000001",
+ "20000201-0000-0000-0000-000000000201",
+ "20000005-0000-0000-0000-000000000005",
+ "20000004-0000-0000-0000-000000000004",
+ "20000007-0000-0000-0000-000000000007",
+ ],
+ "each inbox thread appears once, newest first, ties broken by id"
+ );
+ Ok(())
+}