Add filter to hide posts with comments. (#5158)

* Add filter to hide posts with comments.

- Useful for Q/A type communities.
- Fixes #1106

* Changing to no_comments_only
This commit is contained in:
Dessalines 2024-11-05 12:09:25 -05:00 committed by GitHub
parent 9f40387569
commit 298c8fa521
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 0 deletions

View file

@ -85,6 +85,8 @@ pub struct GetPosts {
pub show_read: Option<bool>, pub show_read: Option<bool>,
/// If true, then show the nsfw posts (even if your user setting is to hide them) /// If true, then show the nsfw posts (even if your user setting is to hide them)
pub show_nsfw: Option<bool>, pub show_nsfw: Option<bool>,
/// If true, then only show posts with no comments
pub no_comments_only: Option<bool>,
pub page_cursor: Option<PaginationCursor>, pub page_cursor: Option<PaginationCursor>,
} }

View file

@ -42,6 +42,7 @@ pub async fn list_posts(
let show_hidden = data.show_hidden; let show_hidden = data.show_hidden;
let show_read = data.show_read; let show_read = data.show_read;
let show_nsfw = data.show_nsfw; let show_nsfw = data.show_nsfw;
let no_comments_only = data.no_comments_only;
let liked_only = data.liked_only; let liked_only = data.liked_only;
let disliked_only = data.disliked_only; let disliked_only = data.disliked_only;
@ -82,6 +83,7 @@ pub async fn list_posts(
show_hidden, show_hidden,
show_read, show_read,
show_nsfw, show_nsfw,
no_comments_only,
..Default::default() ..Default::default()
} }
.list(&local_site.site, &mut context.pool()) .list(&local_site.site, &mut context.pool())

View file

@ -401,6 +401,11 @@ fn queries<'a>() -> Queries<
query = query.filter(person::bot_account.eq(false)); query = query.filter(person::bot_account.eq(false));
}; };
// Filter to show only posts with no comments
if options.no_comments_only.unwrap_or_default() {
query = query.filter(post_aggregates::comments.eq(0));
};
// If its saved only, then filter, and order by the saved time, not the comment creation time. // If its saved only, then filter, and order by the saved time, not the comment creation time.
if options.saved_only.unwrap_or_default() { if options.saved_only.unwrap_or_default() {
query = query query = query
@ -617,6 +622,7 @@ pub struct PostQuery<'a> {
pub show_hidden: Option<bool>, pub show_hidden: Option<bool>,
pub show_read: Option<bool>, pub show_read: Option<bool>,
pub show_nsfw: Option<bool>, pub show_nsfw: Option<bool>,
pub no_comments_only: Option<bool>,
} }
impl<'a> PostQuery<'a> { impl<'a> PostQuery<'a> {
@ -1988,4 +1994,34 @@ mod tests {
cleanup(data, pool).await cleanup(data, pool).await
} }
#[tokio::test]
#[serial]
async fn post_listings_no_comments_only() -> LemmyResult<()> {
let pool = &build_db_pool().await?;
let pool = &mut pool.into();
let data = init_data(pool).await?;
// Create a comment for a post
let comment_form = CommentInsertForm::new(
data.local_user_view.person.id,
data.inserted_post.id,
"a comment".to_owned(),
);
Comment::create(pool, &comment_form, None).await?;
// Make sure it doesnt come back with the no_comments option
let post_listings_no_comments = PostQuery {
sort: Some(PostSortType::New),
no_comments_only: Some(true),
local_user: Some(&data.local_user_view.local_user),
..Default::default()
}
.list(&data.site, pool)
.await?;
assert_eq!(vec![POST_BY_BOT], names(&post_listings_no_comments));
cleanup(data, pool).await
}
} }