Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(comments): keep a reply to a reply in the comment listing (#806) (#839)

muraa-p9c4414c50e78
diff --git a/crates/automation/src/comments.rs b/crates/automation/src/comments.rs
index b7f83c2f1..c6e722d2c 100644
--- a/crates/automation/src/comments.rs
+++ b/crates/automation/src/comments.rs
@@ -191,8 +191,21 @@ impl Automation {
.filter(|c| c.in_reply_to.is_none() && only.is_none_or(|p| c.page == p))
.map(|c| {
let mut v = view(c);
- let thread: Vec<&Annotation> = match &c.name {
- Some(nm) => all.iter().filter(|r| r.in_reply_to.as_deref() == Some(nm.as_str())).collect(),
+ // Every reply under this comment, at any depth, in document order. A reply to a
+ // reply keeps its `/IRT` on the direct parent, so following one level would drop it.
+ let descends_from = |r: &Annotation, root: &str| -> bool {
+ let mut parent = r.in_reply_to.as_deref();
+ for _ in 0..64 {
+ let Some(nm) = parent else { return false };
+ if nm == root {
+ return true;
+ }
+ parent = all.iter().find(|x| x.name.as_deref() == Some(nm)).and_then(|x| x.in_reply_to.as_deref());
+ }
+ false
+ };
+ let thread: Vec<&Annotation> = match c.name.as_deref() {
+ Some(nm) => all.iter().filter(|r| r.in_reply_to.is_some() && descends_from(r, nm)).collect(),
None => Vec::new(),
};
let status = thread.iter().rev().filter(|r| !r.is_mark()).find_map(|r| r.state.clone());
diff --git a/crates/automation/tests/automation.rs b/crates/automation/tests/automation.rs
index f31ffc2a2..6d0e8262f 100644
--- a/crates/automation/tests/automation.rs
+++ b/crates/automation/tests/automation.rs
@@ -1023,6 +1023,29 @@ fn comments_through_tools() {
assert_eq!(ok(&mut b, "comment_list", json!({ "doc": re }))["count"], 5);
}
+/// #806: a reply to another reply stays in the listing, at any depth.
+#[test]
+fn nested_replies_stay_in_the_listing() {
+ let dir = workdir("nested-replies");
+ let mut a = auto(&dir);
+ let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap();
+ let root = ok(&mut a, "comment_add", json!({ "doc": doc, "page": 1, "type": "note", "at": [40, 40], "contents": "ROOT_NOTE", "author": "A" }));
+ let root_id = root["comment"]["id"].as_str().unwrap().to_string();
+ ok(&mut a, "comment_reply", json!({ "doc": doc, "id": root_id, "text": "DIRECT_REPLY", "author": "B" }));
+ let listed = ok(&mut a, "comment_list", json!({ "doc": doc }));
+ let root_comment = listed["comments"].as_array().unwrap().iter().find(|c| c["contents"] == "ROOT_NOTE").unwrap().clone();
+ let direct_id =
+ root_comment["replies"].as_array().unwrap().iter().find(|r| r["contents"] == "DIRECT_REPLY").unwrap()["id"].as_str().unwrap().to_string();
+ ok(&mut a, "comment_reply", json!({ "doc": doc, "id": direct_id, "text": "NESTED_REPLY", "author": "C" }));
+ ok(&mut a, "comment_add", json!({ "doc": doc, "page": 1, "type": "note", "at": [120, 120], "contents": "CONTROL_NOTE", "author": "D" }));
+
+ let list = ok(&mut a, "comment_list", json!({ "doc": doc }));
+ assert_eq!(list["count"], 2, "the two notes stay roots: {list}");
+ let root = list["comments"].as_array().unwrap().iter().find(|c| c["contents"] == "ROOT_NOTE").unwrap().clone();
+ let replies: Vec<&str> = root["replies"].as_array().unwrap().iter().filter_map(|r| r["contents"].as_str()).collect();
+ assert!(replies.contains(&"DIRECT_REPLY") && replies.contains(&"NESTED_REPLY"), "the nested reply is listed: {replies:?}");
+}
+
#[test]
fn protecting_through_tools() {
let dir = workdir("protect");