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(inbox): unread email filter surfaces all unread threads (#5944)

teodf99904cba50
diff --git a/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx b/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx
index ef2750852c..b77f5ba4f7 100644
--- a/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx
+++ b/apps/web/src/features/next-soup/soup-view/soup-view-context.tsx
@@ -67,6 +67,7 @@ import {
isWithNotification,
type Notification,
toNotificationEntity,
+ unreadFilterFn,
} from '@entity';
import { useNotificationsForEntity } from '@notifications';
import { SYSTEM_PROPERTY_IDS } from '@property/constants';
@@ -767,7 +768,7 @@ export const SoupViewContextProvider: FlowComponent<
documentSeen: seen,
emailSeen: seen,
channelSeen: seen,
- // channelThreadSeen: seen,
+ channelThreadSeen: seen,
chatSeen: seen,
folderSeen: seen,
foreignEntitySeen: seen,
@@ -775,6 +776,13 @@ export const SoupViewContextProvider: FlowComponent<
};
};
+ const entityMatchesInboxReadFilter = (entity: EntityData): boolean => {
+ const filter = readFilter();
+ if (filter === 'all' || !isInboxView()) return true;
+ const isUnread = unreadFilterFn(entity);
+ return filter === 'unread' ? isUnread : !isUnread;
+ };
+
const applyViewFilters = (state: QueryState): QueryState => {
let next = applyInboxFilter(state);
next = applyInboxThreadFilter(next);
@@ -947,9 +955,10 @@ export const SoupViewContextProvider: FlowComponent<
const membershipFilter = config().itemMembershipFilter;
if (membershipFilter && !membershipFilter(item)) return false;
- return soup.predicates.test(
- mapApiSoupItemToEntity(item) as SoupEntity,
- getFilterContext()
+ const entity = mapApiSoupItemToEntity(item) as SoupEntity;
+ return (
+ soup.predicates.test(entity, getFilterContext()) &&
+ entityMatchesInboxReadFilter(entity)
);
};
@@ -1053,6 +1062,9 @@ export const SoupViewContextProvider: FlowComponent<
if (!soup.predicates.test(entity, ctx)) {
continue;
}
+ if (!entityMatchesInboxReadFilter(entity)) {
+ continue;
+ }
if (!entityMatchesTagFilter(entity, tagOptionIds, tagFilterMode)) {
continue;
}
diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs b/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs
index 7151236682..0bd8add69a 100644
--- a/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs
+++ b/crates/email/src/outbound/email_pg_repo/dynamic/filters.rs
@@ -36,6 +36,8 @@ pub(super) fn has_thread_literals(ast: &Expr<EmailLiteral>) -> bool {
| EmailLiteral::ProjectId(_)
| EmailLiteral::CalendarOnly(_)
| EmailLiteral::Importance(_)
+ | EmailLiteral::NotificationSeen(_)
+ | EmailLiteral::NotificationDone(_)
| EmailLiteral::CreatedAt(_)
| EmailLiteral::UpdatedAt(_)
| EmailLiteral::Property(_),
@@ -56,6 +58,8 @@ pub(super) fn has_message_literals(ast: &Expr<EmailLiteral>) -> bool {
| EmailLiteral::Shared(_)
| EmailLiteral::CalendarOnly(_)
| EmailLiteral::Importance(_)
+ | EmailLiteral::NotificationSeen(_)
+ | EmailLiteral::NotificationDone(_)
| EmailLiteral::CreatedAt(_)
| EmailLiteral::UpdatedAt(_)
| EmailLiteral::Property(_),
@@ -875,13 +879,20 @@ pub(super) fn build_thread_email_filter(
build_thread_property_predicate(lit)
}
+ filter_ast::ExprFrame::Literal(EmailLiteral::NotificationSeen(true)) => {
+ SqlFragment::raw("t.is_read = TRUE")
+ }
+
+ filter_ast::ExprFrame::Literal(EmailLiteral::NotificationSeen(false)) => {
+ SqlFragment::raw("t.is_read = FALSE")
+ }
+
filter_ast::ExprFrame::Literal(
EmailLiteral::Sender(_)
| EmailLiteral::Cc(_)
| EmailLiteral::Bcc(_)
| EmailLiteral::Recipient(_)
| EmailLiteral::NotificationDone(_)
- | EmailLiteral::NotificationSeen(_)
| EmailLiteral::Shared(_),
) => SqlFragment::raw("TRUE"),
});
diff --git a/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs b/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs
index 170bdf1a2b..721e9d9e93 100644
--- a/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs
+++ b/crates/email/src/outbound/email_pg_repo/dynamic/tests.rs
@@ -253,6 +253,65 @@ fn test_partial_and_domain_emit_different_predicates_for_same_string() {
assert!(domain.has_bind_string("acme.com"));
}
+#[test]
+fn test_has_thread_literals_true_when_notification_seen_present() {
+ let expr = Expr::Literal(EmailLiteral::NotificationSeen(false));
+ assert!(has_thread_literals(&expr));
+ assert!(!has_message_literals(&expr));
+}
+
+#[test]
+fn test_notification_seen_compiles_to_thread_is_read() {
+ let unread = build_thread_email_filter(
+ &Expr::Literal(EmailLiteral::NotificationSeen(false)),
+ DEFAULT_SORT_TS,
+ )
+ .to_debug_sql();
+ assert!(unread.contains("t.is_read = FALSE"));
+
+ let read = build_thread_email_filter(
+ &Expr::Literal(EmailLiteral::NotificationSeen(true)),
+ DEFAULT_SORT_TS,
+ )
+ .to_debug_sql();
+ assert!(read.contains("t.is_read = TRUE"));
+}
+
+#[test]
+fn test_notification_seen_is_noop_in_message_filter() {
+ for seen in [true, false] {
+ let result = build_message_email_filter(
+ &Expr::Literal(EmailLiteral::NotificationSeen(seen)),
+ &ResolvedFilters::empty(),
+ );
+ let debug = result.to_debug_sql();
+ assert!(debug.contains("TRUE"));
+ assert!(!debug.contains("is_read"));
+ }
+}
+
+#[test]
+fn test_full_query_notification_seen_filters_candidate_by_is_read() {
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Inbox);
+ let unread = super::query::debug_build_query_sql(
+ &view,
+ &Expr::Literal(EmailLiteral::NotificationSeen(false)),
+ );
+ assert!(
+ unread.contains("t.is_read = FALSE"),
+ "unread NotificationSeen must land in the candidate WHERE: {unread}"
+ );
+
+ let read = super::query::debug_build_query_sql(
+ &view,
+ &Expr::Literal(EmailLiteral::NotificationSeen(true)),
+ );
+ assert!(
+ read.contains("t.is_read = TRUE"),
+ "read NotificationSeen must land in the candidate WHERE: {read}"
+ );
+}
+
#[test]
fn test_importance_compiles_to_thread_signal_flag() {
let thread = build_thread_email_filter(
diff --git a/crates/email/src/outbound/email_pg_repo/test/dynamic_query.rs b/crates/email/src/outbound/email_pg_repo/test/dynamic_query.rs
index 9d1e0b135c..903dde11c0 100644
--- a/crates/email/src/outbound/email_pg_repo/test/dynamic_query.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/dynamic_query.rs
@@ -37,6 +37,65 @@ async fn test_dynamic_query_inbox_view(pool: Pool<Postgres>) -> anyhow::Result<(
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../fixtures", scripts("email_dynamic_query"))
+)]
+async fn test_dynamic_query_notification_seen_filters_by_is_read(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;
+ let read_thread = Uuid::parse_str("20000001-0000-0000-0000-000000000001")?;
+ let unread_thread = Uuid::parse_str("20000004-0000-0000-0000-000000000004")?;
+
+ // Fixture inbox threads all start unread; mark one read so the filter
+ // has both sides of the partition to distinguish.
+ sqlx::query("UPDATE email_threads SET is_read = TRUE WHERE id = $1")
+ .bind(read_thread)
+ .execute(&pool)
+ .await?;
+
+ let view = PreviewView::StandardLabel(PreviewViewStandardLabel::Inbox);
+ let unread_filter = Arc::new(Expr::Literal(EmailLiteral::NotificationSeen(false)));
+ let unread_query = Query::new(None, SimpleSortMethod::UpdatedAt, unread_filter);
+ let unread_results =
+ dynamic::dynamic_email_thread_cursor(&pool, &[link_id], 50, &view, unread_query, "", None)
+ .await?;
+
+ assert!(
+ unread_results.iter().all(|r| !r.is_read),
+ "unread filter must not return read threads: {:?}",
+ unread_results
+ .iter()
+ .map(|r| (r.id, r.is_read))
+ .collect::<Vec<_>>()
+ );
+ assert!(
+ unread_results.iter().any(|r| r.id == unread_thread),
+ "unread filter should still return unread inbox threads"
+ );
+ assert!(
+ !unread_results.iter().any(|r| r.id == read_thread),
+ "unread filter must exclude the thread marked read"
+ );
+
+ let read_filter = Arc::new(Expr::Literal(EmailLiteral::NotificationSeen(true)));
+ let read_query = Query::new(None, SimpleSortMethod::UpdatedAt, read_filter);
+ let read_results =
+ dynamic::dynamic_email_thread_cursor(&pool, &[link_id], 50, &view, read_query, "", None)
+ .await?;
+
+ assert_eq!(
+ read_results.len(),
+ 1,
+ "read filter should return only the marked-read inbox thread"
+ );
+ assert_eq!(read_results[0].id, read_thread);
+ assert!(read_results[0].is_read);
+
+ Ok(())
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../../fixtures", scripts("email_dynamic_query"))