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.

[db-perf] perf(soup): read Home unread notifications once per list (#9580)

teo40450104d17e
diff --git a/.sqlx/query-25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209.json b/.sqlx/query-32ca8ccd308eebdc3daf7555114ff2b980ff31abcbb28f99dcda7e8dee50c0f0.json
similarity index 73%
rename from .sqlx/query-25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209.json
rename to .sqlx/query-32ca8ccd308eebdc3daf7555114ff2b980ff31abcbb28f99dcda7e8dee50c0f0.json
index 5c5f0a7eb8..c603d40e65 100644
--- a/.sqlx/query-25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209.json
+++ b/.sqlx/query-32ca8ccd308eebdc3daf7555114ff2b980ff31abcbb28f99dcda7e8dee50c0f0.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH source_ids AS (\n SELECT DISTINCT stored_for_id, stored_for_auth_entity\n FROM UNNEST($1::text[], $2::text[])\n AS source_rows(stored_for_id, stored_for_auth_entity)\n ),\n deduped AS (\n SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id,\n CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END)\n fe.id,\n fe.foreign_entity_id,\n fe.foreign_entity_source,\n fe.metadata,\n fe.stored_for_id,\n fe.stored_for_auth_entity,\n fe.created_at,\n COALESCE(gpr.github_updated_at, fe.updated_at) AS updated_at,\n CASE $3::text\n WHEN 'created_at' THEN fe.created_at\n ELSE COALESCE(gpr.github_updated_at, fe.updated_at)\n END AS sort_at\n FROM foreign_entity fe\n LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id\n WHERE fe.foreign_entity_source = $10::text\n AND ($13::uuid[] IS NULL OR fe.id = ANY($13))\n AND EXISTS (\n SELECT 1\n FROM source_ids s\n WHERE s.stored_for_id = fe.stored_for_id\n AND s.stored_for_auth_entity = fe.stored_for_auth_entity\n )\n AND (\n $4::text IS NULL\n OR jsonb_path_match(\n jsonb_build_object(\n 'id', fe.id::text,\n 'foreignEntityId', fe.foreign_entity_id,\n 'foreignEntitySource', fe.foreign_entity_source\n ),\n ($4::text)::jsonpath\n )\n )\n AND (\n $11::text IS NULL\n OR (gpr.github_key IS NOT NULL AND jsonb_path_match(\n jsonb_build_object(\n 'repositoryId', gpr.repository_id,\n 'authorId', gpr.author_github_user_id,\n 'status', gpr.status,\n 'draft', gpr.draft,\n 'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),\n 'participants', to_jsonb(gpr.participant_github_user_ids),\n 'assignees', jsonb_path_query_array(gpr.assignees, '$[*].githubUserId'),\n 'labels', jsonb_path_query_array(gpr.labels, '$[*].name'),\n 'reviewers', jsonb_path_query_array(gpr.reviews, '$[*].reviewerGithubUserId'),\n 'reviewDecision', gpr.review_decision,\n 'reviewCount', jsonb_array_length(gpr.reviews)\n ),\n ($11::text)::jsonpath\n ))\n )\n AND (\n $8::int[] IS NULL\n OR ($9::text IS NOT NULL AND (\n SELECT COALESCE(bit_or(CASE un.state\n WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)\n FROM notification n\n JOIN user_notification un ON un.notification_id = n.id\n WHERE un.user_id = $9::text\n AND un.deleted_at IS NULL\n AND n.event_item_type = 'foreign_entity'\n AND n.event_item_id = fe.id::text\n ) = ANY($8::int[]))\n )\n ORDER BY\n fe.foreign_entity_source,\n fe.foreign_entity_id,\n CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END,\n sort_at DESC,\n fe.updated_at DESC,\n fe.id DESC\n )\n SELECT\n id as \"id!: Uuid\",\n foreign_entity_id as \"foreign_entity_id!: String\",\n foreign_entity_source as \"foreign_entity_source!: String\",\n metadata as \"metadata!: serde_json::Value\",\n stored_for_id as \"stored_for_id!: String\",\n stored_for_auth_entity as \"stored_for_auth_entity!: String\",\n created_at as \"created_at!: DateTime<Utc>\",\n updated_at as \"updated_at!: DateTime<Utc>\"\n FROM deduped\n WHERE $5::timestamptz IS NULL\n OR ($12::bool AND (sort_at, id) > ($5::timestamptz, $6::uuid))\n OR (NOT $12::bool AND (sort_at, id) < ($5::timestamptz, $6::uuid))\n ORDER BY\n CASE WHEN $12::bool THEN sort_at END ASC,\n CASE WHEN $12::bool THEN id END ASC,\n sort_at DESC,\n id DESC\n LIMIT $7\n ",
+ "query": "\n WITH source_ids AS (\n SELECT DISTINCT stored_for_id, stored_for_auth_entity\n FROM UNNEST($1::text[], $2::text[])\n AS source_rows(stored_for_id, stored_for_auth_entity)\n ),\n deduped AS (\n SELECT DISTINCT ON (fe.foreign_entity_source, fe.foreign_entity_id,\n CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END)\n fe.id,\n fe.foreign_entity_id,\n fe.foreign_entity_source,\n fe.metadata,\n fe.stored_for_id,\n fe.stored_for_auth_entity,\n fe.created_at,\n COALESCE(gpr.github_updated_at, fe.updated_at) AS updated_at,\n CASE $3::text\n WHEN 'created_at' THEN fe.created_at\n ELSE COALESCE(gpr.github_updated_at, fe.updated_at)\n END AS sort_at\n FROM foreign_entity fe\n LEFT JOIN github_pull_request gpr ON gpr.github_key = fe.foreign_entity_id\n WHERE fe.foreign_entity_source = $10::text\n AND ($13::uuid[] IS NULL OR fe.id = ANY($13))\n AND EXISTS (\n SELECT 1\n FROM source_ids s\n WHERE s.stored_for_id = fe.stored_for_id\n AND s.stored_for_auth_entity = fe.stored_for_auth_entity\n )\n AND (\n $4::text IS NULL\n OR jsonb_path_match(\n jsonb_build_object(\n 'id', fe.id::text,\n 'foreignEntityId', fe.foreign_entity_id,\n 'foreignEntitySource', fe.foreign_entity_source\n ),\n ($4::text)::jsonpath\n )\n )\n AND (\n $11::text IS NULL\n OR (gpr.github_key IS NOT NULL AND jsonb_path_match(\n jsonb_build_object(\n 'repositoryId', gpr.repository_id,\n 'authorId', gpr.author_github_user_id,\n 'status', gpr.status,\n 'draft', gpr.draft,\n 'requestedReviewers', to_jsonb(gpr.requested_reviewer_github_user_ids),\n 'participants', to_jsonb(gpr.participant_github_user_ids),\n 'assignees', jsonb_path_query_array(gpr.assignees, '$[*].githubUserId'),\n 'labels', jsonb_path_query_array(gpr.labels, '$[*].name'),\n 'reviewers', jsonb_path_query_array(gpr.reviews, '$[*].reviewerGithubUserId'),\n 'reviewDecision', gpr.review_decision,\n 'reviewCount', jsonb_array_length(gpr.reviews)\n ),\n ($11::text)::jsonpath\n ))\n )\n AND (\n $8::int[] IS NULL\n OR ($9::text IS NOT NULL AND (\n SELECT COALESCE(bit_or(CASE un.state\n WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)\n FROM user_notification un\n WHERE un.user_id = $9::text\n AND un.deleted_at IS NULL\n AND un.event_item_type = 'foreign_entity'\n AND un.event_item_id = fe.id::text\n ) = ANY($8::int[]))\n )\n ORDER BY\n fe.foreign_entity_source,\n fe.foreign_entity_id,\n CASE WHEN $13::uuid[] IS NOT NULL THEN fe.id END,\n sort_at DESC,\n fe.updated_at DESC,\n fe.id DESC\n )\n SELECT\n id as \"id!: Uuid\",\n foreign_entity_id as \"foreign_entity_id!: String\",\n foreign_entity_source as \"foreign_entity_source!: String\",\n metadata as \"metadata!: serde_json::Value\",\n stored_for_id as \"stored_for_id!: String\",\n stored_for_auth_entity as \"stored_for_auth_entity!: String\",\n created_at as \"created_at!: DateTime<Utc>\",\n updated_at as \"updated_at!: DateTime<Utc>\"\n FROM deduped\n WHERE $5::timestamptz IS NULL\n OR ($12::bool AND (sort_at, id) > ($5::timestamptz, $6::uuid))\n OR (NOT $12::bool AND (sort_at, id) < ($5::timestamptz, $6::uuid))\n ORDER BY\n CASE WHEN $12::bool THEN sort_at END ASC,\n CASE WHEN $12::bool THEN id END ASC,\n sort_at DESC,\n id DESC\n LIMIT $7\n ",
"describe": {
"columns": [
{
@@ -72,5 +72,5 @@
null
]
},
- "hash": "25fc7e8600d28e94dd5cc2a1422863f16730f8c494b8467d1c78a04ddf57d209"
+ "hash": "32ca8ccd308eebdc3daf7555114ff2b980ff31abcbb28f99dcda7e8dee50c0f0"
}
diff --git a/crates/channels/AGENTS.md b/crates/channels/AGENTS.md
index 98d2cf9f67..b93e3444b7 100644
--- a/crates/channels/AGENTS.md
+++ b/crates/channels/AGENTS.md
@@ -98,6 +98,10 @@ DMs, and the shared message store other domains discuss things on.
user's notifications (`= ANY(ARRAY(...))`). A per-channel `EXISTS` or plain
`IN` walks every candidate channel's notification history; that was ~24% of
the prod read replica's buffer hits.
+- Notification filters read `user_notification`'s copied entity columns, not a
+ `notification` join, and compare `c.id` as a uuid so the channel primary key
+ stays usable. Web Home sends `(unseen OR seen) AND unseen`;
+ `drop_implied_notification_conjuncts` folds that to one notification read.
## Related
diff --git a/crates/channels/src/outbound/pg_channels_repo.rs b/crates/channels/src/outbound/pg_channels_repo.rs
index 7364be5ea4..fa80ba3497 100644
--- a/crates/channels/src/outbound/pg_channels_repo.rs
+++ b/crates/channels/src/outbound/pg_channels_repo.rs
@@ -543,23 +543,6 @@ static CHANNEL_LIST_SELECT: &str = r#"
ELSE uc.updated_at
END) DESC, uc.id::text DESC
LIMIT $3
- ),
- channel_participants_json AS (
- SELECT
- pc.id as channel_id,
- ARRAY_AGG(
- json_build_object(
- 'channel_id', cp.channel_id,
- 'user_id', cp.user_id,
- 'role', cp.role,
- 'joined_at', cp.joined_at,
- 'left_at', cp.left_at
- )
- ) as participants
- FROM paged_channels pc
- JOIN comms_channel_participants cp ON cp.channel_id = pc.id
- WHERE cp.left_at IS NULL
- GROUP BY pc.id
)
SELECT
pc.id as "id",
@@ -580,7 +563,22 @@ static CHANNEL_LIST_SELECT: &str = r#"
AND cp_active.left_at IS NULL
) as "is_participant"
FROM paged_channels pc
- LEFT JOIN channel_participants_json cpj ON cpj.channel_id = pc.id
+ -- Per channel: a grouped join over the whole page is re-aggregated once
+ -- per page row when the page is estimated at one row.
+ LEFT JOIN LATERAL (
+ SELECT ARRAY_AGG(
+ json_build_object(
+ 'channel_id', cp.channel_id,
+ 'user_id', cp.user_id,
+ 'role', cp.role,
+ 'joined_at', cp.joined_at,
+ 'left_at', cp.left_at
+ )
+ ) as participants
+ FROM comms_channel_participants cp
+ WHERE cp.channel_id = pc.id
+ AND cp.left_at IS NULL
+ ) cpj ON TRUE
ORDER BY (CASE $2
WHEN 'created_at' THEN pc.created_at
WHEN 'viewed_at' THEN COALESCE(pc.viewed_at, '1970-01-01 00:00:00+00')
@@ -591,45 +589,130 @@ static CHANNEL_LIST_SELECT: &str = r#"
/// Matches channels where the user has a live notification in `state`.
///
-/// Unseen and seen rows are few per user and usually the newest of a channel,
-/// so a per-channel `EXISTS` walks the channel's whole notification history
-/// before finding them. Those states read the user's own notifications once
-/// instead. `= ANY(ARRAY(...))` keeps that set an InitPlan; a plain `IN` is
-/// pulled up into a semi-join that probes every channel's history again.
-/// Done rows accumulate without bound and the oldest one usually matches, so
-/// the per-channel `EXISTS` stays cheaper for them.
+/// Both forms read only the user's own channel rows through
+/// `idx_user_notification_viewer_entity`, which carries the entity, so neither
+/// joins `notification`. Unseen and seen rows are few per user, so those states
+/// collect the user's notified channels once: `= ANY(ARRAY(...))` keeps that
+/// set an InitPlan, where a plain `IN` is pulled up into a per-channel
+/// semi-join. Done rows accumulate without bound, so done seeks the index once
+/// per candidate channel instead.
#[cfg(feature = "list")]
fn build_channel_notification_state_clause(state: item_filters::NotificationState) -> String {
let predicate = notification_state_predicate(state);
match state {
item_filters::NotificationState::Unseen | item_filters::NotificationState::Seen => {
format!(
- r#"(c.id)::text = ANY(ARRAY(
- SELECT DISTINCT n.event_item_id
+ r#"c.id = ANY(ARRAY(
+ SELECT DISTINCT un.event_item_id::uuid
FROM user_notification un
- JOIN notification n ON n.id = un.notification_id
WHERE un.user_id = $1
AND un.deleted_at IS NULL
+ AND un.event_item_type = 'channel'
AND {predicate}
- AND n.event_item_type = 'channel'
+ AND un.event_item_id ~ '{UUID_TEXT_PATTERN}'
))"#
)
}
item_filters::NotificationState::Done => format!(
r#"EXISTS (
SELECT 1
- FROM notification n
- JOIN user_notification un ON un.notification_id = n.id
+ FROM user_notification un
WHERE un.user_id = $1
AND un.deleted_at IS NULL
- AND n.event_item_type = 'channel'
- AND n.event_item_id = (c.id)::text
+ AND un.event_item_type = 'channel'
+ AND un.event_item_id = (c.id)::text
AND {predicate}
)"#
),
}
}
+#[cfg(feature = "list")]
+fn notification_state_bit(state: item_filters::NotificationState) -> u8 {
+ match state {
+ item_filters::NotificationState::Unseen => 1,
+ item_filters::NotificationState::Seen => 2,
+ item_filters::NotificationState::Done => 4,
+ }
+}
+
+/// States matched by a subtree made only of notification literals joined by OR.
+#[cfg(feature = "list")]
+fn notification_state_set<T>(
+ expr: &Expr<T>,
+ state_of: impl Fn(&T) -> Option<item_filters::NotificationState> + Copy,
+) -> Option<u8> {
+ match expr {
+ Expr::Literal(literal) => state_of(literal).map(notification_state_bit),
+ Expr::Or(left, right) => {
+ Some(notification_state_set(left, state_of)? | notification_state_set(right, state_of)?)
+ }
+ Expr::And(..) | Expr::Not(_) => None,
+ }
+}
+
+#[cfg(feature = "list")]
+fn collect_conjuncts<'a, T>(expr: &'a Expr<T>, conjuncts: &mut Vec<&'a Expr<T>>) {
+ match expr {
+ Expr::And(left, right) => {
+ collect_conjuncts(left, conjuncts);
+ collect_conjuncts(right, conjuncts);
+ }
+ _ => conjuncts.push(expr),
+ }
+}
+
+/// Drops each notification conjunct that a narrower one on the same AND chain
+/// implies. The Home unread feed sends `(unseen OR seen) AND unseen`, which
+/// matches exactly what `unseen` does, but every notification conjunct is its
+/// own read of the user's notifications.
+#[cfg(feature = "list")]
+fn drop_implied_notification_conjuncts<T: Clone>(
+ expr: &Expr<T>,
+ state_of: impl Fn(&T) -> Option<item_filters::NotificationState> + Copy,
+) -> Expr<T> {
+ let mut conjuncts = Vec::new();
+ collect_conjuncts(expr, &mut conjuncts);
+ let sets: Vec<Option<u8>> = conjuncts
+ .iter()
+ .map(|conjunct| notification_state_set(conjunct, state_of))
+ .collect();
+ let implied = |index: usize, set: u8| {
+ sets.iter().enumerate().any(|(other_index, other)| {
+ other.is_some_and(|other| {
+ other_index != index
+ && other & set == other
+ && (other != set || other_index < index)
+ })
+ })
+ };
+ conjuncts
+ .into_iter()
+ .enumerate()
+ .filter(|(index, _)| sets[*index].is_none_or(|set| !implied(*index, set)))
+ .map(|(_, conjunct)| conjunct.clone())
+ .reduce(Expr::and)
+ .unwrap_or_else(|| expr.clone())
+}
+
+#[cfg(feature = "list")]
+fn channel_literal_state(literal: &ChannelLiteral) -> Option<item_filters::NotificationState> {
+ match literal {
+ ChannelLiteral::NotificationState(state) => Some(*state),
+ _ => None,
+ }
+}
+
+#[cfg(feature = "list")]
+fn channel_thread_literal_state(
+ literal: &ChannelThreadLiteral,
+) -> Option<item_filters::NotificationState> {
+ match literal {
+ ChannelThreadLiteral::NotificationState(state) => Some(*state),
+ _ => None,
+ }
+}
+
#[cfg(feature = "list")]
fn build_channel_list_filter(ast: Option<&Expr<ChannelLiteral>>) -> String {
let Some(expr) = ast else {
@@ -704,22 +787,38 @@ fn channel_filter_mentions_participation(expr: &Expr<ChannelLiteral>) -> bool {
})
}
+/// Whether `IsParticipant(true)` is on the top-level AND chain, so only
+/// channels the user participates in can match.
+#[cfg(feature = "list")]
+fn channel_filter_requires_participation(expr: &Expr<ChannelLiteral>) -> bool {
+ match expr {
+ Expr::And(left, right) => {
+ channel_filter_requires_participation(left)
+ || channel_filter_requires_participation(right)
+ }
+ Expr::Literal(ChannelLiteral::IsParticipant(true)) => true,
+ _ => false,
+ }
+}
+
#[cfg(feature = "list")]
fn build_channel_list_query(
filter_ast: &LiteralTree<ChannelLiteral>,
-) -> QueryBuilder<'_, Postgres> {
+) -> QueryBuilder<'static, Postgres> {
// QueryBuilder is required because the channel filter is an AST with arbitrary
// AND/OR/NOT shape. Dynamic literals are constrained to parsed domain types.
- let prefix = if filter_ast
+ let filter = filter_ast
.as_deref()
- .is_some_and(channel_filter_mentions_participation)
- {
+ .map(|expr| drop_implied_notification_conjuncts(expr, channel_literal_state));
+ let prefix = if filter.as_ref().is_some_and(|expr| {
+ channel_filter_mentions_participation(expr) && !channel_filter_requires_participation(expr)
+ }) {
CHANNEL_LIST_PREFIX_WITH_TEAM_CHANNELS
} else {
CHANNEL_LIST_PREFIX
};
let mut builder = QueryBuilder::new(prefix);
- builder.push(build_channel_list_filter(filter_ast.as_deref()));
+ builder.push(build_channel_list_filter(filter.as_ref()));
builder.push(CHANNEL_LIST_SELECT);
builder
}
@@ -895,8 +994,7 @@ fn push_channel_thread_notification_filter_expr(
}
#[cfg(feature = "list")]
-const CHANNEL_MESSAGE_ID_PATTERN: &str =
- r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
+const UUID_TEXT_PATTERN: &str = r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
/// Positive notification states, plus predicates that were ANDed onto them.
///
@@ -929,7 +1027,9 @@ fn channel_thread_expr_mentions_notification(expr: &Expr<ChannelThreadLiteral>)
/// Root ids of channel-message notifications in `state_predicate`.
///
/// `$1` is the requesting user, the first bind `build_channel_thread_notification_query`
-/// pushes. Each arm fences the user's rows and the notification lookup with
+/// pushes. The user's rows are narrowed to channel notifications through
+/// `idx_user_notification_viewer_entity` before any `notification` lookup.
+/// Each arm fences the user's rows and the notification lookup with
/// `OFFSET 0`. Without the inner fence Postgres hashes every `channel_message`
/// notification in the table when that type's statistics are too low. AND is
/// `INTERSECT` because two states can be witnessed by different notification
@@ -944,6 +1044,7 @@ fn channel_message_notification_ids_sql(state_predicate: &str) -> String {
FROM user_notification un
WHERE un.user_id = $1
AND un.deleted_at IS NULL
+ AND un.event_item_type = 'channel'
AND {state_predicate}
OFFSET 0
) un
@@ -953,7 +1054,7 @@ fn channel_message_notification_ids_sql(state_predicate: &str) -> String {
WHERE n.id = un.notification_id
AND n.event_item_type = 'channel'
AND n.secondary_event_item_type = 'channel_message'
- AND n.secondary_event_item_id ~ '{CHANNEL_MESSAGE_ID_PATTERN}'
+ AND n.secondary_event_item_id ~ '{UUID_TEXT_PATTERN}'
OFFSET 0
) n ON TRUE"#
)
@@ -1153,7 +1254,11 @@ fn build_channel_thread_rows_query(
let cursor_id_str = cursor_id.map(|id| id.to_string());
let cursor_timestamp = cursor_timestamp.cloned();
- if let Some(expr) = cursor.filter().as_deref()
+ let filter = cursor
+ .filter()
+ .as_deref()
+ .map(|expr| drop_implied_notification_conjuncts(expr, channel_thread_literal_state));
+ if let Some(expr) = filter.as_ref()
&& let ThreadNotifPlan::Ready { ids_sql, residuals } =
classify_channel_thread_notification(expr)
{
@@ -1203,7 +1308,7 @@ fn build_channel_thread_rows_query(
"#,
);
- if let Some(expr) = cursor.filter().as_deref() {
+ if let Some(expr) = filter.as_ref() {
builder.push(" AND ");
push_channel_thread_filter_expr(&mut builder, expr, params.user());
}
diff --git a/crates/channels/src/outbound/pg_channels_repo/tests.rs b/crates/channels/src/outbound/pg_channels_repo/tests.rs
index c209850bba..254f515e35 100644
--- a/crates/channels/src/outbound/pg_channels_repo/tests.rs
+++ b/crates/channels/src/outbound/pg_channels_repo/tests.rs
@@ -1686,15 +1686,22 @@ async fn insert_user_notification(
) -> anyhow::Result<()> {
sqlx::query!(
r#"
- INSERT INTO user_notification (user_id, notification_id, created_at, seen_at, state)
- VALUES (
+ INSERT INTO user_notification (
+ user_id, notification_id, created_at, seen_at, state,
+ event_item_type, event_item_id, notification_event_type
+ )
+ SELECT
$1,
- $2,
+ n.id,
'2024-01-02 00:00:00'::timestamp,
CASE WHEN $3::bool THEN '2024-01-02 00:00:00'::timestamp ELSE NULL END,
CASE WHEN $4::bool THEN 'done'::notification_state
- WHEN $3 THEN 'seen'::notification_state ELSE 'unseen'::notification_state END
- )
+ WHEN $3 THEN 'seen'::notification_state ELSE 'unseen'::notification_state END,
+ n.event_item_type,
+ n.event_item_id,
+ n.notification_event_type
+ FROM notification n
+ WHERE n.id = $2
"#,
user_id,
notification_id,
@@ -2922,3 +2929,158 @@ async fn channel_picture_round_trips_through_batched_previews(pool: Pool<Postgre
assert!(previews[0].has_access);
}
}
+
+/// Home's unread presence filter: `done = false`, a scope literal, `seen = false`.
+fn home_unread<T>(
+ state: impl Fn(item_filters::NotificationState) -> Expr<T>,
+ scope: Expr<T>,
+) -> Expr<T> {
+ use item_filters::NotificationState::{Seen, Unseen};
+ Expr::and(
+ Expr::and(Expr::or(state(Unseen), state(Seen)), scope),
+ state(Unseen),
+ )
+}
+
+fn assert_same(actual: Expr<ChannelLiteral>, expected: Expr<ChannelLiteral>) {
+ assert_eq!(format!("{actual:?}"), format!("{expected:?}"));
+}
+
+#[test]
+fn implied_notification_conjuncts_are_dropped() {
+ use item_filters::NotificationState::{Done, Seen, Unseen};
+ let state_of = super::channel_literal_state;
+ let participant = Expr::val(ChannelLiteral::IsParticipant(true));
+
+ assert_same(
+ super::drop_implied_notification_conjuncts(
+ &home_unread(channel_state, participant.clone()),
+ state_of,
+ ),
+ Expr::and(participant.clone(), channel_state(Unseen)),
+ );
+ // Two states on one chain can be witnessed by different notifications.
+ let both = Expr::and(channel_state(Unseen), channel_state(Seen));
+ assert_same(
+ super::drop_implied_notification_conjuncts(&both, state_of),
+ both,
+ );
+ let repeated = Expr::and(channel_state(Done), channel_state(Done));
+ assert_same(
+ super::drop_implied_notification_conjuncts(&repeated, state_of),
+ channel_state(Done),
+ );
+ let negated = Expr::and(
+ Expr::is_not(channel_state(Done)),
+ Expr::or(channel_state(Unseen), channel_state(Seen)),
+ );
+ assert_same(
+ super::drop_implied_notification_conjuncts(&negated, state_of),
+ negated,
+ );
+ let mixed = Expr::and(
+ Expr::or(channel_state(Unseen), participant.clone()),
+ channel_state(Unseen),
+ );
+ assert_same(
+ super::drop_implied_notification_conjuncts(&mixed, state_of),
+ mixed,
+ );
+}
+
+#[test]
+fn home_unread_channel_list_reads_notifications_once() {
+ let filter = home_unread(
+ channel_state,
+ Expr::val(ChannelLiteral::IsParticipant(true)),
+ );
+ let sql = super::build_channel_list_query(&Some(Arc::new(filter)))
+ .sql()
+ .to_owned();
+ assert_eq!(sql.matches("FROM user_notification").count(), 1, "{sql}");
+ assert!(!sql.contains("JOIN notification"), "{sql}");
+ assert!(!sql.contains("team_user"), "{sql}");
+}
+
+#[test]
+fn home_unread_thread_feed_reads_notifications_once() {
+ let state = |state| Expr::val(ChannelThreadLiteral::NotificationState(state));
+ let filter = home_unread(
+ state,
+ Expr::val(ChannelThreadLiteral::Participant(macro_user_id(USER_A))),
+ );
+ let params = thread_rows_request(
+ USER_A,
+ Some(Arc::new(filter)),
+ SimpleSortMethod::UpdatedAt,
+ 50,
+ )
+ .into_params();
+ let sql = super::build_channel_thread_rows_query(&params)
+ .sql()
+ .to_owned();
+ assert_eq!(sql.matches("FROM user_notification").count(), 1, "{sql}");
+ assert!(!sql.contains("INTERSECT"), "{sql}");
+ assert!(!sql.contains("UNION"), "{sql}");
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn home_unread_channel_list_matches_unseen_participant_channels(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ use item_filters::NotificationState::Unseen;
+
+ seed_channel_list_notifications(&pool).await?;
+ let repo = repo(pool);
+ let participant = || Expr::val(ChannelLiteral::IsParticipant(true));
+
+ // c13 has a seen and an unseen notification; c11's is unseen; left-user
+ // left c12, and c21's unseen one is soft-deleted.
+ let home =
+ channel_ids_matching(&repo, LEFT_USER, home_unread(channel_state, participant())).await;
+ assert_eq!(home, HashSet::from([TEAM_A_AUTO_ACTIVE, TEAM_A_MANUAL]));
+ assert_eq!(
+ home,
+ channel_ids_matching(
+ &repo,
+ LEFT_USER,
+ Expr::and(channel_state(Unseen), participant())
+ )
+ .await
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ fixtures(path = "../../../fixtures", scripts("channels_repo")),
+ migrator = "MACRO_DB_MIGRATIONS"
+)]
+async fn home_unread_thread_feed_matches_unseen_participant_threads(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ insert_channel_thread_notification(&pool, USER_A, CH1, MSG3, false, false).await?;
+ insert_channel_thread_notification(&pool, USER_A, CH1, MSG1, false, true).await?;
+ insert_channel_thread_notification(&pool, USER_A, CH1, MSG31, true, false).await?;
+ insert_channel_thread_notification(&pool, USER_B, CH1, MSG1, false, false).await?;
+
+ let state = |state| Expr::val(ChannelThreadLiteral::NotificationState(state));
+ let participant = || Expr::val(ChannelThreadLiteral::Participant(macro_user_id(USER_A)));
+ let home = thread_ids_matching(pool.clone(), USER_A, home_unread(state, participant())).await?;
+ assert_eq!(home, vec![MSG3]);
+ assert_eq!(
+ home,
+ thread_ids_matching(
+ pool,
+ USER_A,
+ Expr::and(
+ state(item_filters::NotificationState::Unseen),
+ participant()
+ ),
+ )
+ .await?
+ );
+ Ok(())
+}
diff --git a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
index cca3e2114f..505065f056 100644
--- a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
+++ b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing.rs
@@ -373,12 +373,11 @@ impl PgGithubPullRequestRepo {
OR ($9::text IS NOT NULL AND (
SELECT COALESCE(bit_or(CASE un.state
WHEN 'unseen' THEN 1 WHEN 'seen' THEN 2 WHEN 'done' THEN 4 END), 0)
- FROM notification n
- JOIN user_notification un ON un.notification_id = n.id
+ FROM user_notification un
WHERE un.user_id = $9::text
AND un.deleted_at IS NULL
- AND n.event_item_type = 'foreign_entity'
- AND n.event_item_id = fe.id::text
+ AND un.event_item_type = 'foreign_entity'
+ AND un.event_item_id = fe.id::text
) = ANY($8::int[]))
)
ORDER BY
diff --git a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
index e4abb7181f..635827ccbe 100644
--- a/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
+++ b/crates/github_pull_requests/src/outbound/pg_github_pull_request_repo/listing/test.rs
@@ -236,15 +236,20 @@ async fn insert_foreign_entity_notification(
let seen_at: Option<chrono::NaiveDateTime> = seen.then(|| Utc::now().naive_utc());
sqlx::query!(
r#"
- INSERT INTO user_notification (user_id, notification_id, state, seen_at)
+ INSERT INTO user_notification (
+ user_id, notification_id, state, seen_at,
+ event_item_type, event_item_id, notification_event_type
+ )
VALUES ($1, $2, CASE WHEN $3::bool THEN 'done'::notification_state
WHEN $4::timestamp IS NOT NULL THEN 'seen'::notification_state
- ELSE 'unseen'::notification_state END, $4)
+ ELSE 'unseen'::notification_state END, $4,
+ 'foreign_entity', $5, 'github_pr_status_changed')
"#,
user_id,
notification_id,
done,
seen_at,
+ foreign_entity_id.to_string(),
)
.execute(pool)
.await