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

View File

@@ -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<GetPostResponse, reqwest::Error> {
let params = GetPost {
id: Some(id),
@@ -27,6 +27,7 @@ pub fn get_comments(post_id: PostId) -> Result<Vec<CommentView>, 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<Vec<CommentView>, 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<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 {

View File

@@ -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 {}
}
}

View File

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