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(soup): Email pagination (#2360)

Evan Hutnik0a63205d04b3
diff --git a/rust/cloud-storage/models_pagination/src/cursor.rs b/rust/cloud-storage/models_pagination/src/cursor.rs
index f4287de39c..da81e1c116 100644
--- a/rust/cloud-storage/models_pagination/src/cursor.rs
+++ b/rust/cloud-storage/models_pagination/src/cursor.rs
@@ -30,6 +30,46 @@ impl<T, U> Paginated<T, U> {
}
}
+impl<T, I, S: Sortable, F> PaginatedCursor<T, I, S, F>
+where
+ T: Identify<Id = I>,
+ I: Serialize,
+ S: Serialize,
+ CursorVal<S>: Serialize,
+ F: Serialize,
+{
+ /// Ensures a cursor is present as long as items is non-empty.
+ /// Useful when post-processing (e.g. CROSS JOIN LATERAL) may have filtered
+ /// rows after the initial limit, making the standard "len < limit means
+ /// last page" heuristic unreliable.
+ pub fn ensure_cursor(
+ self,
+ limit: usize,
+ mut cb: impl FnMut(&T) -> CursorVal<S>,
+ filter: F,
+ ) -> Self {
+ if self.next_cursor.is_some() || self.items.is_empty() {
+ return self;
+ }
+
+ let next_cursor = self
+ .items
+ .last()
+ .map(|last| Cursor {
+ id: last.id(),
+ limit,
+ val: cb(last),
+ filter,
+ })
+ .map(Base64Str::encode_json);
+
+ Paginated {
+ items: self.items,
+ next_cursor,
+ }
+ }
+}
+
/// Top level cursor information encodes all the required information for paginating by [Cursor]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Cursor<Id, C, F> {
diff --git a/rust/cloud-storage/models_soup/src/item.rs b/rust/cloud-storage/models_soup/src/item.rs
index a587499745..96f4c839cf 100644
--- a/rust/cloud-storage/models_soup/src/item.rs
+++ b/rust/cloud-storage/models_soup/src/item.rs
@@ -88,17 +88,11 @@ impl SoupItem {
(SoupItem::Project(soup_project), SimpleSortMethod::ViewedUpdated) => {
soup_project.viewed_at.unwrap_or(soup_project.updated_at)
}
- (SoupItem::EmailThread(thread), SimpleSortMethod::ViewedAt) => {
- thread.thread.viewed_at.unwrap_or_default()
- }
- (SoupItem::EmailThread(thread), SimpleSortMethod::UpdatedAt) => {
- thread.thread.updated_at
- }
- (SoupItem::EmailThread(thread), SimpleSortMethod::CreatedAt) => {
- thread.thread.created_at
- }
- (SoupItem::EmailThread(thread), SimpleSortMethod::ViewedUpdated) => {
- thread.thread.viewed_at.unwrap_or(thread.thread.updated_at)
+ (SoupItem::EmailThread(thread), _) => {
+ // Always use sort_ts for emails — this is the pre-computed effective_ts
+ // from the email SQL query, which is also what the cursor offset logic
+ // uses: (effective_ts, id) < (cursor_ts, cursor_id).
+ thread.thread.sort_ts
}
(SoupItem::Channel(soup_channel), SimpleSortMethod::ViewedAt) => {
soup_channel.viewed_at.unwrap_or_default()
diff --git a/rust/cloud-storage/soup/src/domain/service.rs b/rust/cloud-storage/soup/src/domain/service.rs
index 7a62d6d43d..02f538c24c 100644
--- a/rust/cloud-storage/soup/src/domain/service.rs
+++ b/rust/cloud-storage/soup/src/domain/service.rs
@@ -20,7 +20,7 @@ use frecency::domain::{
use item_filters::ast::EntityFilterAst;
use macro_user_id::user_id::MacroUserIdStr;
use models_pagination::{
- Cursor, CursorVal, Frecency, FrecencyValue, PaginateOn, Query, SimpleSortMethod,
+ Cursor, CursorVal, Frecency, FrecencyValue, PaginateOn, Query, SimpleSortMethod, SortOn,
};
use models_soup::{
comms::SoupChannel,
@@ -385,15 +385,33 @@ where
let (main_soup, email_soup, comms_soup) =
tokio::join!(main_soup_fut, email_soup_fut, comms_soup_fut);
- Ok(Either::Left(
- main_soup?
- .chain(email_soup?)
- .chain(comms_soup?)
- .paginate_on(limit.into(), sort_method)
- .filter_on(entity_filter)
- .sort_desc()
- .into_page(),
- ))
+ let page = main_soup?
+ .chain(email_soup?)
+ .chain(comms_soup?)
+ .paginate_on(limit.into(), sort_method)
+ .filter_on(entity_filter.clone())
+ .sort_desc()
+ .into_page();
+
+ // Email queries use CROSS JOIN LATERAL which can filter out
+ // threads after the initial LIMIT, making the standard
+ // "len < limit means last page" heuristic unreliable.
+ // Force a cursor when emails are in the response.
+ let has_emails = page
+ .items
+ .iter()
+ .any(|item| matches!(&item.item, SoupItem::EmailThread(_)));
+ let page = if has_emails {
+ page.ensure_cursor(
+ limit.into(),
+ FrecencySoupItem::sort_on(sort_method),
+ entity_filter,
+ )
+ } else {
+ page
+ };
+
+ Ok(Either::Left(page))
}
SoupQuery::Frecency(FrecencyQueryInner(cursor)) => Ok(Either::Right(
self.handle_advanced_sort(cursor, req.soup_type, req.user, limit)