Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show_nsfw override filter to GetPosts. #4889

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add show_nsfw override filter to GetPosts.
- Fixes #4124
  • Loading branch information
dessalines committed Jul 7, 2024
commit 441e87691e81bc5e61c0d742f918c3fdadf026e8
2 changes: 2 additions & 0 deletions crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub struct GetPosts {
pub show_hidden: Option<bool>,
/// If true, then show the read posts (even if your user setting is to hide them)
pub show_read: Option<bool>,
/// If true, then show the nsfw posts (even if your user setting is to hide them)
pub show_nsfw: Option<bool>,
pub page_cursor: Option<PaginationCursor>,
}

Expand Down
2 changes: 2 additions & 0 deletions crates/apub/src/api/list_posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
48 changes: 47 additions & 1 deletion crates/db_views/src/post_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,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));
Expand Down Expand Up @@ -617,6 +620,7 @@ pub struct PostQuery<'a> {
pub page_back: Option<bool>,
pub show_hidden: Option<bool>,
pub show_read: Option<bool>,
pub show_nsfw: Option<bool>,
}

impl<'a> PostQuery<'a> {
Expand Down Expand Up @@ -1585,6 +1589,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?;
Expand Down