Support for comment nesting with a tree structure

This commit is contained in:
Bnyro 2023-07-08 18:02:02 +02:00
parent eda3567c1a
commit 5e0c84aec7
5 changed files with 37 additions and 8 deletions

16
Cargo.lock generated
View File

@ -577,6 +577,12 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "either"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.32" version = "0.8.32"
@ -1322,6 +1328,15 @@ dependencies = [
"phf 0.11.2", "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]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.6" version = "1.0.6"
@ -1444,6 +1459,7 @@ version = "0.2.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"html2pango", "html2pango",
"itertools",
"lemmy_api_common", "lemmy_api_common",
"markdown", "markdown",
"mime_guess", "mime_guess",

View File

@ -16,3 +16,4 @@ rand = "0.8.5"
mime_guess = "2.0.4" mime_guess = "2.0.4"
chrono = "0.4.26" chrono = "0.4.26"
timeago = "0.4.1" timeago = "0.4.1"
itertools = "0.11.0"

View File

@ -1,3 +1,5 @@
use crate::settings;
use itertools::Itertools;
use lemmy_api_common::{ use lemmy_api_common::{
comment::{GetComments, GetCommentsResponse}, comment::{GetComments, GetCommentsResponse},
lemmy_db_schema::{ lemmy_db_schema::{
@ -10,8 +12,6 @@ use lemmy_api_common::{
}, },
}; };
use crate::settings;
pub fn get_post(id: PostId) -> Result<GetPostResponse, reqwest::Error> { pub fn get_post(id: PostId) -> Result<GetPostResponse, reqwest::Error> {
let params = GetPost { let params = GetPost {
id: Some(id), id: Some(id),
@ -27,6 +27,7 @@ pub fn get_comments(post_id: PostId) -> Result<Vec<CommentView>, reqwest::Error>
post_id: Some(post_id), post_id: Some(post_id),
sort: Some(CommentSortType::Hot), sort: Some(CommentSortType::Hot),
type_: Some(ListingType::All), type_: Some(ListingType::All),
max_depth: Some(8),
auth: settings::get_current_account().jwt, auth: settings::get_current_account().jwt,
..Default::default() ..Default::default()
}; };
@ -36,7 +37,20 @@ pub fn get_comments(post_id: PostId) -> Result<Vec<CommentView>, reqwest::Error>
// hide removed and deleted comments // hide removed and deleted comments
comments.retain(|c| !c.comment.deleted && !c.comment.removed); 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<CommentView> = vec![];
for (_, comments_group) in &comments
.iter()
.group_by(|a| a.comment.path.split(".").collect::<Vec<&str>>()[1].to_owned())
{
let mut group = comments_group.collect::<Vec<&CommentView>>();
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 { pub fn default_post() -> GetPostResponse {

View File

@ -48,9 +48,11 @@ impl FactoryComponent for CommentRow {
set_orientation: gtk::Orientation::Vertical, set_orientation: gtk::Orientation::Vertical,
set_spacing: 10, set_spacing: 10,
set_margin_end: 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, set_margin_top: 10,
gtk::Separator {},
gtk::Box { gtk::Box {
set_orientation: gtk::Orientation::Horizontal, set_orientation: gtk::Orientation::Horizontal,
set_spacing: 10, set_spacing: 10,
@ -107,8 +109,6 @@ impl FactoryComponent for CommentRow {
set_visible: self.comment.creator.id.0 == settings::get_current_account().id, set_visible: self.comment.creator.id.0 == settings::get_current_account().id,
} }
}, },
gtk::Separator {}
} }
} }

View File

@ -176,8 +176,6 @@ impl SimpleComponent for PostPage {
} }
}, },
gtk::Separator {},
#[local_ref] #[local_ref]
comments -> gtk::Box { comments -> gtk::Box {
set_orientation: gtk::Orientation::Vertical, set_orientation: gtk::Orientation::Vertical,