Skip to content

Commit 9894641

Browse files
author
Eva
committed
fix(buzz-v1): let a thread on a never-unread root be read
A thread whose root is a kind that is never unread itself, such as a diff (40008), was listed in the sidebar but could not be opened or cleared. Reading its context returned `unavailable`, and `mark_through` with the sidebar's own `latest_reply_id` returned `blocked`, so the badge stayed until the whole channel was marked read. `valid_target` looked the root up through `message()`, which only finds kinds that can be unread. A root only has to exist in the channel and not be a reply itself, so that lookup no longer filters by kind. The `mark_through` anchor keeps the filter: a diff still never counts as unread and is never a valid anchor. The new Postgres test covers the sidebar, the context read and the write for such a thread, and fails without the writes.rs change. Reported in review on #7906 (writes.rs:166). Co-authored-by: Eva <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Signed-off-by: Eva <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz>
1 parent a09bbdf commit 9894641

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,79 @@ impl ThreadReadSummary {
471471
}
472472
}
473473

474+
#[tokio::test]
475+
#[ignore = "requires Postgres"]
476+
async fn thread_on_a_never_unread_root_is_selectable_and_readable_by_itself() {
477+
let (db, pool, community, channel, actor, root) = fixture().await;
478+
let base = root.created_at.as_secs();
479+
let mention = || vec![Tag::parse(["p", &actor.public_key().to_hex()]).unwrap()];
480+
// A diff is never unread, not even one addressed to the actor.
481+
let diff = EventBuilder::new(Kind::Custom(40008), "a diff")
482+
.tags(mention())
483+
.custom_created_at(nostr::Timestamp::from(base + 1))
484+
.sign_with_keys(&Keys::generate())
485+
.unwrap();
486+
db.insert_event(community, &diff, Some(channel))
487+
.await
488+
.unwrap();
489+
let on_diff = reply(&db, &pool, community, channel, &diff, base + 10, mention()).await;
490+
let elsewhere = reply(&db, &pool, community, channel, &root, base + 20, mention()).await;
491+
492+
let row = sidebar(&db, community, &actor).await;
493+
assert_eq!(exact(&row.unread), Some(3), "the root and two replies");
494+
assert_eq!(row.threads.items.len(), 2);
495+
let listed = &row.threads.items[1];
496+
assert_eq!(listed.root_id, diff.id.to_hex());
497+
assert_eq!(listed.latest_reply_id, on_diff.id.to_hex());
498+
499+
// The listed thread is a usable context; the diff stays uncounted.
500+
let thread = ReadTarget {
501+
channel_id: channel,
502+
root_id: Some(listed.root_id.clone()),
503+
};
504+
let timeline = ReadTarget {
505+
channel_id: channel,
506+
root_id: None,
507+
};
508+
let queries = [(&thread, &on_diff), (&timeline, &diff)].map(|(target, message)| ContextQuery {
509+
target: target.clone(),
510+
message_ids: vec![message.id.to_hex()],
511+
});
512+
let page = db
513+
.personal_read_contexts(
514+
community,
515+
&actor.public_key(),
516+
DEFAULT_RETENTION_SECONDS,
517+
&queries,
518+
)
519+
.await
520+
.unwrap();
521+
let wire = serde_json::to_value(&page).unwrap();
522+
assert_eq!(
523+
wire["contexts"][0]["messages"][0],
524+
serde_json::json!({"message_id":on_diff.id.to_hex(),"status":"unread","reason":"mention"})
525+
);
526+
assert_eq!(wire["contexts"][1]["messages"][0]["status"], "not_counted");
527+
528+
// The diff is still no anchor; the listed reply reads its thread alone.
529+
let through_diff = ReadIntent::MarkThrough {
530+
target: thread,
531+
message_id: diff.id.to_hex(),
532+
};
533+
assert_eq!(
534+
apply(&db, community, &actor, through_diff).await,
535+
IntentOutcome::Blocked
536+
);
537+
assert_eq!(
538+
apply(&db, community, &actor, listed.clone_target(channel)).await,
539+
IntentOutcome::Applied
540+
);
541+
let row = sidebar(&db, community, &actor).await;
542+
assert_eq!(exact(&row.unread), Some(2));
543+
assert_eq!(row.threads.items.len(), 1);
544+
assert_eq!(row.threads.items[0].latest_reply_id, elsewhere.id.to_hex());
545+
}
546+
474547
#[tokio::test]
475548
#[ignore = "requires Postgres"]
476549
async fn thread_summaries_are_incomplete_when_tag_evidence_could_hide_a_root() {

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,23 @@ struct Message {
8686
root: Option<Vec<u8>>,
8787
}
8888

89+
/// A channel event and its canonical ancestry. `kinds` bounds the lookup: an
90+
/// anchor must be a kind that can be unread; a thread root need not be.
8991
async fn message(
9092
conn: &mut PgConnection,
9193
community: CommunityId,
9294
channel: Uuid,
9395
id: &[u8],
96+
kinds: Option<&[i32]>,
9497
) -> Result<Option<Message>> {
9598
let row = sqlx::query(
9699
"SELECT e.id, e.created_at, e.tags, tm.root_event_id
97100
FROM events e LEFT JOIN thread_metadata tm ON tm.community_id=e.community_id
98101
AND tm.event_created_at=e.created_at AND tm.event_id=e.id AND tm.channel_id=e.channel_id
99102
WHERE e.community_id=$1 AND e.channel_id=$2 AND e.id=$3
100-
AND e.kind=ANY($4)
103+
AND ($4::int4[] IS NULL OR e.kind=ANY($4))
101104
LIMIT 1",
102-
).bind(community.as_uuid()).bind(channel).bind(id).bind(ELIGIBLE_KINDS.as_slice()).fetch_optional(&mut *conn).await?;
105+
).bind(community.as_uuid()).bind(channel).bind(id).bind(kinds).fetch_optional(&mut *conn).await?;
103106
row.map(|row| {
104107
let timestamp: DateTime<Utc> = row.try_get("created_at")?;
105108
let id: Vec<u8> = row.try_get("id")?;
@@ -160,9 +163,10 @@ pub(super) async fn valid_target(
160163
return Ok(None);
161164
}
162165
if !root.is_empty() {
163-
// Deleted roots still own living replies. Validate actual ancestry,
164-
// not absence of metadata: a missing index row is not a top-level proof.
165-
let Some(msg) = message(conn, community, target.channel_id, &root).await? else {
166+
// Deleted roots still own living replies, and so do roots of a kind
167+
// that is never unread itself (a diff). Validate actual ancestry, not
168+
// absence of metadata: a missing index row is not a top-level proof.
169+
let Some(msg) = message(conn, community, target.channel_id, &root, None).await? else {
166170
return Ok(None);
167171
};
168172
if msg.root.as_ref().is_some_and(|r| r != &msg.id) {
@@ -204,7 +208,15 @@ pub(super) async fn apply(
204208
let Some(root) = valid_target(conn, community, actor, target).await? else {
205209
return Ok(IntentOutcome::Blocked);
206210
};
207-
let Some(msg) = message(conn, community, target.channel_id, &id).await? else {
211+
let Some(msg) = message(
212+
conn,
213+
community,
214+
target.channel_id,
215+
&id,
216+
Some(&ELIGIBLE_KINDS),
217+
)
218+
.await?
219+
else {
208220
return Ok(IntentOutcome::Blocked);
209221
};
210222
let is_reply = msg.root.as_ref().is_some_and(|r| r != &msg.id);

0 commit comments

Comments
 (0)