Skip to content

Commit 7afdc2a

Browse files
baxenhMB2hMurderbotARTclaude
authored
feat(relay): record when a read-state account starts (#8157)
## Goal Add `started_at` to read-state accounts from the day the table is created. This prevents users seeing ALL their channels as unread when they first install a client update that starts using these APIs --------- Signed-off-by: hMB2 <0400eb0da32afbbf16cdd1dee6ffd243587e8f3d6a2823fe7dfa95d16b002b7d@buzz.block.builderlab.xyz> Signed-off-by: ART <ea39c1c8acf57180161ef4ba4ed68abf8589f5291956b909a5aa991fc66664ca@buzz.block.builderlab.xyz> Signed-off-by: hMurderbot <219c58afff8ac2c76e4e527a329aa61c2ebbb896786b1916d4f9cca5140ecd04@buzz.block.builderlab.xyz> Co-authored-by: hMB2 <0400eb0da32afbbf16cdd1dee6ffd243587e8f3d6a2823fe7dfa95d16b002b7d@buzz.block.builderlab.xyz> Co-authored-by: hMurderbot <219c58afff8ac2c76e4e527a329aa61c2ebbb896786b1916d4f9cca5140ecd04@buzz.block.builderlab.xyz> Co-authored-by: ART <ea39c1c8acf57180161ef4ba4ed68abf8589f5291956b909a5aa991fc66664ca@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d34c9c2 commit 7afdc2a

12 files changed

Lines changed: 349 additions & 38 deletions

File tree

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,17 @@ 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(), 56);
708+
assert_eq!(migrations.len(), 57);
709709
assert_eq!(migrations[55].version, 56);
710710
assert!(migrations[55]
711711
.sql
712712
.as_str()
713713
.contains("CREATE TABLE personal_read_accounts"));
714+
assert_eq!(migrations[56].version, 57);
715+
assert!(migrations[56]
716+
.sql
717+
.as_str()
718+
.contains("ALTER TABLE personal_read_accounts ADD COLUMN started_at"));
714719
assert_eq!(migrations[48].version, 49);
715720
assert_eq!(migrations[49].version, 50);
716721
assert_eq!(migrations[50].version, 51);
@@ -2210,9 +2215,30 @@ mod postgres_tests {
22102215
.sql
22112216
.as_ref(),
22122217
);
2218+
// 0057 adds personal_read_accounts.started_at after 0056's CREATE TABLE.
2219+
let started_at_column = "started_at timestamptz, ";
2220+
assert!(MIGRATOR
2221+
.iter()
2222+
.find(|m| m.version == 57)
2223+
.expect("personal read start migration")
2224+
.sql
2225+
.as_str()
2226+
.contains("ADD COLUMN started_at TIMESTAMPTZ"));
2227+
assert!(schema
2228+
.tables
2229+
.get("personal_read_accounts")
2230+
.expect("schema.sql personal read accounts")
2231+
.contains(started_at_column));
22132232
for (table, definition) in personal.tables {
2233+
let in_schema = schema.tables.get(&table).map(|schema_definition| {
2234+
if table == "personal_read_accounts" {
2235+
schema_definition.replacen(started_at_column, "", 1)
2236+
} else {
2237+
schema_definition.clone()
2238+
}
2239+
});
22142240
assert_eq!(
2215-
schema.tables.get(&table),
2241+
in_schema.as_ref(),
22162242
Some(&definition),
22172243
"personal read table {table} differs"
22182244
);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,16 @@ async fn migration_schema_thread_window_prebuild_does_not_queue_behind_writer()
311311
production_result.is_ok(),
312312
"production migrator must preserve ingestion progress: {production_result:?}"
313313
);
314-
assert_eq!(version, 56);
314+
let latest = sqlx::migrate::Migrator::new(
315+
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../migrations"),
316+
)
317+
.await
318+
.unwrap()
319+
.iter()
320+
.map(|m| m.version)
321+
.max()
322+
.unwrap();
323+
assert_eq!(version, latest);
315324
assert_eq!(final_oid, oid, "prebuild must not be replaced");
316325
assert_eq!(count, 4, "all writer witnesses must persist");
317326
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ impl Db {
3939
.await?;
4040
writes::deadlines(&mut tx).await?;
4141
let actor_bytes = actor.to_bytes();
42-
let account = read_account(&mut tx, retention_seconds).await?;
42+
let account = read_account(&mut tx, community, &actor_bytes, retention_seconds).await?;
43+
// Every frontier is floored at the account's start.
44+
let floor = account.floor();
4345
let mut contexts = Vec::with_capacity(queries.len());
4446
// Replies whose state turns on conversation membership, by position.
4547
let mut pending = Vec::new();
@@ -128,7 +130,7 @@ impl Db {
128130
account.cutoff_ms,
129131
) {
130132
MessageReadState::NotCounted
131-
} else if prefix.is_some_and(|p| received <= p) {
133+
} else if received <= prefix.map_or(floor, |p| p.max(floor)) {
132134
MessageReadState::Read
133135
} else {
134136
let reason = classification::reason(

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use chrono::{DateTime, Utc};
12
use serde::{Deserialize, Serialize};
23
use uuid::Uuid;
34

@@ -56,6 +57,18 @@ pub struct ReadAccount {
5657
pub retention_seconds: u32,
5758
/// Read-time author-time cutoff (Unix milliseconds), not a discard boundary.
5859
pub cutoff_ms: i64,
60+
/// Arrival floor of every frontier: the account's first applied read
61+
/// intent. `None` until then, and nothing counts as unread. Not on the wire.
62+
#[serde(skip)]
63+
pub started_at: Option<DateTime<Utc>>,
64+
}
65+
66+
impl ReadAccount {
67+
/// The arrival every frontier is floored at: the start, or the end of time
68+
/// for an account that has not started.
69+
pub(super) fn floor(&self) -> DateTime<Utc> {
70+
self.started_at.unwrap_or(DateTime::<Utc>::MAX_UTC)
71+
}
5972
}
6073

6174
/// Maximum channel summaries in one sidebar page.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ impl World {
3434
.await
3535
.unwrap()
3636
.id;
37+
let actor = Keys::generate();
38+
super::postgres_tests::start_before_everything(&pool, community, &actor.public_key()).await;
3739
Self {
3840
db,
3941
pool,
4042
community,
41-
actor: Keys::generate(),
43+
actor,
4244
peer: Keys::generate(),
4345
now: Timestamp::now().as_secs(),
4446
}

0 commit comments

Comments
 (0)