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.

fix(email): cover all linked inboxes in the sent view (#4512)

Evan Hutnika3f18158154e
diff --git a/js/app/packages/app/component/app-sidebar/soup-filter-presets.ts b/js/app/packages/app/component/app-sidebar/soup-filter-presets.ts
index 61d52eb0bc..b448b1f5c6 100644
--- a/js/app/packages/app/component/app-sidebar/soup-filter-presets.ts
+++ b/js/app/packages/app/component/app-sidebar/soup-filter-presets.ts
@@ -28,7 +28,6 @@ type SoupFiltersPreset = {
// Tab preset configuration types
export type PresetContext = {
userId: string | undefined;
- email: string | undefined;
/** True iff the current user has admin/owner team role. Drives
* visibility of admin-only tabs (e.g. companies → hidden). */
isTeamAdmin: boolean;
@@ -238,16 +237,15 @@ export const VIEW_TAB_PRESETS: Record<ListView, ViewTabConfig> = {
}),
clientFilters: { and: ['email-drafts'] },
}),
- sent: (ctx) => {
- if (!ctx.email) return undefined;
- return {
- filters: defineQueryFilters({
- include: { emailSender: [ctx.email] },
- emailView: 'sent',
- }),
- clientFilters: { and: ['email', 'no-drafts'] },
- };
- },
+ // No sender filter: the 'sent' view already scopes to messages with
+ // is_sent = TRUE per linked inbox, which covers multi-inbox correctly
+ // (a single sender address would drop secondary inboxes' sent mail).
+ sent: () => ({
+ filters: defineQueryFilters({
+ emailView: 'sent',
+ }),
+ clientFilters: { and: ['email', 'no-drafts'] },
+ }),
shared: () => ({
filters: defineQueryFilters({
include: { emailShared: 'only' },
@@ -527,7 +525,6 @@ export function getViewPreset(
const presetCtx: PresetContext = ctx ?? {
userId: undefined,
- email: undefined,
isTeamAdmin: false,
};
const resolved = resolver(presetCtx);
diff --git a/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts b/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts
index 5e9991c075..821156b6ec 100644
--- a/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts
+++ b/js/app/packages/app/component/next-soup/filters/filter-store/compile.test.ts
@@ -1,7 +1,28 @@
import { describe, expect, it } from 'vitest';
-import { compileToAst, queryStateFrom } from './compile';
+import { compileToAst, defineQueryFilters, queryStateFrom } from './compile';
import type { DocumentFilterExpression, QueryState } from './types';
+const NIL_UUID = '00000000-0000-0000-0000-000000000000';
+
+describe('defineQueryFilters', () => {
+ it('treats emailView as referencing the email target', () => {
+ const query = defineQueryFilters({ emailView: 'sent' });
+
+ // No match-nothing filter on the email target itself...
+ expect(query.include?.threadId).toBeUndefined();
+ // ...while other entity targets are still excluded.
+ expect(query.include?.documentId).toEqual([NIL_UUID]);
+ expect(query.include?.chatId).toEqual([NIL_UUID]);
+ });
+
+ it('stuffs match-nothing filters on all targets when nothing is referenced', () => {
+ const query = defineQueryFilters({});
+
+ expect(query.include?.threadId).toEqual([NIL_UUID]);
+ expect(query.include?.documentId).toEqual([NIL_UUID]);
+ });
+});
+
describe('compileToAst', () => {
it('keeps existing flat include and exclude document filters unchanged', () => {
const state: QueryState = {
diff --git a/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts b/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts
index 1e48e9d771..1f708668f5 100644
--- a/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts
+++ b/js/app/packages/app/component/next-soup/filters/filter-store/compile.ts
@@ -436,6 +436,12 @@ const extractQueryTargets = (query: Query): QueryTarget[] => {
targets.add('df');
}
+ // An email view scopes the query to emails even when no ef field is set,
+ // so don't stuff the match-nothing threadId filter onto the email target.
+ if (query.emailView) {
+ targets.add('ef');
+ }
+
for (const field of Object.keys(query.include ?? {})) {
if (field in FIELD_CONFIG) {
targets.add(FIELD_CONFIG[field as CompiledFieldName].target);
diff --git a/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx b/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx
index 3a96ff0768..b05a0b71ee 100644
--- a/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx
+++ b/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx
@@ -82,7 +82,6 @@ export function useFilterRefinements() {
const getPresetContext = (): PresetContext => ({
userId: user.userId(),
- email: user.email(),
// Filter refinements don't surface admin-gated tabs, so passing
// false here is safe — the value only matters where the resolver
// gates on it (companies → hidden).
diff --git a/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx b/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx
index 8c00cbd1b6..eb0701e0ea 100644
--- a/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx
+++ b/js/app/packages/app/component/next-soup/soup-view/soup-view-tabs.tsx
@@ -120,7 +120,6 @@ export const useApplyPreset = () => {
const getPresetContext = (): PresetContext => ({
userId: user.userId(),
- email: user.email(),
isTeamAdmin: isTeamAdmin(),
});
diff --git a/js/app/packages/app/component/split-layout/componentRegistry.tsx b/js/app/packages/app/component/split-layout/componentRegistry.tsx
index 7216c274c8..4b61066d7d 100644
--- a/js/app/packages/app/component/split-layout/componentRegistry.tsx
+++ b/js/app/packages/app/component/split-layout/componentRegistry.tsx
@@ -159,7 +159,6 @@ registerComponent(
const user = useUserContext();
const preset = getViewPreset('agents', undefined, {
userId: user.userId(),
- email: user.email(),
isTeamAdmin: false,
});
const automationEntities = useAutomationEntities();
@@ -198,7 +197,6 @@ registerComponent(
const user = useUserContext();
const preset = getViewPreset('documents', undefined, {
userId: user.userId(),
- email: user.email(),
isTeamAdmin: false,
});
const initialFilters =
@@ -227,7 +225,6 @@ registerComponent(
const user = useUserContext();
const preset = getViewPreset('tasks', undefined, {
userId: user.userId(),
- email: user.email(),
isTeamAdmin: false,
});
return (
@@ -301,7 +298,6 @@ registerComponent(
const user = useUserContext();
const preset = getViewPreset('folders', undefined, {
userId: user.userId(),
- email: user.email(),
isTeamAdmin: false,
});
return (
diff --git a/rust/cloud-storage/email/fixtures/email_dynamic_query.sql b/rust/cloud-storage/email/fixtures/email_dynamic_query.sql
index eaf5cae952..7eb43ff2fc 100644
--- a/rust/cloud-storage/email/fixtures/email_dynamic_query.sql
+++ b/rust/cloud-storage/email/fixtures/email_dynamic_query.sql
@@ -17,7 +17,7 @@ VALUES
-- Insert test link
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
VALUES
- ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro1', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW());
+ ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro|[email protected]', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW());
-- Insert test contacts
INSERT INTO email_contacts (id, link_id, email_address, name, created_at, updated_at)
diff --git a/rust/cloud-storage/email/fixtures/email_dynamic_query_multi_inbox.sql b/rust/cloud-storage/email/fixtures/email_dynamic_query_multi_inbox.sql
new file mode 100644
index 0000000000..7c13adb023
--- /dev/null
+++ b/rust/cloud-storage/email/fixtures/email_dynamic_query_multi_inbox.sql
@@ -0,0 +1,47 @@
+-- Fixture for multi-inbox testing: a second email link owned by the SAME user
+-- as email_dynamic_query's link, with its own sent thread.
+-- Composes on top of the email_dynamic_query fixture.
+
+INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
+VALUES
+ ('dddddddd-dddd-dddd-dddd-dddddddddddd', 'macro|[email protected]', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW());
+
+INSERT INTO email_contacts (id, link_id, email_address, name, created_at, updated_at)
+VALUES
+ ('40000201-0000-0000-0000-000000000201', 'dddddddd-dddd-dddd-dddd-dddddddddddd', '[email protected]', 'User One Alt', NOW(), NOW()),
+ ('40000202-0000-0000-0000-000000000202', 'dddddddd-dddd-dddd-dddd-dddddddddddd', '[email protected]', 'Jane Smith', NOW(), NOW());
+
+INSERT INTO email_labels (id, link_id, provider_label_id, name, message_list_visibility, label_list_visibility, type, created_at)
+VALUES
+ ('10000201-0000-0000-0000-000000000201', 'dddddddd-dddd-dddd-dddd-dddddddddddd', 'SENT', 'SENT', 'Show', 'LabelShow', 'System', NOW()),
+ ('10000202-0000-0000-0000-000000000202', 'dddddddd-dddd-dddd-dddd-dddddddddddd', 'TRASH', 'TRASH', 'Hide', 'LabelHide', 'System', NOW());
+
+-- Thread 201: sent from the second inbox, newer than the first inbox's sent thread
+INSERT INTO email_threads (
+ id, provider_id, link_id, inbox_visible, is_read,
+ latest_inbound_message_ts, latest_outbound_message_ts, latest_non_spam_message_ts,
+ created_at, updated_at
+)
+VALUES
+ ('20000201-0000-0000-0000-000000000201', 'alt_thread1', 'dddddddd-dddd-dddd-dddd-dddddddddddd',
+ false, true, NULL, '2024-01-16 12:00:00+00', '2024-01-16 12:00:00+00', NOW(), NOW());
+
+INSERT INTO email_messages (
+ id, thread_id, link_id, provider_id, from_contact_id,
+ subject, snippet, internal_date_ts,
+ is_draft, is_sent, is_starred, is_read,
+ created_at, updated_at
+)
+VALUES
+ ('30000201-0000-0000-0000-000000000201', '20000201-0000-0000-0000-000000000201',
+ 'dddddddd-dddd-dddd-dddd-dddddddddddd', 'alt_msg1', '40000201-0000-0000-0000-000000000201',
+ 'Sent From Alt Inbox', 'Sent from the secondary inbox', '2024-01-16 12:00:00+00',
+ false, true, false, true, NOW(), NOW());
+
+INSERT INTO email_message_recipients (message_id, contact_id, recipient_type)
+VALUES
+ ('30000201-0000-0000-0000-000000000201', '40000202-0000-0000-0000-000000000202', 'TO');
+
+INSERT INTO email_message_labels (message_id, label_id)
+VALUES
+ ('30000201-0000-0000-0000-000000000201', '10000201-0000-0000-0000-000000000201');
diff --git a/rust/cloud-storage/email/fixtures/email_importance_pagination.sql b/rust/cloud-storage/email/fixtures/email_importance_pagination.sql
index 6a3ce16037..96e1645dbc 100644
--- a/rust/cloud-storage/email/fixtures/email_importance_pagination.sql
+++ b/rust/cloud-storage/email/fixtures/email_importance_pagination.sql
@@ -9,7 +9,7 @@
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
VALUES
- ('a1111111-1111-1111-1111-111111111111', 'macro1', 'fa1', '[email protected]', 'GMAIL', true, NOW(), NOW()),
+ ('a1111111-1111-1111-1111-111111111111', 'macro|[email protected]', 'fa1', '[email protected]', 'GMAIL', true, NOW(), NOW()),
('b2222222-2222-2222-2222-222222222222', 'macro2', 'fa2', '[email protected]', 'GMAIL', true, NOW(), NOW());
INSERT INTO email_contacts (id, link_id, email_address, name, created_at, updated_at)
diff --git a/rust/cloud-storage/email/fixtures/email_labels.sql b/rust/cloud-storage/email/fixtures/email_labels.sql
index 0a94d0e9f8..f041dd94f7 100644
--- a/rust/cloud-storage/email/fixtures/email_labels.sql
+++ b/rust/cloud-storage/email/fixtures/email_labels.sql
@@ -4,7 +4,7 @@
-- Insert test link
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
VALUES
- ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro1', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW());
+ ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro|[email protected]', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW());
-- Insert test threads
INSERT INTO email_threads (id, provider_id, link_id, inbox_visible, is_read, created_at, updated_at)
diff --git a/rust/cloud-storage/email/fixtures/email_thread_labels.sql b/rust/cloud-storage/email/fixtures/email_thread_labels.sql
index 5d71413b53..0e266144ee 100644
--- a/rust/cloud-storage/email/fixtures/email_thread_labels.sql
+++ b/rust/cloud-storage/email/fixtures/email_thread_labels.sql
@@ -6,7 +6,7 @@
-- Insert test links
INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
VALUES
- ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro1', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW()),
+ ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'macro|[email protected]', 'user1', '[email protected]', 'GMAIL', true, NOW(), NOW()),
('aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb', 'macro2', 'user2', '[email protected]', 'GMAIL', true, NOW(), NOW());
-- Insert test threads
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs
index fc25b8a030..c5efde436d 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/query.rs
@@ -95,6 +95,26 @@ fn push_thread_candidate_select(
) {
let defer_uh = !sort_uses_view_history(&params.sort_method_str);
+ // Multi-inbox owned scans fan out one ordered, LIMITed subscan per link:
+ // `link_id = ANY(...)` index scans can't return ordered output (pre-PG17),
+ // forcing a fetch-and-sort of every candidate thread across all inboxes.
+ // Per-link scans walk the index in order and stop at LIMIT.
+ let per_link_fanout = matches!(source, ThreadCandidateSource::Owned)
+ && params.team_id.is_none()
+ && params.link_ids.len() > 1;
+
+ if per_link_fanout {
+ builder.push(
+ r#"
+ SELECT per_link.* FROM unnest("#,
+ );
+ builder.push_bind(params.link_ids.clone());
+ builder.push(
+ r#") AS links(link_id)
+ CROSS JOIN LATERAL ("#,
+ );
+ }
+
builder.push(
r#"
SELECT
@@ -187,9 +207,13 @@ fn push_thread_candidate_select(
ThreadCandidateSource::Owned => match params.team_id {
// Normal per-mailbox query.
None => {
- builder.push("t.link_id = ANY(");
- builder.push_bind(params.link_ids.clone());
- builder.push(")");
+ if per_link_fanout {
+ builder.push("t.link_id = links.link_id");
+ } else {
+ builder.push("t.link_id = ANY(");
+ builder.push_bind(params.link_ids.clone());
+ builder.push(")");
+ }
}
// CRM-scoped query: expand to every primary email_link owned by
// any member of the team. Non-primary links (connected secondary
@@ -278,38 +302,52 @@ fn push_thread_candidate_select(
// Three closes: right row operand, the grouped row-comparison, the
// outer AND-group opened by `AND ((`.
builder.push("::uuid)))");
- return;
- }
-
- builder.push(
- r#"
+ } else {
+ builder.push(
+ r#"
-- Cursor logic
AND (("#,
- );
+ );
- builder.push_bind(params.cursor_timestamp);
+ builder.push_bind(params.cursor_timestamp);
- builder.push(
- r#"::timestamptz IS NULL) OR (
+ builder.push(
+ r#"::timestamptz IS NULL) OR (
CASE "#,
- );
+ );
- builder.push_bind(params.sort_method_str.clone());
+ builder.push_bind(params.sort_method_str.clone());
- builder.push(format!(
- r#"
+ builder.push(format!(
+ r#"
WHEN 'viewed_at' THEN COALESCE(uh."updated_at", '1970-01-01 00:00:00+00')
WHEN 'viewed_updated' THEN COALESCE(uh.updated_at, {})
ELSE {}
END, t.id
) < ("#,
- sort_ts_field, sort_ts_field
- ));
+ sort_ts_field, sort_ts_field
+ ));
+
+ builder.push_bind(params.cursor_timestamp);
+ builder.push("::timestamptz, ");
+ builder.push_bind(params.cursor_id_str.clone());
+ builder.push("::uuid))");
+ }
- builder.push_bind(params.cursor_timestamp);
- builder.push("::timestamptz, ");
- builder.push_bind(params.cursor_id_str.clone());
- builder.push("::uuid))");
+ if per_link_fanout {
+ // Per-link top-N: the outer candidate sort only ever needs the best
+ // `query_limit` rows from each inbox.
+ builder.push(
+ r#"
+ ORDER BY effective_ts DESC, id DESC
+ LIMIT "#,
+ );
+ builder.push_bind(params.query_limit);
+ builder.push(
+ r#"
+ ) AS per_link"#,
+ );
+ }
}
/// Builds a dynamic email thread query with filters applied.
@@ -614,6 +652,36 @@ pub(super) fn debug_build_query_sql_team_scoped(
)
}
+#[cfg(test)]
+pub(super) fn debug_build_query_sql_multi_link(
+ view: &PreviewView,
+ email_filter: &Expr<EmailLiteral>,
+) -> String {
+ debug_build_query_sql_full(
+ view,
+ email_filter,
+ vec![Uuid::nil(), Uuid::from_u128(1)],
+ ResolvedFilters::empty(),
+ None,
+ SimpleSortMethod::UpdatedAt,
+ )
+}
+
+#[cfg(test)]
+pub(super) fn debug_build_query_sql_team_scoped_multi_link(
+ view: &PreviewView,
+ email_filter: &Expr<EmailLiteral>,
+) -> String {
+ debug_build_query_sql_full(
+ view,
+ email_filter,
+ vec![Uuid::nil(), Uuid::from_u128(1)],
+ ResolvedFilters::empty(),
+ Some(Uuid::nil()),
+ SimpleSortMethod::UpdatedAt,
+ )
+}
+
#[cfg(test)]
fn debug_build_query_sql_inner(
view: &PreviewView,
@@ -621,6 +689,25 @@ fn debug_build_query_sql_inner(
resolved: ResolvedFilters,
team_id: Option<Uuid>,
sort_method: SimpleSortMethod,
+) -> String {
+ debug_build_query_sql_full(
+ view,
+ email_filter,
+ vec![Uuid::nil()],
+ resolved,
+ team_id,
+ sort_method,
+ )
+}
+
+#[cfg(test)]
+fn debug_build_query_sql_full(
+ view: &PreviewView,
+ email_filter: &Expr<EmailLiteral>,
+ link_ids: Vec<Uuid>,
+ resolved: ResolvedFilters,
+ team_id: Option<Uuid>,
+ sort_method: SimpleSortMethod,
) -> String {
use sqlx::Execute;
@@ -634,7 +721,7 @@ fn debug_build_query_sql_inner(
view,
email_filter,
QueryParams {
- link_ids: vec![Uuid::nil()],
+ link_ids,
sort_method_str: sort_method.to_string(),
query_limit: 50,
cursor_timestamp: None,
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs
index 65bffb9dc9..da2c309b43 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/dynamic/tests.rs
@@ -509,6 +509,54 @@ fn test_build_query_projects_real_updated_at_for_candidate_threads() {
assert!(!sql.contains("COALESCE(t.latest_non_spam_message_ts, t.updated_at) AS updated_at"));
}
+#[test]
+fn test_build_query_multi_link_fans_out_per_link() {
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Sent);
+ let expr = Expr::Literal(EmailLiteral::CalendarOnly(false));
+ let sql = super::query::debug_build_query_sql_multi_link(&view, &expr);
+
+ // Each link gets its own ordered, LIMITed candidate scan...
+ assert!(
+ sql.contains("FROM unnest(") && sql.contains("CROSS JOIN LATERAL"),
+ "multi-link candidates must fan out per link: {sql}"
+ );
+ assert!(
+ sql.contains("t.link_id = links.link_id"),
+ "per-link branch must scope to a single link: {sql}"
+ );
+ assert!(
+ !sql.contains("t.link_id = ANY("),
+ "multi-link owned scan must not use = ANY: {sql}"
+ );
+ // ...with a per-branch LIMIT feeding the outer sort.
+ assert_eq!(
+ sql.matches("ORDER BY effective_ts DESC, id DESC").count(),
+ 2,
+ "expected per-link and outer candidate ordering: {sql}"
+ );
+}
+
+#[test]
+fn test_build_query_single_link_keeps_any_scan() {
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Sent);
+ let expr = Expr::Literal(EmailLiteral::CalendarOnly(false));
+ let sql = super::query::debug_build_query_sql(&view, &expr);
+
+ assert!(sql.contains("t.link_id = ANY("));
+ assert!(!sql.contains("FROM unnest("));
+}
+
+#[test]
+fn test_build_query_team_scoped_multi_link_does_not_fan_out() {
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Inbox);
+ let expr = Expr::Literal(EmailLiteral::Sender(Email::Domain("acme.com".to_string())));
+ let sql = super::query::debug_build_query_sql_team_scoped_multi_link(&view, &expr);
+
+ // Team-scoped candidates dedupe across links before the cursor; a
+ // per-link LIMIT could starve the dedupe of duplicate copies.
+ assert!(!sql.contains("FROM unnest("));
+}
+
#[test]
fn test_build_query_orders_by_id_to_match_cursor_tiebreak() {
let view = PreviewView::StandardLabel(PreviewViewStandardLabel::All);
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/preview.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/preview.rs
index 440a9732c2..b38fe6b427 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/preview.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/preview.rs
@@ -4,8 +4,11 @@ use crate::domain::models::{
};
use doppleganger::{Doppleganger, Mirror};
use either::Either;
+use filter_ast::Expr;
+use item_filters::ast::email::EmailLiteral;
use macro_user_id::user_id::MacroUserIdStr;
use sqlx::PgPool;
+use std::sync::Arc;
use uuid::Uuid;
use super::db_types::{AttachmentDbRow, LabelDbRow, ThreadPreviewCursorDbRow};
@@ -45,8 +48,24 @@ pub(super) async fn previews_for_view_cursor(
)
.await?
}
- (PreviewView::StandardLabel(PreviewViewStandardLabel::Sent), Either::Left(query)) => {
- super::preview_views::sent::sent_preview_cursor(pool, &link_ids, limit, &query).await?
+ (
+ view @ PreviewView::StandardLabel(PreviewViewStandardLabel::Sent),
+ Either::Left(query),
+ ) => {
+ // Sent always uses the dynamic builder, even with no filter;
+ // CalendarOnly(false) is the documented no-op literal.
+ let query =
+ query.map_filter(|()| Arc::new(Expr::Literal(EmailLiteral::CalendarOnly(false))));
+ super::dynamic::dynamic_email_thread_cursor(
+ pool,
+ &link_ids,
+ limit,
+ &view,
+ query,
+ user_id.as_ref(),
+ team_id,
+ )
+ .await?
}
(PreviewView::StandardLabel(PreviewViewStandardLabel::Drafts), Either::Left(query)) => {
super::preview_views::draft::drafts_preview_cursor(pool, &link_ids, limit, &query)
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views.rs
index 053d030005..17cda409fb 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views.rs
@@ -3,6 +3,5 @@ pub(super) mod draft;
pub(super) mod important;
pub(super) mod new_inbox;
pub(super) mod other_inbox;
-pub(super) mod sent;
pub(super) mod starred;
pub(super) mod user_label;
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views/sent.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views/sent.rs
deleted file mode 100644
index b96bd012b6..0000000000
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/preview_views/sent.rs
+++ /dev/null
@@ -1,121 +0,0 @@
-use super::super::db_types::*;
-use models_pagination::{Query, SimpleSortMethod};
-use sqlx::PgPool;
-use uuid::Uuid;
-
-/// Fetches a paginated list of thread previews for the "Sent" view.
-/// This view includes all threads (archived or not) sorted by the timestamp
-/// of the latest sent message. The preview details (subject, snippet) are
-/// taken from the most recent sent message in the thread that is not in the trash.
-#[tracing::instrument(skip(pool), err)]
-pub(crate) async fn sent_preview_cursor(
- pool: &PgPool,
- link_ids: &[Uuid],
- limit: u32,
- query: &Query<Uuid, SimpleSortMethod, ()>,
-) -> Result<Vec<ThreadPreviewCursorDbRow>, sqlx::Error> {
- let query_limit = limit as i64;
- let sort_method_str = query.sort_method().to_string();
- let (cursor_id, cursor_timestamp) = query.vals();
-
- sqlx::query_as!(
- ThreadPreviewCursorDbRow,
- r#"
- SELECT
- t.id,
- t.provider_id,
- t.inbox_visible,
- t.is_read,
- 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?",
- 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
- )
- ) AS "is_important!",
- c.email_address AS "sender_email?",
- COALESCE(lmp.from_name, c.name) AS "sender_name?",
- c.sfs_photo_url as "sender_photo_url?",
- 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 only touches `threads` and `user_history`.
- SELECT
- t.id,
- t.provider_id,
- t.link_id,
- t.inbox_visible,
- t.is_read,
- t.project_id,
- t.latest_outbound_message_ts AS created_at,
- t.latest_outbound_message_ts 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, t.latest_outbound_message_ts)
- ELSE t.latest_outbound_message_ts
- 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.latest_outbound_message_ts IS NOT NULL
-
- -- Cursor logic moved inside 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, t.latest_outbound_message_ts)
- ELSE t.latest_outbound_message_ts
- END, t.id
- ) < ($3::timestamptz, $4::uuid))
- ORDER BY effective_ts DESC, t.updated_at DESC
- LIMIT $2
- ) AS t
- -- Step 2: For EACH of the limited threads from above, find its latest SENT, non-trashed message.
- CROSS JOIN LATERAL (
- SELECT
- m.subject,
- m.snippet,
- m.from_contact_id,
- m.from_name,
- m.is_draft
- FROM email_messages m
- WHERE m.thread_id = t.id
- AND m.is_sent = TRUE -- This condition is specific to the "Sent" view
- 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
- )
- ORDER BY m.internal_date_ts DESC
- LIMIT 1
- ) 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
- 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
- "#,
- link_ids, // $1
- query_limit, // $2
- cursor_timestamp, // $3
- cursor_id, // $4
- sort_method_str, // $5
- )
- .fetch_all(pool)
- .await
-}
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs
index dc64f615a3..f0eb6a3d1b 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/dynamic_query.rs
@@ -64,6 +64,128 @@ async fn test_dynamic_query_sent_view(pool: Pool<Postgres>) -> anyhow::Result<()
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../../fixtures",
+ scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
+ )
+)]
+async fn test_dynamic_query_sent_view_multi_inbox_without_sender_filter(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_ids = [
+ Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
+ Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
+ ];
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Sent);
+ // No sender restriction: is_sent alone must scope each inbox's sent mail.
+ let filter = Arc::new(Expr::Literal(EmailLiteral::CalendarOnly(false)));
+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);
+
+ let results =
+ dynamic::dynamic_email_thread_cursor(&pool, &link_ids, 50, &view, query, "", None).await?;
+
+ let ids: Vec<String> = results.iter().map(|r| r.id.to_string()).collect();
+ assert_eq!(
+ ids,
+ vec![
+ "20000201-0000-0000-0000-000000000201".to_string(),
+ "20000002-0000-0000-0000-000000000002".to_string(),
+ ],
+ "Sent view should return each inbox's sent thread, newest first"
+ );
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../../fixtures",
+ scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
+ )
+)]
+async fn test_dynamic_query_sent_view_multi_inbox_cursor_pagination(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ // Exercises the per-link fan-out: each inbox's candidates are LIMITed
+ // per branch, and the cursor must page across inboxes correctly.
+ let link_ids = [
+ Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
+ Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
+ ];
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Sent);
+ let no_op = || Arc::new(Expr::Literal(EmailLiteral::CalendarOnly(false)));
+
+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, no_op());
+ let first_page =
+ dynamic::dynamic_email_thread_cursor(&pool, &link_ids, 1, &view, query, "", None).await?;
+
+ assert_eq!(first_page.len(), 1);
+ assert_eq!(
+ first_page[0].id.to_string(),
+ "20000201-0000-0000-0000-000000000201",
+ "First page should hold the newest sent thread (alt inbox)"
+ );
+
+ let cursor = Cursor {
+ id: first_page[0].id,
+ limit: 1,
+ val: CursorVal {
+ sort_type: SimpleSortMethod::UpdatedAt,
+ last_val: first_page[0].sort_ts,
+ },
+ filter: no_op(),
+ };
+ let query2 = Query::new(Some(cursor), SimpleSortMethod::UpdatedAt, no_op());
+ let second_page =
+ dynamic::dynamic_email_thread_cursor(&pool, &link_ids, 1, &view, query2, "", None).await?;
+
+ assert_eq!(second_page.len(), 1);
+ assert_eq!(
+ second_page[0].id.to_string(),
+ "20000002-0000-0000-0000-000000000002",
+ "Second page should hold the other inbox's sent thread"
+ );
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../../fixtures",
+ scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
+ )
+)]
+async fn test_dynamic_query_sent_view_single_address_sender_filter_misses_other_inbox(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ // Documents why the sent view must not filter by a single sender address:
+ // one inbox's address can never match mail sent from the other inbox.
+ let link_ids = [
+ Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
+ Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
+ ];
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Sent);
+ let filter = Arc::new(Expr::Literal(EmailLiteral::Sender(Email::Partial(
+ "[email protected]".to_string(),
+ ))));
+ let query = Query::new(None, SimpleSortMethod::UpdatedAt, filter);
+
+ let results =
+ dynamic::dynamic_email_thread_cursor(&pool, &link_ids, 50, &view, query, "", None).await?;
+
+ assert_eq!(results.len(), 1, "Only the alt inbox's thread should match");
+ assert_eq!(
+ results[0].id.to_string(),
+ "20000201-0000-0000-0000-000000000201"
+ );
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_dynamic_query"))
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/preview.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/preview.rs
index 4c50544c31..960a043128 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/test/preview.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/test/preview.rs
@@ -1,5 +1,51 @@
use super::*;
+// ── previews_for_view_cursor: sent routing ──────────────────────────
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(
+ path = "../../../../fixtures",
+ scripts("email_dynamic_query", "email_dynamic_query_multi_inbox")
+ )
+)]
+async fn test_sent_view_without_filter_routes_to_dynamic_multi_inbox(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ // The sent tab sends no filter tree; the routing must still take the
+ // dynamic path and cover every linked inbox without a sender filter.
+ let repo = EmailPgRepo::new(pool);
+ let query = crate::domain::models::PreviewCursorQuery {
+ view: PreviewView::StandardLabel(PreviewViewStandardLabel::Sent),
+ link_ids: vec![
+ Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?,
+ Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?,
+ ],
+ limit: 50,
+ query: Query::new(None, SimpleSortMethod::UpdatedAt, None),
+ team_id: None,
+ };
+
+ let results = repo
+ .previews_for_view_cursor(
+ query,
+ MacroUserIdStr::parse_from_str("macro|[email protected]")?.to_owned(),
+ )
+ .await?;
+
+ let ids: Vec<String> = results.iter().map(|r| r.id.to_string()).collect();
+ assert_eq!(
+ ids,
+ vec![
+ "20000201-0000-0000-0000-000000000201".to_string(),
+ "20000002-0000-0000-0000-000000000002".to_string(),
+ ],
+ "Sent view should return each inbox's sent thread, newest first"
+ );
+
+ Ok(())
+}
+
// ── attachments_by_thread_ids ───────────────────────────────────────
#[sqlx::test(
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260706160211_email_threads_sent_view_id_tiebreak.sql b/rust/cloud-storage/macro_db_client/migrations/20260706160211_email_threads_sent_view_id_tiebreak.sql
new file mode 100644
index 0000000000..ffa426f120
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260706160211_email_threads_sent_view_id_tiebreak.sql
@@ -0,0 +1,8 @@
+-- no-transaction
+-- Sent-view thread index with the id tiebreak, so per-link candidate scans
+-- (ORDER BY latest_outbound_message_ts DESC, id DESC LIMIT n) are pure
+-- ordered index scans with no sort step. Replaces idx_email_threads_sent_view
+-- (link_id, latest_outbound_message_ts DESC), dropped by the next migration.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_email_threads_sent_view_link_ts_id
+ ON email_threads (link_id, latest_outbound_message_ts DESC, id DESC)
+ WHERE (latest_outbound_message_ts IS NOT NULL);
diff --git a/rust/cloud-storage/macro_db_client/migrations/20260706160302_drop_old_email_threads_sent_view_index.sql b/rust/cloud-storage/macro_db_client/migrations/20260706160302_drop_old_email_threads_sent_view_index.sql
new file mode 100644
index 0000000000..3814460747
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20260706160302_drop_old_email_threads_sent_view_index.sql
@@ -0,0 +1,3 @@
+-- no-transaction
+-- Superseded by idx_email_threads_sent_view_link_ts_id (adds the id tiebreak).
+DROP INDEX CONCURRENTLY IF EXISTS idx_email_threads_sent_view;