Bugfix: 'all posts are hidden because NSFW' when no posts where found (#666)

* Fix 'all_posts_hidden_nsfw' when there are no posts.

If a search query yielded no results and the user set nsfw posts to be
hidden, libreddit would show 'All posts are hidden because they are NSFW.
Enable "Show NSFW posts" in settings to view'. This is fixed by
verifying tnat posts.len > 0 before setting 'all_posts_hidden_nsfw' to
true.

* Add a message when no posts were found.

* Delete 2
This commit is contained in:
gmnsii 2022-12-31 18:11:59 -08:00 committed by GitHub
parent 37d1939dc0
commit ab30b8bbec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 3 deletions

View file

@ -44,6 +44,7 @@ struct SearchTemplate {
all_posts_filtered: bool, all_posts_filtered: bool,
/// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW)
all_posts_hidden_nsfw: bool, all_posts_hidden_nsfw: bool,
no_posts: bool,
} }
// SERVICES // SERVICES
@ -103,12 +104,14 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
all_posts_hidden_nsfw: false, all_posts_hidden_nsfw: false,
no_posts: false,
}) })
} else { } else {
match Post::fetch(&path, quarantined).await { match Post::fetch(&path, quarantined).await {
Ok((mut posts, after)) => { Ok((mut posts, after)) => {
let (_, all_posts_filtered) = filter_posts(&mut posts, &filters); let (_, all_posts_filtered) = filter_posts(&mut posts, &filters);
let all_posts_hidden_nsfw = posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on"; let no_posts = posts.is_empty();
let all_posts_hidden_nsfw = !no_posts && (posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on");
template(SearchTemplate { template(SearchTemplate {
posts, posts,
subreddits, subreddits,
@ -127,6 +130,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,
all_posts_hidden_nsfw, all_posts_hidden_nsfw,
no_posts,
}) })
} }
Err(msg) => { Err(msg) => {

View file

@ -26,6 +26,7 @@ struct SubredditTemplate {
all_posts_filtered: bool, all_posts_filtered: bool,
/// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW)
all_posts_hidden_nsfw: bool, all_posts_hidden_nsfw: bool,
no_posts: bool,
} }
#[derive(Template)] #[derive(Template)]
@ -114,12 +115,14 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
all_posts_hidden_nsfw: false, all_posts_hidden_nsfw: false,
no_posts: false,
}) })
} else { } else {
match Post::fetch(&path, quarantined).await { match Post::fetch(&path, quarantined).await {
Ok((mut posts, after)) => { Ok((mut posts, after)) => {
let (_, all_posts_filtered) = filter_posts(&mut posts, &filters); let (_, all_posts_filtered) = filter_posts(&mut posts, &filters);
let all_posts_hidden_nsfw = posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on"; let no_posts = posts.is_empty();
let all_posts_hidden_nsfw = !no_posts && (posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on");
template(SubredditTemplate { template(SubredditTemplate {
sub, sub,
posts, posts,
@ -131,6 +134,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,
all_posts_hidden_nsfw, all_posts_hidden_nsfw,
no_posts,
}) })
} }
Err(msg) => match msg.as_str() { Err(msg) => match msg.as_str() {

View file

@ -26,6 +26,7 @@ struct UserTemplate {
all_posts_filtered: bool, all_posts_filtered: bool,
/// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW)
all_posts_hidden_nsfw: bool, all_posts_hidden_nsfw: bool,
no_posts: bool,
} }
// FUNCTIONS // FUNCTIONS
@ -61,13 +62,15 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
all_posts_hidden_nsfw: false, all_posts_hidden_nsfw: false,
no_posts: false,
}) })
} else { } else {
// Request user posts/comments from Reddit // Request user posts/comments from Reddit
match Post::fetch(&path, false).await { match Post::fetch(&path, false).await {
Ok((mut posts, after)) => { Ok((mut posts, after)) => {
let (_, all_posts_filtered) = filter_posts(&mut posts, &filters); let (_, all_posts_filtered) = filter_posts(&mut posts, &filters);
let all_posts_hidden_nsfw = posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on"; let no_posts = posts.is_empty();
let all_posts_hidden_nsfw = !no_posts && (posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on");
template(UserTemplate { template(UserTemplate {
user, user,
posts, posts,
@ -80,6 +83,7 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,
all_posts_hidden_nsfw, all_posts_hidden_nsfw,
no_posts,
}) })
} }
// If there is an error show error page // If there is an error show error page

View file

@ -61,6 +61,10 @@
<span class="listing_warn">All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</span> <span class="listing_warn">All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</span>
{% endif %} {% endif %}
{% if no_posts %}
<center>No posts were found.</center>
{% endif %}
{% if all_posts_filtered %} {% if all_posts_filtered %}
<span class="listing_warn">(All content on this page has been filtered)</span> <span class="listing_warn">(All content on this page has been filtered)</span>
{% else if is_filtered %} {% else if is_filtered %}

View file

@ -50,6 +50,10 @@
<center>All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</center> <center>All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</center>
{% endif %} {% endif %}
{% if no_posts %}
<center>No posts were found.</center>
{% endif %}
{% if all_posts_filtered %} {% if all_posts_filtered %}
<center>(All content on this page has been filtered)</center> <center>(All content on this page has been filtered)</center>
{% else %} {% else %}

View file

@ -36,6 +36,10 @@
<center>All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</center> <center>All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</center>
{% endif %} {% endif %}
{% if no_posts %}
<center>No posts were found.</center>
{% endif %}
{% if all_posts_filtered %} {% if all_posts_filtered %}
<center>(All content on this page has been filtered)</center> <center>(All content on this page has been filtered)</center>
{% else %} {% else %}