From 53a226b9443816452c33ee10a0e4d3457f1be6a3 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 9 Jul 2024 13:44:23 -0400 Subject: [PATCH] Add show_nsfw override filter to GetPosts. (#4889) - Fixes #4124 --- crates/api_common/src/post.rs | 2 ++ crates/apub/src/api/list_posts.rs | 2 ++ crates/db_views/src/post_view.rs | 48 ++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index f42a468b5..74369173b 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -81,6 +81,8 @@ pub struct GetPosts { pub show_hidden: Option, /// If true, then show the read posts (even if your user setting is to hide them) pub show_read: Option, + /// If true, then show the nsfw posts (even if your user setting is to hide them) + pub show_nsfw: Option, pub page_cursor: Option, } diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index c00c87f4c..7ceafed8d 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -43,6 +43,7 @@ pub async fn list_posts( let saved_only = data.saved_only; let show_hidden = data.show_hidden; let show_read = data.show_read; + let show_nsfw = data.show_nsfw; let liked_only = data.liked_only; let disliked_only = data.disliked_only; @@ -84,6 +85,7 @@ pub async fn list_posts( limit, show_hidden, show_read, + show_nsfw, ..Default::default() } .list(&local_site.site, &mut context.pool()) diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 588bb017b..0ec7e0a5d 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -396,7 +396,10 @@ fn queries<'a>() -> Queries< .filter(not(post::removed.or(post::deleted))); } - if !options.local_user.show_nsfw(site) { + if !options + .show_nsfw + .unwrap_or(options.local_user.show_nsfw(site)) + { query = query .filter(post::nsfw.eq(false)) .filter(community::nsfw.eq(false)); @@ -621,6 +624,7 @@ pub struct PostQuery<'a> { pub page_back: Option, pub show_hidden: Option, pub show_read: Option, + pub show_nsfw: Option, } impl<'a> PostQuery<'a> { @@ -1589,6 +1593,48 @@ mod tests { cleanup(data, pool).await } + #[tokio::test] + #[serial] + async fn post_listings_hide_nsfw() -> LemmyResult<()> { + let pool = &build_db_pool().await?; + let pool = &mut pool.into(); + let data = init_data(pool).await?; + + // Mark a post as nsfw + let update_form = PostUpdateForm { + nsfw: Some(true), + ..Default::default() + }; + + Post::update(pool, data.inserted_bot_post.id, &update_form).await?; + + // Make sure you don't see the nsfw post in the regular results + let post_listings_hide_nsfw = data.default_post_query().list(&data.site, pool).await?; + assert_eq!(vec![POST], names(&post_listings_hide_nsfw)); + + // Make sure it does come back with the show_nsfw option + let post_listings_show_nsfw = PostQuery { + sort: Some(SortType::New), + show_nsfw: Some(true), + local_user: Some(&data.local_user_view.local_user), + ..Default::default() + } + .list(&data.site, pool) + .await?; + assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_nsfw)); + + // Make sure that nsfw field is true. + assert!( + &post_listings_show_nsfw + .first() + .ok_or(LemmyErrorType::CouldntFindPost)? + .post + .nsfw + ); + + cleanup(data, pool).await + } + async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> { let num_deleted = Post::delete(pool, data.inserted_post.id).await?; Community::delete(pool, data.inserted_community.id).await?;