From ab30b8bbecc187284afd05a36b3fa39e37936795 Mon Sep 17 00:00:00 2001 From: gmnsii <95436780+gmnsii@users.noreply.github.com> Date: Sat, 31 Dec 2022 18:11:59 -0800 Subject: [PATCH] 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 --- src/search.rs | 6 +++++- src/subreddit.rs | 6 +++++- src/user.rs | 6 +++++- templates/search.html | 4 ++++ templates/subreddit.html | 4 ++++ templates/user.html | 4 ++++ 6 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/search.rs b/src/search.rs index 0a62b06..9fbe77a 100644 --- a/src/search.rs +++ b/src/search.rs @@ -44,6 +44,7 @@ struct SearchTemplate { all_posts_filtered: bool, /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) all_posts_hidden_nsfw: bool, + no_posts: bool, } // SERVICES @@ -103,12 +104,14 @@ pub async fn find(req: Request) -> Result, String> { is_filtered: true, all_posts_filtered: false, all_posts_hidden_nsfw: false, + no_posts: false, }) } else { match Post::fetch(&path, quarantined).await { Ok((mut posts, after)) => { 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 { posts, subreddits, @@ -127,6 +130,7 @@ pub async fn find(req: Request) -> Result, String> { is_filtered: false, all_posts_filtered, all_posts_hidden_nsfw, + no_posts, }) } Err(msg) => { diff --git a/src/subreddit.rs b/src/subreddit.rs index 4aff027..ef511c2 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -26,6 +26,7 @@ struct SubredditTemplate { all_posts_filtered: bool, /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) all_posts_hidden_nsfw: bool, + no_posts: bool, } #[derive(Template)] @@ -114,12 +115,14 @@ pub async fn community(req: Request) -> Result, String> { is_filtered: true, all_posts_filtered: false, all_posts_hidden_nsfw: false, + no_posts: false, }) } else { match Post::fetch(&path, quarantined).await { Ok((mut posts, after)) => { 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 { sub, posts, @@ -131,6 +134,7 @@ pub async fn community(req: Request) -> Result, String> { is_filtered: false, all_posts_filtered, all_posts_hidden_nsfw, + no_posts, }) } Err(msg) => match msg.as_str() { diff --git a/src/user.rs b/src/user.rs index 8c0540c..6c991ef 100644 --- a/src/user.rs +++ b/src/user.rs @@ -26,6 +26,7 @@ struct UserTemplate { all_posts_filtered: bool, /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) all_posts_hidden_nsfw: bool, + no_posts: bool, } // FUNCTIONS @@ -61,13 +62,15 @@ pub async fn profile(req: Request) -> Result, String> { is_filtered: true, all_posts_filtered: false, all_posts_hidden_nsfw: false, + no_posts: false, }) } else { // Request user posts/comments from Reddit match Post::fetch(&path, false).await { Ok((mut posts, after)) => { 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 { user, posts, @@ -80,6 +83,7 @@ pub async fn profile(req: Request) -> Result, String> { is_filtered: false, all_posts_filtered, all_posts_hidden_nsfw, + no_posts, }) } // If there is an error show error page diff --git a/templates/search.html b/templates/search.html index 43fadb4..b9742f6 100644 --- a/templates/search.html +++ b/templates/search.html @@ -61,6 +61,10 @@ All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view. {% endif %} + {% if no_posts %} +
No posts were found.
+ {% endif %} + {% if all_posts_filtered %} (All content on this page has been filtered) {% else if is_filtered %} diff --git a/templates/subreddit.html b/templates/subreddit.html index 4fdad65..9ad1932 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -50,6 +50,10 @@
All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.
{% endif %} + {% if no_posts %} +
No posts were found.
+ {% endif %} + {% if all_posts_filtered %}
(All content on this page has been filtered)
{% else %} diff --git a/templates/user.html b/templates/user.html index 04dc4e6..a72cce0 100644 --- a/templates/user.html +++ b/templates/user.html @@ -36,6 +36,10 @@
All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.
{% endif %} + {% if no_posts %} +
No posts were found.
+ {% endif %} + {% if all_posts_filtered %}
(All content on this page has been filtered)
{% else %}