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 <tyhou13@gmx.com>
pull/3937/head^2
Nutomic 7 months ago committed by GitHub
parent 3a19af5215
commit d827af725a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,

Loading…
Cancel
Save