From 5e0c84aec754c042da0db02b937f1774f14de107 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 8 Jul 2023 18:02:02 +0200 Subject: [PATCH] Support for comment nesting with a tree structure --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/api/post.rs | 20 +++++++++++++++++--- src/components/comment_row.rs | 6 +++--- src/components/post_page.rs | 2 -- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b61f00..ac16e78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -577,6 +577,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "either" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + [[package]] name = "encoding_rs" version = "0.8.32" @@ -1322,6 +1328,15 @@ dependencies = [ "phf 0.11.2", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.6" @@ -1444,6 +1459,7 @@ version = "0.2.0" dependencies = [ "chrono", "html2pango", + "itertools", "lemmy_api_common", "markdown", "mime_guess", diff --git a/Cargo.toml b/Cargo.toml index e139a7c..8ad5e10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ rand = "0.8.5" mime_guess = "2.0.4" chrono = "0.4.26" timeago = "0.4.1" +itertools = "0.11.0" diff --git a/src/api/post.rs b/src/api/post.rs index cff8a4b..96fc2b3 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -1,3 +1,5 @@ +use crate::settings; +use itertools::Itertools; use lemmy_api_common::{ comment::{GetComments, GetCommentsResponse}, lemmy_db_schema::{ @@ -10,8 +12,6 @@ use lemmy_api_common::{ }, }; -use crate::settings; - pub fn get_post(id: PostId) -> Result { let params = GetPost { id: Some(id), @@ -27,6 +27,7 @@ pub fn get_comments(post_id: PostId) -> Result, reqwest::Error> post_id: Some(post_id), sort: Some(CommentSortType::Hot), type_: Some(ListingType::All), + max_depth: Some(8), auth: settings::get_current_account().jwt, ..Default::default() }; @@ -36,7 +37,20 @@ pub fn get_comments(post_id: PostId) -> Result, reqwest::Error> // hide removed and deleted comments comments.retain(|c| !c.comment.deleted && !c.comment.removed); - Ok(comments) + // group comments by their parent and generate the tree structure known from the web interface + let mut grouped_comments: Vec = vec![]; + for (_, comments_group) in &comments + .iter() + .group_by(|a| a.comment.path.split(".").collect::>()[1].to_owned()) + { + let mut group = comments_group.collect::>(); + group.sort_by(|a, b| a.comment.path.partial_cmp(&b.comment.path).unwrap()); + for c in group { + grouped_comments.push(c.clone()); + } + } + + Ok(grouped_comments) } pub fn default_post() -> GetPostResponse { diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index 351f185..4d19394 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -48,9 +48,11 @@ impl FactoryComponent for CommentRow { set_orientation: gtk::Orientation::Vertical, set_spacing: 10, set_margin_end: 10, - set_margin_start: 10, + set_margin_start: ((self.comment.comment.path.matches('.').count() - 1) * 20 + 10) as i32, set_margin_top: 10, + gtk::Separator {}, + gtk::Box { set_orientation: gtk::Orientation::Horizontal, set_spacing: 10, @@ -107,8 +109,6 @@ impl FactoryComponent for CommentRow { set_visible: self.comment.creator.id.0 == settings::get_current_account().id, } }, - - gtk::Separator {} } } diff --git a/src/components/post_page.rs b/src/components/post_page.rs index 9c44f5f..f2bceb2 100644 --- a/src/components/post_page.rs +++ b/src/components/post_page.rs @@ -176,8 +176,6 @@ impl SimpleComponent for PostPage { } }, - gtk::Separator {}, - #[local_ref] comments -> gtk::Box { set_orientation: gtk::Orientation::Vertical,