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): build the work feed from pending notifications only (#7993)

teoc3f3b993c81c
diff --git a/crates/macro_db_client/migrations/20261013193738_user_notification_user_pending_idx.down.sql b/crates/macro_db_client/migrations/20261013193738_user_notification_user_pending_idx.down.sql
new file mode 100644
index 0000000000..15ee74800d
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261013193738_user_notification_user_pending_idx.down.sql
@@ -0,0 +1,2 @@
+-- no-transaction
+DROP INDEX CONCURRENTLY IF EXISTS idx_user_notification_user_pending;
diff --git a/crates/macro_db_client/migrations/20261013193738_user_notification_user_pending_idx.up.sql b/crates/macro_db_client/migrations/20261013193738_user_notification_user_pending_idx.up.sql
new file mode 100644
index 0000000000..b96a0677c9
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261013193738_user_notification_user_pending_idx.up.sql
@@ -0,0 +1,9 @@
+-- no-transaction
+-- The viewer's pending (unseen or seen) notifications for the work feed:
+-- attention reads only these, so a long done history costs nothing. Covers
+-- the read with notification_id, so it needs no user_notification heap pages.
+-- Must stay a single statement: sqlx sends no-transaction migrations as
+-- one batch, and CONCURRENTLY cannot run inside a transaction.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_notification_user_pending
+ ON user_notification (user_id, created_at DESC) INCLUDE (notification_id)
+ WHERE deleted_at IS NULL AND state <> 'done';
diff --git a/crates/soup/fixtures/work_feed.sql b/crates/soup/fixtures/work_feed.sql
index a1551cb97d..7ded5897ad 100644
--- a/crates/soup/fixtures/work_feed.sql
+++ b/crates/soup/fixtures/work_feed.sql
@@ -1,7 +1,7 @@
-- Fixture for the soup work feed candidate query.
--
-- user-1 is the subject. Each entity exercises one rule of the merge between
--- attention (live notifications, one not done) and own work (latest non-view
+-- attention (the newest pending notification) and own work (latest non-view
-- activity).
-- (T = minute past 10:00 on 2024-06-01, UTC)
--
@@ -9,6 +9,8 @@
-- because the thread is not in the inbox -> own T13
-- thread-Z in the inbox, notified at T12 -> attention T12
-- channel-X user-1 messaged at T11 -> own T11
+-- doc-H pending notification T6, edited T10; its
+-- newer T15 notification is done -> own T10
-- doc-A notified at T5, edited at T9 -> own T9
-- doc-B notified at T8, edited at T3 -> attention T8
-- doc-G notified at T8 (ties doc-B; id breaks it) -> attention T8
@@ -45,7 +47,8 @@ VALUES (1, '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa'),
(3, '11111111-cccc-cccc-cccc-cccccccccccc'),
(5, '11111111-eeee-eeee-eeee-eeeeeeeeeeee'),
(6, '11111111-ffff-ffff-ffff-ffffffffffff'),
- (7, '11111111-0000-0000-0000-000000000000');
+ (7, '11111111-0000-0000-0000-000000000000'),
+ (8, '11111111-9999-9999-9999-999999999999');
INSERT INTO public."Document" ("id", "name", "owner", "documentFamilyId", "fileType", "createdAt", "updatedAt", "deletedAt")
VALUES ('11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'Doc A', 'macro|[email protected]', 1, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL),
@@ -53,7 +56,8 @@ VALUES ('11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'Doc A', 'macro|[email protected]'
('11111111-cccc-cccc-cccc-cccccccccccc', 'Doc C', 'macro|[email protected]', 3, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL),
('11111111-eeee-eeee-eeee-eeeeeeeeeeee', 'Doc E', 'macro|[email protected]', 5, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL),
('11111111-ffff-ffff-ffff-ffffffffffff', 'Doc F', 'macro|[email protected]', 6, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL),
- ('11111111-0000-0000-0000-000000000000', 'Doc G', 'macro|[email protected]', 7, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL);
+ ('11111111-0000-0000-0000-000000000000', 'Doc G', 'macro|[email protected]', 7, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL),
+ ('11111111-9999-9999-9999-999999999999', 'Doc H', 'macro|[email protected]', 8, 'md', '2023-01-05 10:00:00', '2023-01-05 10:00:00', NULL);
INSERT INTO public."Chat" ("id", "name", "userId", "projectId", "createdAt", "updatedAt")
VALUES ('22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'Chat A', 'macro|[email protected]', NULL, '2023-01-06 10:00:00', '2023-01-06 10:00:00');
@@ -84,6 +88,7 @@ VALUES ('11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'document', 'macro|[email protected]
('11111111-eeee-eeee-eeee-eeeeeeeeeeee', 'document', 'macro|[email protected]', 'user', 'owner', NULL),
('11111111-ffff-ffff-ffff-ffffffffffff', 'document', 'macro|[email protected]', 'user', 'owner', NULL),
('11111111-0000-0000-0000-000000000000', 'document', 'macro|[email protected]', 'user', 'view', NULL),
+ ('11111111-9999-9999-9999-999999999999', 'document', 'macro|[email protected]', 'user', 'owner', NULL),
('22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'chat', 'macro|[email protected]', 'user', 'owner', NULL);
---------------------------------------------------
@@ -97,6 +102,8 @@ VALUES
('0190b000-0000-7000-8000-000000000108', 'document_mention', '11111111-0000-0000-0000-000000000000', 'document', 'test', '2024-06-01 10:08:00', '{}', 'macro|[email protected]', NULL, NULL),
('0190b000-0000-7000-8000-000000000004', 'ai_response', '22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'chat', 'test', '2024-06-01 10:04:00', '{}', NULL, NULL, NULL),
('0190b000-0000-7000-8000-000000000010', 'document_mention', '11111111-eeee-eeee-eeee-eeeeeeeeeeee', 'document', 'test', '2024-06-01 10:10:00', '{}', 'macro|[email protected]', NULL, NULL),
+('0190b000-0000-7000-8000-000000000006', 'document_mention', '11111111-9999-9999-9999-999999999999', 'document', 'test', '2024-06-01 10:06:00', '{}', 'macro|[email protected]', NULL, NULL),
+('0190b000-0000-7000-8000-000000000015', 'document_mention', '11111111-9999-9999-9999-999999999999', 'document', 'test', '2024-06-01 10:15:00', '{}', 'macro|[email protected]', NULL, NULL),
('0190b000-0000-7000-8000-000000000001', 'channel_mention', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel', 'test', '2024-06-01 10:01:00', '{"messageId": "99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}', 'macro|[email protected]', '99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'channel_message'),
('0190b000-0000-7000-8000-000000000012', 'new_email', '44444444-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'email_thread', 'test', '2024-06-01 10:12:00', '{}', NULL, NULL, NULL),
('0190b000-0000-7000-8000-000000000000', 'new_email', '44444444-5555-5555-5555-555555555555', 'email_thread', 'test', '2024-06-01 10:00:00', '{}', NULL, NULL, NULL),
@@ -109,6 +116,8 @@ VALUES
('macro|[email protected]', '0190b000-0000-7000-8000-000000000108', '2024-06-01 10:08:00', TRUE, NULL, NULL, 'unseen', FALSE),
('macro|[email protected]', '0190b000-0000-7000-8000-000000000004', '2024-06-01 10:04:00', TRUE, NULL, NULL, 'unseen', FALSE),
('macro|[email protected]', '0190b000-0000-7000-8000-000000000010', '2024-06-01 10:10:00', TRUE, NULL, NULL, 'done', FALSE),
+('macro|[email protected]', '0190b000-0000-7000-8000-000000000006', '2024-06-01 10:06:00', TRUE, NULL, NULL, 'unseen', FALSE),
+('macro|[email protected]', '0190b000-0000-7000-8000-000000000015', '2024-06-01 10:15:00', TRUE, NULL, NULL, 'done', FALSE),
('macro|[email protected]', '0190b000-0000-7000-8000-000000000001', '2024-06-01 10:01:00', TRUE, NULL, NULL, 'unseen', FALSE),
('macro|[email protected]', '0190b000-0000-7000-8000-000000000012', '2024-06-01 10:12:00', TRUE, NULL, NULL, 'unseen', FALSE),
('macro|[email protected]', '0190b000-0000-7000-8000-000000000000', '2024-06-01 10:00:00', TRUE, NULL, NULL, 'unseen', FALSE),
@@ -126,6 +135,7 @@ VALUES
('0190c000-0000-7000-8000-000000000002', 'macro|[email protected]', 'macro|[email protected]', 'created', NULL, 'document', '11111111-eeee-eeee-eeee-eeeeeeeeeeee', '2024-06-01 10:02:00+00'),
('0190c000-0000-7000-8000-000000000015', 'macro|[email protected]', 'macro|[email protected]', 'opened', NULL, 'document', '11111111-ffff-ffff-ffff-ffffffffffff', '2024-06-01 10:15:00+00'),
('0190c000-0000-7000-8000-000000000011', 'macro|[email protected]', 'macro|[email protected]', 'messaged', NULL, 'channel', '33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:11:00+00'),
+('0190c000-0000-7000-8000-000000000010', 'macro|[email protected]', 'macro|[email protected]', 'edited', NULL, 'document', '11111111-9999-9999-9999-999999999999', '2024-06-01 10:10:00+00'),
('0190c000-0000-7000-8000-000000000013', 'macro|[email protected]', 'macro|[email protected]', 'sent', NULL, 'email_thread', '44444444-5555-5555-5555-555555555555', '2024-06-01 10:13:00+00'),
-- Opening doc-A after editing it must not move it.
('0190c000-0000-7000-8000-000000000016', 'macro|[email protected]', 'macro|[email protected]', 'opened', NULL, 'document', '11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa', '2024-06-01 10:16:00+00'),
diff --git a/crates/soup/src/domain/models.rs b/crates/soup/src/domain/models.rs
index d126d9975c..f5efdec328 100644
--- a/crates/soup/src/domain/models.rs
+++ b/crates/soup/src/domain/models.rs
@@ -965,8 +965,9 @@ pub struct WorkFeedCandidate {
/// The candidate's entity; channel-thread candidates are keyed on their
/// thread root as a `channel_message`, like the notified-at feed.
pub entity: Entity<'static>,
- /// The latest live notification keyed to the candidate, when at least
- /// one of them is not done.
+ /// The newest pending (unseen or seen) notification keyed to the
+ /// candidate. An own-work candidate omits attention older than the page's
+ /// oldest own work, which the query does not read and which cannot move it.
pub attention_at: Option<chrono::DateTime<chrono::Utc>>,
/// The viewer's latest own (non-view) activity on the candidate. Always
/// `None` in attention mode.
diff --git a/crates/soup/src/outbound/pg_soup_repo/work_feed.rs b/crates/soup/src/outbound/pg_soup_repo/work_feed.rs
index e91827e4b8..cc67a13588 100644
--- a/crates/soup/src/outbound/pg_soup_repo/work_feed.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/work_feed.rs
@@ -1,6 +1,6 @@
//! The work feed candidate query: one keyset-paginated page merging the
-//! viewer's attention (live notifications, at least one not done) with their
-//! own work (latest activity that counts as work), newest first.
+//! viewer's attention (the newest pending, i.e. unseen or seen, notification)
+//! with their own work (latest activity that counts as work), newest first.
//!
//! An item's place in the feed is `sort_at = max(attention_at, touched_at)`.
//! The query runs two streams, each ordered by its own timestamp: the
@@ -16,6 +16,13 @@
//! email thread's attention lasts only while the thread is in the inbox
//! (mail's own notion of done).
//!
+//! Done notifications never place an item, so the query reads only the
+//! viewer's pending ones, once, and both streams look attention up there.
+//! It also reads them only back to a floor: a full page of own work holds
+//! `limit` items placed at or after its oldest touch, so nothing placed
+//! earlier can reach the page. Reading every notification instead made a
+//! viewer with a long history pay for all of it on each page.
+//!
//! Like the notified-at feed, channel, channel-thread, email and
//! foreign-entity filter trees fold in their own domains, so the service
//! drops candidates their hydration legs reject and refills from the next
@@ -74,50 +81,6 @@ fn email_inbox_gate() -> String {
)
}
-/// The viewer's live notifications keyed to the candidate at `alias`, with
-/// `predicate` applied. Own-work candidates are never thread rows, so a
-/// channel candidate keeps only its channel-level notifications.
-fn keyed_notifications(alias: &str, predicate: &str) -> String {
- format!(
- r#"EXISTS (
- SELECT 1
- FROM notification n
- JOIN user_notification un ON un.notification_id = n.id
- WHERE un.user_id = $1
- AND un.deleted_at IS NULL
- AND n.event_item_type = {alias}.entity_type
- AND n.event_item_id = {alias}.entity_id
- AND ({alias}.entity_type <> 'channel'
- OR n.secondary_event_item_type IS DISTINCT FROM 'channel_message')
- AND {predicate}
- )"#
- )
-}
-
-/// The attention timestamp of an own-work row, computed exactly as the
-/// attention stream would: the newest live notification, when one is not
-/// done and (for email) the thread is in the inbox.
-fn own_attention_at() -> String {
- r#"(
- SELECT max(un.created_at) AT TIME ZONE 'UTC'
- FROM notification n
- JOIN user_notification un ON un.notification_id = n.id
- WHERE un.user_id = $1
- AND un.deleted_at IS NULL
- AND n.event_item_type = o.entity_type
- AND n.event_item_id = o.entity_id
- AND (o.entity_type <> 'channel'
- OR n.secondary_event_item_type IS DISTINCT FROM 'channel_message')
- AND (o.entity_type <> 'email_thread' OR EXISTS (
- SELECT 1 FROM email_threads inbox
- WHERE inbox.id::text = o.entity_id
- AND inbox.inbox_visible
- ))
- HAVING bool_or(un.state <> 'done')
- )"#
- .to_string()
-}
-
/// Whether the `activity_events` row aliased `alias` is the viewer's own work:
/// any action but a view, except that an email thread is only worked on by
/// sending from it. Archiving, starring, labeling and moving a thread record
@@ -139,10 +102,6 @@ fn build_query(req: &WorkFeedCandidateRequest<'_>) -> String {
own_work_ae = own_work("ae"),
own_work_newer = own_work("newer"),
email_inbox_gate = email_inbox_gate(),
- attention_pending = keyed_notifications("nc", "un.state <> 'done'"),
- attention_since_touch =
- keyed_notifications("nc", "un.created_at AT TIME ZONE 'UTC' >= nc.touched_at"),
- own_attention_at = own_attention_at(),
document_gate = document_gate(ID_SQL, filter),
chat_gate = chat_gate(ID_SQL, filter),
project_gate = project_gate(ID_SQL, filter),
diff --git a/crates/soup/src/outbound/pg_soup_repo/work_feed/query.sql b/crates/soup/src/outbound/pg_soup_repo/work_feed/query.sql
index ef211e79b0..e8e246a8fe 100644
--- a/crates/soup/src/outbound/pg_soup_repo/work_feed/query.sql
+++ b/crates/soup/src/outbound/pg_soup_repo/work_feed/query.sql
@@ -11,32 +11,93 @@ only_keys AS MATERIALIZED (
SELECT k.entity_type, k.entity_id
FROM unnest($10::text[], $11::text[]) AS k(entity_type, entity_id)
),
-notified AS NOT MATERIALIZED (
+-- The viewer's own work: their latest work on each item they may see,
+-- newest first.
+own_work AS NOT MATERIALIZED (
+ SELECT nc.entity_type, nc.entity_id, nc.touched_at
+ FROM (
+ SELECT ae.entity_type, ae.entity_id, ae.occurred_at AS touched_at
+ FROM activity_events ae
+ WHERE $9
+ AND ae.subject_id = $1
+ AND {own_work_ae}
+ AND ae.entity_type = ANY($2)
+ AND ae.entity_type <> 'channel_message'
+ AND ($3::timestamptz IS NULL OR (ae.occurred_at, ae.entity_id) < ($3, $4))
+ AND ($10::text[] IS NULL OR (ae.entity_type, ae.entity_id) IN (
+ SELECT entity_type, entity_id FROM only_keys
+ ))
+ AND NOT EXISTS (
+ SELECT 1 FROM activity_events newer
+ WHERE newer.subject_id = $1
+ AND newer.entity_type = ae.entity_type
+ AND newer.entity_id = ae.entity_id
+ AND {own_work_newer}
+ AND (newer.occurred_at, newer.id) > (ae.occurred_at, ae.id)
+ )
+ ORDER BY ae.occurred_at DESC, ae.entity_id DESC
+ OFFSET 0
+ ) nc
+ WHERE CASE nc.entity_type
+ WHEN 'document' THEN {document_gate}
+ WHEN 'chat' THEN {chat_gate}
+ WHEN 'project' THEN {project_gate}
+ WHEN 'initiative' THEN {initiative_gate}
+ WHEN 'channel' THEN {channel_gate}
+ WHEN 'email_thread' THEN {email_gate}
+ WHEN 'calendar_event' THEN {calendar_event_gate}
+ WHEN 'foreign_entity' THEN {foreign_entity_gate}
+ WHEN 'agent_session' THEN {agent_session_gate}
+ ELSE FALSE
+ END
+),
+-- A full page of own work, before attention claims any of it, holds $6
+-- distinct items placed at or after its oldest touch, so no item placed
+-- earlier can reach the page. Attention older than that floor is never read.
+-- Without a full page (attention mode, or little own work) there is no floor.
+attention_floor AS MATERIALIZED (
+ SELECT CASE WHEN count(*) = $6 THEN min(touched_at) END AS floor_at
+ FROM (
+ SELECT touched_at
+ FROM own_work
+ ORDER BY touched_at DESC, entity_id DESC
+ LIMIT $6
+ ) page_of_own_work
+),
+-- The viewer's attention: the newest pending (unseen or seen) notification
+-- keyed to each item. Only pending rows can place an item, so done ones are
+-- never read. Both streams look items up here, so it is built once.
+pending AS MATERIALIZED (
SELECT
- un.created_at,
- un.state,
- CASE WHEN n.event_item_type = 'channel'
- AND n.secondary_event_item_type = 'channel_message'
- THEN 'channel_message' ELSE n.event_item_type
- END AS entity_type,
- CASE WHEN n.event_item_type = 'channel'
- AND n.secondary_event_item_type = 'channel_message'
- THEN n.secondary_event_item_id ELSE n.event_item_id
- END AS entity_id
+ notified.entity_type,
+ notified.entity_id,
+ max(un.created_at) AT TIME ZONE 'UTC' AS attention_at
FROM user_notification un
- JOIN notification n ON n.id = un.notification_id
+ -- One primary-key probe per pending row. Estimates of both sides are
+ -- poor, and a hash join would scan every user's notifications instead.
+ CROSS JOIN LATERAL (
+ SELECT
+ CASE WHEN n.event_item_type = 'channel'
+ AND n.secondary_event_item_type = 'channel_message'
+ THEN 'channel_message' ELSE n.event_item_type
+ END AS entity_type,
+ CASE WHEN n.event_item_type = 'channel'
+ AND n.secondary_event_item_type = 'channel_message'
+ THEN n.secondary_event_item_id ELSE n.event_item_id
+ END AS entity_id
+ FROM notification n
+ WHERE n.id = un.notification_id
+ OFFSET 0
+ ) notified
WHERE un.user_id = $1
AND un.deleted_at IS NULL
-),
-attention AS NOT MATERIALIZED (
- SELECT
- entity_type,
- entity_id,
- max(created_at) AT TIME ZONE 'UTC' AS attention_at
- FROM notified
- WHERE entity_type = ANY($2)
- GROUP BY entity_type, entity_id
- HAVING bool_or(state <> 'done')
+ AND un.state <> 'done'
+ AND un.created_at >= COALESCE(
+ (SELECT floor_at AT TIME ZONE 'UTC' FROM attention_floor),
+ '-infinity'
+ )
+ AND notified.entity_type = ANY($2)
+ GROUP BY notified.entity_type, notified.entity_id
),
attention_page AS (
SELECT nc.entity_type, nc.entity_id, nc.attention_at, nc.touched_at
@@ -48,7 +109,7 @@ attention_page AS (
latest_own.occurred_at AS touched_at
FROM (
SELECT entity_type, entity_id, attention_at
- FROM attention
+ FROM pending
WHERE ($3::timestamptz IS NULL OR (attention_at, entity_id) < ($3, $4))
AND ($10::text[] IS NULL OR (entity_type, entity_id) IN (
SELECT entity_type, entity_id FROM only_keys
@@ -87,49 +148,22 @@ attention_page AS (
LIMIT $6
),
own_page AS (
- SELECT nc.entity_type, nc.entity_id, nc.touched_at
- FROM (
- SELECT ae.entity_type, ae.entity_id, ae.occurred_at AS touched_at
- FROM activity_events ae
- WHERE $9
- AND ae.subject_id = $1
- AND {own_work_ae}
- AND ae.entity_type = ANY($2)
- AND ae.entity_type <> 'channel_message'
- AND ($3::timestamptz IS NULL OR (ae.occurred_at, ae.entity_id) < ($3, $4))
- AND ($10::text[] IS NULL OR (ae.entity_type, ae.entity_id) IN (
- SELECT entity_type, entity_id FROM only_keys
- ))
- AND NOT EXISTS (
- SELECT 1 FROM activity_events newer
- WHERE newer.subject_id = $1
- AND newer.entity_type = ae.entity_type
- AND newer.entity_id = ae.entity_id
- AND {own_work_newer}
- AND (newer.occurred_at, newer.id) > (ae.occurred_at, ae.id)
- )
- ORDER BY ae.occurred_at DESC, ae.entity_id DESC
- OFFSET 0
- ) nc
- -- The attention stream owns a candidate whose live attention is at least
- -- as new as this touch; skipping it here emits every candidate once.
- WHERE NOT (
- {attention_pending}
- AND {attention_since_touch}
+ SELECT nc.entity_type, nc.entity_id, own_attention.attention_at, nc.touched_at
+ FROM own_work nc
+ -- A per-row lookup keeps the touch order, so the stream stops at the
+ -- limit; flattened into a hash join it would sort the whole history.
+ LEFT JOIN LATERAL (
+ SELECT p.attention_at
+ FROM pending p
+ WHERE p.entity_type = nc.entity_type
+ AND p.entity_id = nc.entity_id
AND (nc.entity_type <> 'email_thread' OR {email_inbox_gate})
- )
- AND CASE nc.entity_type
- WHEN 'document' THEN {document_gate}
- WHEN 'chat' THEN {chat_gate}
- WHEN 'project' THEN {project_gate}
- WHEN 'initiative' THEN {initiative_gate}
- WHEN 'channel' THEN {channel_gate}
- WHEN 'email_thread' THEN {email_gate}
- WHEN 'calendar_event' THEN {calendar_event_gate}
- WHEN 'foreign_entity' THEN {foreign_entity_gate}
- WHEN 'agent_session' THEN {agent_session_gate}
- ELSE FALSE
- END
+ OFFSET 0
+ ) own_attention ON TRUE
+ -- The attention stream owns a candidate whose attention is at least as
+ -- new as this touch; skipping it here emits every candidate once.
+ WHERE own_attention.attention_at IS NULL
+ OR own_attention.attention_at < nc.touched_at
ORDER BY nc.touched_at DESC, nc.entity_id DESC
LIMIT $6
)
@@ -138,8 +172,8 @@ FROM (
SELECT entity_type, entity_id, attention_at, touched_at, attention_at AS sort_at
FROM attention_page
UNION ALL
- SELECT o.entity_type, o.entity_id, {own_attention_at}, o.touched_at, o.touched_at AS sort_at
- FROM own_page o
+ SELECT entity_type, entity_id, attention_at, touched_at, touched_at AS sort_at
+ FROM own_page
) merged
ORDER BY sort_at DESC, entity_id DESC
LIMIT $6
diff --git a/crates/soup/src/outbound/pg_soup_repo/work_feed/test.rs b/crates/soup/src/outbound/pg_soup_repo/work_feed/test.rs
index 4a65b09460..293d2d06b4 100644
--- a/crates/soup/src/outbound/pg_soup_repo/work_feed/test.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/work_feed/test.rs
@@ -15,6 +15,7 @@ const DOC_C: &str = "11111111-cccc-cccc-cccc-cccccccccccc";
const DOC_E: &str = "11111111-eeee-eeee-eeee-eeeeeeeeeeee";
const DOC_F: &str = "11111111-ffff-ffff-ffff-ffffffffffff";
const DOC_G: &str = "11111111-0000-0000-0000-000000000000";
+const DOC_H: &str = "11111111-9999-9999-9999-999999999999";
const CHAT_A: &str = "22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const CHANNEL_X: &str = "33333333-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const THREAD_M: &str = "99999999-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
@@ -83,6 +84,23 @@ fn sources() -> Vec<SourceId> {
vec![SourceId::user(USER_1)]
}
+/// The fixture's whole work-mode feed, newest first.
+fn work_feed_order() -> Vec<(EntityType, String)> {
+ vec![
+ (EntityType::EmailThread, THREAD_S.to_string()),
+ (EntityType::EmailThread, THREAD_Z.to_string()),
+ (EntityType::Channel, CHANNEL_X.to_string()),
+ (EntityType::Document, DOC_H.to_string()),
+ (EntityType::Document, DOC_A.to_string()),
+ (EntityType::Document, DOC_B.to_string()),
+ (EntityType::Document, DOC_G.to_string()),
+ (EntityType::Document, DOC_C.to_string()),
+ (EntityType::Chat, CHAT_A.to_string()),
+ (EntityType::Document, DOC_E.to_string()),
+ (EntityType::ChannelMessage, THREAD_M.to_string()),
+ ]
+}
+
#[sqlx::test(
fixtures(path = "../../../../fixtures", scripts("work_feed")),
migrator = "MACRO_DB_MIGRATIONS"
@@ -92,21 +110,7 @@ async fn work_mode_merges_attention_and_own_work(pool: Pool<Postgres>) -> anyhow
let sources = sources();
let page = work_feed_soup_page(&pool, req(WorkFeedSoupMode::Work, &link_ids, &sources)).await?;
- assert_eq!(
- keys(&page),
- vec![
- (EntityType::EmailThread, THREAD_S.to_string()),
- (EntityType::EmailThread, THREAD_Z.to_string()),
- (EntityType::Channel, CHANNEL_X.to_string()),
- (EntityType::Document, DOC_A.to_string()),
- (EntityType::Document, DOC_B.to_string()),
- (EntityType::Document, DOC_G.to_string()),
- (EntityType::Document, DOC_C.to_string()),
- (EntityType::Chat, CHAT_A.to_string()),
- (EntityType::Document, DOC_E.to_string()),
- (EntityType::ChannelMessage, THREAD_M.to_string()),
- ]
- );
+ assert_eq!(keys(&page), work_feed_order());
assert_eq!(
minutes(&page),
vec![
@@ -117,6 +121,9 @@ async fn work_mode_merges_attention_and_own_work(pool: Pool<Postgres>) -> anyhow
(12, Some(12), None),
// channel-X: the thread mention belongs to thread-M.
(11, None, Some(11)),
+ // doc-H: attention is its newest pending notification; the newer
+ // done one neither places it nor outranks the edit.
+ (10, Some(6), Some(10)),
// doc-A: the later opened row does not count as own work.
(9, Some(5), Some(9)),
// doc-B: user-2's later edit is not user-1's own work.
@@ -150,13 +157,16 @@ async fn attention_mode_ignores_own_work(pool: Pool<Postgres>) -> anyhow::Result
(EntityType::EmailThread, THREAD_Z.to_string()),
(EntityType::Document, DOC_B.to_string()),
(EntityType::Document, DOC_G.to_string()),
+ (EntityType::Document, DOC_H.to_string()),
(EntityType::Document, DOC_A.to_string()),
(EntityType::Chat, CHAT_A.to_string()),
(EntityType::ChannelMessage, THREAD_M.to_string()),
]
);
assert!(page.iter().all(|c| c.touched_at.is_none()));
- assert_eq!(page[3].sort_at.minute(), 5);
+ // doc-H sits at its pending notification, not its newer done one.
+ assert_eq!(page[3].sort_at.minute(), 6);
+ assert_eq!(page[4].sort_at.minute(), 5);
Ok(())
}
@@ -193,7 +203,28 @@ async fn keyset_paginates_across_both_streams(pool: Pool<Postgres>) -> anyhow::R
let one_page =
work_feed_soup_page(&pool, req(WorkFeedSoupMode::Work, &link_ids, &sources)).await?;
assert_eq!(keys(&all), keys(&one_page));
- assert_eq!(all.len(), 10);
+ assert_eq!(all.len(), 11);
+
+ Ok(())
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../../fixtures", scripts("work_feed")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn every_page_size_matches_the_whole_feed(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let link_ids = [Uuid::parse_str(LINK_1)?];
+ let sources = sources();
+ let whole = work_feed_order();
+
+ // A full page of own work bounds how far back attention is read; each
+ // smaller page must still be the head of the whole feed.
+ for limit in 1..=whole.len() {
+ let mut request = req(WorkFeedSoupMode::Work, &link_ids, &sources);
+ request.limit = u16::try_from(limit)?;
+ let page = work_feed_soup_page(&pool, request).await?;
+ assert_eq!(keys(&page), whole[..limit], "limit {limit}");
+ }
Ok(())
}