Skip to content

Commit 4da2b9f

Browse files
baxenclaude
andcommitted
feat(read-state): follow intents, threads view, arrival-ordered access paths
Extend the merged /buzz/v1 read-state wire with the minimum the follow model needs: - `follow` / `unfollow` intents on a thread target. An unfollow sticks until the actor posts in the thread or is mentioned there again. - `GET /me/threads`: followed threads in joined channels, newest reply arrival first, cursor-paged, at most 50 per page. - Thread rows count `mentions` as direct mentions only. - Broadcast replies count on the channel timeline and are valid channel anchors. - NIP-11 advertises `capabilities: ["follow","threads"]` and `max_threads`. Migration 0061 adds `events.read_timeline`, `thread_metadata.received_at` and `personal_read_frontiers.unfollowed`, plus arrival-ordered partial indexes so the sidebar and threads view probe an index per channel and per follow instead of scanning a bounded window. No backfill. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Bradley Axen <[email protected]>
1 parent 419a530 commit 4da2b9f

23 files changed

Lines changed: 1193 additions & 344 deletions

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,19 @@ mod postgres_tests {
705705
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
706706
migrations.sort_by_key(|migration| migration.version);
707707

708-
assert_eq!(migrations.len(), 60);
708+
assert_eq!(migrations.len(), 61);
709709
assert_eq!(migrations[58].version, 59);
710710
assert!(migrations[58].sql.as_str().contains("buzz.push_enabled"));
711711
assert_eq!(migrations[59].version, 60);
712712
assert!(migrations[59]
713713
.sql
714714
.as_str()
715715
.contains("ALTER TABLE push_leases DROP COLUMN app_profile"));
716+
assert_eq!(migrations[60].version, 61);
717+
assert!(migrations[60]
718+
.sql
719+
.as_str()
720+
.contains("idx_events_read_timeline"));
716721
assert_eq!(migrations[55].version, 56);
717722
assert!(migrations[55]
718723
.sql
@@ -2253,6 +2258,16 @@ mod postgres_tests {
22532258
.sql
22542259
.as_str();
22552260
assert!(through_message.contains("DROP COLUMN threads_through_timestamp"));
2261+
// 0061 adds the sticky thread unfollow.
2262+
let unfollowed_column = "unfollowed boolean not null default false \
2263+
check (not unfollowed or root_id <> ''::bytea), ";
2264+
assert!(MIGRATOR
2265+
.iter()
2266+
.find(|m| m.version == 61)
2267+
.expect("read-state access path migration")
2268+
.sql
2269+
.as_str()
2270+
.contains("$d$BOOLEAN NOT NULL DEFAULT false CHECK (NOT unfollowed OR root_id <> ''::bytea)$d$"));
22562271
for (table, definition) in personal.tables {
22572272
let in_schema = schema.tables.get(&table).map(|schema_definition| {
22582273
if table == "personal_read_accounts" {
@@ -2262,7 +2277,11 @@ mod postgres_tests {
22622277
}
22632278
});
22642279
let definition = if table == "personal_read_frontiers" {
2265-
definition.replacen(threads_through_column, through_message_column, 1)
2280+
definition.replacen(
2281+
threads_through_column,
2282+
&format!("{through_message_column}{unfollowed_column}"),
2283+
1,
2284+
)
22662285
} else {
22672286
definition
22682287
};

‎crates/buzz-db/src/runtime/tests/thread_window_postgres_tests.rs‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,18 @@ async fn migration_schema_thread_window_prebuild_does_not_queue_behind_writer()
270270
.await
271271
.unwrap();
272272

273-
// Also bind the public migrator: the advisory schema/destruction lock,
274-
// ledger write and post-migration catalog checks must work with ingestion.
275-
let production_result = migration::run_migrations(&pool).await;
273+
// Also bind the migrator: the advisory schema/destruction lock, ledger
274+
// write and post-migration catalog checks must work with ingestion. 0061
275+
// adds a column to thread_metadata, which takes a brief exclusive lock
276+
// bounded by its lock_timeout, so it runs after the writer below.
277+
let production_result = migration::run_migrations_through(&pool, 60).await;
276278
sqlx::query(insert)
277279
.bind(community)
278280
.bind(channel)
279281
.bind(vec![4_u8; 32])
280282
.execute(&mut *witness)
281283
.await
282284
.unwrap();
283-
let version: i64 = sqlx::query_scalar("SELECT max(version) FROM _sqlx_migrations")
284-
.fetch_one(&mut *witness)
285-
.await
286-
.unwrap();
287285
let final_oid: i64 =
288286
sqlx::query_scalar("SELECT 'idx_thread_metadata_window'::regclass::oid::bigint")
289287
.fetch_one(&mut *witness)
@@ -297,6 +295,11 @@ async fn migration_schema_thread_window_prebuild_does_not_queue_behind_writer()
297295
.unwrap();
298296
drop(witness);
299297
writer.rollback().await.unwrap();
298+
migration::run_migrations(&pool).await.unwrap();
299+
let version: i64 = sqlx::query_scalar("SELECT max(version) FROM _sqlx_migrations")
300+
.fetch_one(&pool)
301+
.await
302+
.unwrap();
300303
drop_scratch_db(&admin, pool, &name).await;
301304

302305
assert!(

‎crates/buzz-db/src/store/event.rs‎

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,9 @@ pub async fn insert_event_in_transaction(
447447
) -> Result<(StoredEvent, bool)> {
448448
let result = insert_event_on(tx, event, channel_id).await?;
449449
if result.1 {
450-
if let Some(channel) = channel_id {
451-
crate::personal_read::record_message(
452-
tx,
453-
event,
454-
channel,
455-
crate::personal_read::Place::TopLevel,
456-
result.0.received_at,
457-
)
458-
.await?;
450+
if let Some((channel, place)) = crate::personal_read::place(event, channel_id, None) {
451+
crate::personal_read::record_message(tx, event, channel, place, result.0.received_at)
452+
.await?;
459453
}
460454
crate::operator_listener::enqueue_mentions_in_transaction(tx, event).await?;
461455
}
@@ -492,8 +486,8 @@ async fn insert_event_on(
492486
let not_before = extract_not_before(event);
493487
let result = sqlx::query(
494488
r#"
495-
INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before)
496-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
489+
INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before, read_timeline)
490+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
497491
ON CONFLICT DO NOTHING
498492
"#,
499493
)
@@ -509,6 +503,7 @@ async fn insert_event_on(
509503
.bind(channel_id)
510504
.bind(d_tag.as_deref())
511505
.bind(not_before)
506+
.bind(crate::personal_read::place(event, channel_id, None).is_some_and(|(_, p)| p.on_timeline()))
512507
.execute(tx.conn())
513508
.await?;
514509

@@ -1740,11 +1735,22 @@ pub(crate) async fn insert_event_with_thread_metadata_tx(
17401735
let received_at = Utc::now();
17411736
let d_tag = extract_d_tag(event);
17421737
let not_before = extract_not_before(event);
1738+
let place = match &thread_meta {
1739+
Some(meta) => crate::personal_read::place(
1740+
event,
1741+
Some(meta.channel_id),
1742+
meta.parent_event_id.map(|parent| match meta.root_event_id {
1743+
Some(root) => (root, meta.root_event_created_at, meta.broadcast),
1744+
None => (parent, meta.parent_event_created_at, meta.broadcast),
1745+
}),
1746+
),
1747+
None => crate::personal_read::place(event, channel_id, None),
1748+
};
17431749

17441750
let result = sqlx::query(
17451751
r#"
1746-
INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before)
1747-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
1752+
INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before, read_timeline)
1753+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
17481754
ON CONFLICT DO NOTHING
17491755
"#,
17501756
)
@@ -1760,6 +1766,7 @@ pub(crate) async fn insert_event_with_thread_metadata_tx(
17601766
.bind(channel_id)
17611767
.bind(d_tag.as_deref())
17621768
.bind(not_before)
1769+
.bind(place.is_some_and(|(_, p)| p.on_timeline()))
17631770
.execute(tx.conn())
17641771
.await?;
17651772

@@ -1774,6 +1781,12 @@ pub(crate) async fn insert_event_with_thread_metadata_tx(
17741781
)
17751782
.await?;
17761783

1784+
// Before the thread counters below: their root row is shared by every
1785+
// reply, so its lock must not be held across these writes.
1786+
if let Some((channel, place)) = place {
1787+
crate::personal_read::record_message(tx, event, channel, place, received_at).await?;
1788+
}
1789+
17771790
if let Some(ref meta) = thread_meta {
17781791
let broadcast_val: bool = meta.broadcast;
17791792

@@ -1783,8 +1796,8 @@ pub(crate) async fn insert_event_with_thread_metadata_tx(
17831796
(community_id, event_created_at, event_id, channel_id,
17841797
parent_event_id, parent_event_created_at,
17851798
root_event_id, root_event_created_at,
1786-
depth, broadcast)
1787-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
1799+
depth, broadcast, received_at)
1800+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
17881801
ON CONFLICT DO NOTHING
17891802
"#,
17901803
)
@@ -1798,6 +1811,8 @@ pub(crate) async fn insert_event_with_thread_metadata_tx(
17981811
.bind(meta.root_event_created_at)
17991812
.bind(meta.depth)
18001813
.bind(broadcast_val)
1814+
// An eligible reply's arrival, for the read-state thread index.
1815+
.bind(place.and(meta.parent_event_id).map(|_| received_at))
18011816
.execute(tx.conn())
18021817
.await?;
18031818

@@ -1882,22 +1897,6 @@ pub(crate) async fn insert_event_with_thread_metadata_tx(
18821897
}
18831898
}
18841899

1885-
let place = match &thread_meta {
1886-
Some(meta) => Some((
1887-
meta.channel_id,
1888-
match meta.parent_event_id {
1889-
Some(parent) => crate::personal_read::Place::Reply {
1890-
root: meta.root_event_id.unwrap_or(parent),
1891-
},
1892-
// Workflow messages store a depth-0 metadata row.
1893-
None => crate::personal_read::Place::TopLevel,
1894-
},
1895-
)),
1896-
None => channel_id.map(|channel| (channel, crate::personal_read::Place::TopLevel)),
1897-
};
1898-
if let Some((channel, place)) = place {
1899-
crate::personal_read::record_message(tx, event, channel, place, received_at).await?;
1900-
}
19011900
crate::operator_listener::enqueue_mentions_in_transaction(tx, event).await?;
19021901
}
19031902

‎crates/buzz-db/src/store/personal_read/arrival_postgres_tests.rs‎

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Read progress follows relay arrival (`received_at`), never author time.
22
//! Each case sets every arrival explicitly: back-to-back inserts share a clock.
33
use super::{
4-
postgres_tests::{fixture, insert_reply},
4+
postgres_tests::{fixture, insert_reply, sync_reply_arrivals},
55
*,
66
};
77
use crate::Db;
@@ -23,6 +23,7 @@ async fn arrive(pool: &PgPool, community: CommunityId, event: &nostr::Event, arr
2323
.unwrap()
2424
.rows_affected();
2525
assert_eq!(updated, 1, "arrival must land on exactly one stored event");
26+
sync_reply_arrivals(pool).await;
2627
}
2728

2829
/// A message authored at `authored` that arrives at `arrived`. It mentions the
@@ -254,10 +255,9 @@ async fn mark_thread_read_with_the_sidebar_anchor_clears_a_late_reply() {
254255
assert!(row.threads.is_empty());
255256
}
256257

257-
/// The latest probe reads the newest `MAX_TIMELINE_SCAN` events by author
258-
/// time, of any kind. A late arrival with an older author time than the
259-
/// uncounted events after it must still be the sidebar anchor, or Mark as read
260-
/// with that anchor leaves it unread.
258+
/// A late arrival with an older author time than the uncounted events after
259+
/// it must still be the sidebar anchor, or Mark as read with that anchor
260+
/// leaves it unread.
261261
async fn reactions(
262262
pool: &PgPool,
263263
db: &Db,
@@ -310,37 +310,6 @@ async fn mark_as_read_clears_a_late_arrival_behind_reactions() {
310310
assert_eq!(counts(&db, community, &actor).await, (false, 0));
311311
}
312312

313-
/// Past the budget the probe finds no anchor, and nothing counts: a badge
314-
/// with no latest to mark could never be cleared.
315-
#[tokio::test]
316-
#[ignore = "requires Postgres"]
317-
async fn a_message_behind_a_full_budget_of_reactions_is_not_a_badge_without_an_anchor() {
318-
let (db, pool, community, channel, actor, first) = fixture().await;
319-
let now = first.created_at.as_secs();
320-
let demoted = sqlx::query("UPDATE events SET kind=7 WHERE community_id=$1 AND id=$2")
321-
.bind(community.as_uuid())
322-
.bind(first.id.as_bytes().as_slice())
323-
.execute(&pool)
324-
.await
325-
.unwrap()
326-
.rows_affected();
327-
assert_eq!(demoted, 1);
328-
post(&db, &pool, community, channel, &actor, now - 600, now - 10).await;
329-
reactions(
330-
&pool,
331-
&db,
332-
community,
333-
channel,
334-
MAX_TIMELINE_SCAN as u64,
335-
now,
336-
)
337-
.await;
338-
339-
let row = sidebar(&db, community, &actor).await;
340-
assert_eq!(row.latest_id, None);
341-
assert_eq!((row.unread, row.mentions), (false, 0));
342-
}
343-
344313
/// Store `event` as having arrived at exactly `seconds` plus `micros`. Built
345314
/// from integers, never a float, so microsecond order cannot hinge on rounding.
346315
async fn arrive_exact(
@@ -360,6 +329,7 @@ async fn arrive_exact(
360329
.unwrap()
361330
.rows_affected();
362331
assert_eq!(updated, 1, "arrival must land on exactly one stored event");
332+
sync_reply_arrivals(pool).await;
363333
let stored: chrono::DateTime<chrono::Utc> =
364334
sqlx::query_scalar("SELECT received_at FROM events WHERE community_id=$1 AND id=$2")
365335
.bind(community.as_uuid())

0 commit comments

Comments
 (0)