From d827af725a0b44eb1f440763c7d06caa7e094154 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Tue, 17 Oct 2023 19:22:50 +0200 Subject: [PATCH] List distinguished comments first (fixes #3843) (#4050) * List distinguished comments first (fixes #3843) * then_order_by * Fixing assert. * move line * then --------- Co-authored-by: Dessalines --- crates/db_views/src/comment_view.rs | 39 ++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 66b52411d..98536fb8f 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -234,7 +234,7 @@ fn queries<'a>() -> Queries< // only order if filtering by a post id, or parent_path. DOS potential otherwise and max_depth + !post_id isn't used anyways (afaik) if options.post_id.is_some() || options.parent_path.is_some() { // Always order by the parent path first - query = query.order_by(subpath(comment::path, 0, -1)); + query = query.then_order_by(subpath(comment::path, 0, -1)); } // TODO limit question. Limiting does not work for comment threads ATM, only max_depth @@ -253,6 +253,11 @@ fn queries<'a>() -> Queries< limit_and_offset(options.page, options.limit)? }; + // distinguished comments should go first when viewing post + if options.post_id.is_some() || options.parent_path.is_some() { + query = query.then_order_by(comment::distinguished.desc()); + } + query = match options.sort.unwrap_or(CommentSortType::Hot) { CommentSortType::Hot => query .then_order_by(comment_aggregates::hot_rank.desc()) @@ -262,7 +267,7 @@ fn queries<'a>() -> Queries< } CommentSortType::New => query.then_order_by(comment::published.desc()), CommentSortType::Old => query.then_order_by(comment::published.asc()), - CommentSortType::Top => query.order_by(comment_aggregates::score.desc()), + CommentSortType::Top => query.then_order_by(comment_aggregates::score.desc()), }; // Note: deleted and removed comments are done on the front side @@ -332,7 +337,7 @@ mod tests { newtypes::LanguageId, source::{ actor_language::LocalUserLanguage, - comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm}, + comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm, CommentUpdateForm}, community::{Community, CommunityInsertForm}, instance::Instance, language::Language, @@ -746,6 +751,34 @@ mod tests { cleanup(data, pool).await; } + #[tokio::test] + #[serial] + async fn test_distinguished_first() { + let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); + let data = init_data(pool).await; + + let form = CommentUpdateForm { + distinguished: Some(true), + ..Default::default() + }; + Comment::update(pool, data.inserted_comment_2.id, &form) + .await + .unwrap(); + + let comments = CommentQuery { + post_id: Some(data.inserted_comment_2.post_id), + ..Default::default() + } + .list(pool) + .await + .unwrap(); + assert_eq!(comments[0].comment.id, data.inserted_comment_2.id); + assert!(comments[0].comment.distinguished); + + cleanup(data, pool).await; + } + async fn cleanup(data: Data, pool: &mut DbPool<'_>) { CommentLike::remove( pool,