mirror of
https://github.com/block/buzz.git
synced 2026-12-01 13:42:45 +00:00
Merge origin/main into projects-channel-first
Keep channel-first project home while taking main's timeline stacking isolation and Projects chrome updates. Signed-off-by: Thomas Petersen <thomasp@squareup.com>
This commit is contained in:
@@ -14,36 +14,45 @@ use buzz_sdk::mentions::{
|
||||
|
||||
/// Extract the thread root event ID from a Nostr tag array.
|
||||
///
|
||||
/// Parses `"e"` tags with NIP-10 markers:
|
||||
/// - If a `"root"` marker exists, returns that event ID.
|
||||
/// - Otherwise, if only a `"reply"` marker exists, returns the reply target
|
||||
/// (a direct reply's parent IS the root, and nested replies need that root
|
||||
/// to thread correctly).
|
||||
/// - If no thread markers exist, returns `None` (parent is a top-level message,
|
||||
/// so it is itself the root).
|
||||
/// Delegates marker parsing and collapse to [`buzz_core::nip10`] (shared with
|
||||
/// relay ingest and ACP) so id-validity, marker selection, and top-level
|
||||
/// classification cannot drift:
|
||||
/// - A `root`+`reply` parent returns its root event ID.
|
||||
/// - A `reply`-only parent returns the reply target (a direct reply's parent IS
|
||||
/// the root).
|
||||
/// - A root-only or marker-less parent returns `None` (it is top-level and its
|
||||
/// own root).
|
||||
fn find_root_from_tags(tags: &serde_json::Value) -> Option<String> {
|
||||
fn valid_event_id(s: &str) -> bool {
|
||||
s.len() == 64 && s.chars().all(|c| c.is_ascii_hexdigit())
|
||||
}
|
||||
let arr = tags.as_array()?;
|
||||
let mut root = None;
|
||||
let mut reply = None;
|
||||
for tag in arr {
|
||||
let Some(parts) = tag.as_array() else {
|
||||
continue;
|
||||
};
|
||||
if parts.len() >= 4 && parts[0].as_str() == Some("e") {
|
||||
// Defensively ignore malformed marker values so a bad tag on the
|
||||
// parent event can't block the reply — fall back to root == parent.
|
||||
let id = parts[1].as_str().filter(|s| valid_event_id(s));
|
||||
match (parts[3].as_str(), id) {
|
||||
(Some("root"), Some(id)) => root = Some(id.to_string()),
|
||||
(Some("reply"), Some(id)) => reply = Some(id.to_string()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
root.or(reply)
|
||||
let parts: Vec<Vec<String>> = tags
|
||||
.as_array()?
|
||||
.iter()
|
||||
.filter_map(|tag| {
|
||||
tag.as_array().map(|a| {
|
||||
a.iter()
|
||||
.map(|v| v.as_str().unwrap_or("").to_string())
|
||||
.collect()
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
buzz_core::nip10::parse_thread_markers_from_parts(parts.iter().map(Vec::as_slice))
|
||||
.resolve()
|
||||
.map(|(root, _)| root)
|
||||
}
|
||||
|
||||
fn thread_ref_from_parent_tags(
|
||||
parent_eid: nostr::EventId,
|
||||
parent_event_id: &str,
|
||||
tags: &serde_json::Value,
|
||||
) -> Result<ThreadRef, CliError> {
|
||||
let root_eid = match find_root_from_tags(tags) {
|
||||
Some(root_hex) if root_hex != parent_event_id => parse_event_id(&root_hex)?,
|
||||
_ => parent_eid,
|
||||
};
|
||||
|
||||
Ok(ThreadRef {
|
||||
root_event_id: root_eid,
|
||||
parent_event_id: parent_eid,
|
||||
})
|
||||
}
|
||||
|
||||
/// Build a `ThreadRef` for a reply, given the immediate parent's event ID.
|
||||
@@ -80,14 +89,7 @@ fn thread_ref_from_event(event_id: &str, event: &serde_json::Value) -> Result<Th
|
||||
.get("tags")
|
||||
.cloned()
|
||||
.unwrap_or(serde_json::Value::Null);
|
||||
let root_event_id = match find_root_from_tags(&tags) {
|
||||
Some(root_hex) if root_hex != event_id => parse_event_id(&root_hex)?,
|
||||
_ => parent_eid,
|
||||
};
|
||||
Ok(ThreadRef {
|
||||
root_event_id,
|
||||
parent_event_id: parent_eid,
|
||||
})
|
||||
thread_ref_from_parent_tags(parent_eid, event_id, &tags)
|
||||
}
|
||||
|
||||
/// Resolve the channel UUID for an event by querying for it via POST /query.
|
||||
@@ -1057,7 +1059,8 @@ mod tests {
|
||||
channel_id_from_event, cmd_get_thread, event_mention_pubkeys, find_root_from_tags,
|
||||
match_profiles_by_name, merge_message_mentions, missing_members,
|
||||
normalize_explicit_mentions, parse_member_pubkeys, resolve_names_to_pubkeys,
|
||||
resolve_thread_target, thread_ref_from_event, BuzzClient, CliError, Uuid,
|
||||
resolve_thread_target, thread_ref_from_event, thread_ref_from_parent_tags, BuzzClient,
|
||||
CliError, Uuid,
|
||||
};
|
||||
use buzz_sdk::mentions::{
|
||||
extract_at_mentions_with_known, extract_at_names, match_names_to_profiles, MentionProfile,
|
||||
@@ -1132,7 +1135,7 @@ mod tests {
|
||||
let channel = "123e4567-e89b-12d3-a456-426614174000";
|
||||
let other_channel = "123e4567-e89b-12d3-a456-426614174001";
|
||||
let selected = json!({
|
||||
"tags": [["h", channel], ["e", ID_A, "", "root"]]
|
||||
"tags": [["h", channel], ["e", ID_A, "", "root"], ["e", ID_B, "", "reply"]]
|
||||
});
|
||||
|
||||
assert!(resolve_thread_target(
|
||||
@@ -1171,6 +1174,23 @@ mod tests {
|
||||
assert_eq!(find_root_from_tags(&tags).as_deref(), Some(ID_A));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_marker_without_reply_is_top_level() {
|
||||
let tags = json!([["e", ID_A, "", "root"], ["p", PUBKEY],]);
|
||||
assert!(find_root_from_tags(&tags).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_only_parent_starts_cli_reply_thread_at_parent() {
|
||||
let tags = json!([["e", ID_A, "", "root"]]);
|
||||
let parent = nostr::EventId::from_hex(ID_B).expect("valid parent id");
|
||||
|
||||
let thread_ref = thread_ref_from_parent_tags(parent, ID_B, &tags).expect("thread ref");
|
||||
|
||||
assert_eq!(thread_ref.parent_event_id, parent);
|
||||
assert_eq!(thread_ref.root_event_id, parent);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reply_only_falls_back_to_reply_target() {
|
||||
// Direct reply to a top-level message — the parent's only e-tag is a
|
||||
@@ -1194,14 +1214,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_tags_are_skipped() {
|
||||
fn malformed_tags_are_skipped_and_root_only_is_top_level() {
|
||||
// Invalid entries are ignored, leaving a valid root-only marker; the
|
||||
// shared collapse rule still classifies that parent as top-level.
|
||||
let tags = json!([
|
||||
"not-an-array",
|
||||
["e"],
|
||||
["e", "short"],
|
||||
["e", ID_A, "", "root"],
|
||||
]);
|
||||
assert_eq!(find_root_from_tags(&tags).as_deref(), Some(ID_A));
|
||||
assert!(find_root_from_tags(&tags).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user