From 238ef73ed43339a77299a5510c065a03efe52f36 Mon Sep 17 00:00:00 2001 From: Matt <69441971+sigaloid@users.noreply.github.com> Date: Sat, 24 Sep 2022 22:14:01 -0400 Subject: [PATCH] Implement 'posts hidden because of NSFW' #508 (#9) Co-authored-by: Matthew Esposito --- src/search.rs | 6 +++++- src/subreddit.rs | 6 +++++- src/user.rs | 8 ++++++-- templates/search.html | 5 +++++ templates/subreddit.html | 4 ++++ templates/user.html | 4 ++++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/search.rs b/src/search.rs index 4dd3b5f..4b13594 100644 --- a/src/search.rs +++ b/src/search.rs @@ -42,6 +42,8 @@ struct SearchTemplate { /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, /// and all fetched posts being filtered). all_posts_filtered: bool, + /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) + all_posts_hidden_nsfw: bool, } // SERVICES @@ -100,12 +102,13 @@ pub async fn find(req: Request) -> Result, String> { url, is_filtered: true, all_posts_filtered: false, + all_posts_hidden_nsfw: 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"; template(SearchTemplate { posts, subreddits, @@ -123,6 +126,7 @@ pub async fn find(req: Request) -> Result, String> { url, is_filtered: false, all_posts_filtered, + all_posts_hidden_nsfw, }) } Err(msg) => { diff --git a/src/subreddit.rs b/src/subreddit.rs index b03e7e9..75e271a 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -24,6 +24,8 @@ struct SubredditTemplate { /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, /// and all fetched posts being filtered). all_posts_filtered: bool, + /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) + all_posts_hidden_nsfw: bool, } #[derive(Template)] @@ -111,12 +113,13 @@ pub async fn community(req: Request) -> Result, String> { redirect_url, is_filtered: true, all_posts_filtered: false, + all_posts_hidden_nsfw: 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"; template(SubredditTemplate { sub, posts, @@ -127,6 +130,7 @@ pub async fn community(req: Request) -> Result, String> { redirect_url, is_filtered: false, all_posts_filtered, + all_posts_hidden_nsfw, }) } Err(msg) => match msg.as_str() { diff --git a/src/user.rs b/src/user.rs index 50a2200..4791ed3 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,7 +1,7 @@ // CRATES use crate::client::json; use crate::server::RequestExt; -use crate::utils::{error, filter_posts, format_url, get_filters, param, template, Post, Preferences, User}; +use crate::utils::{error, filter_posts, format_url, get_filters, param, setting, template, Post, Preferences, User}; use askama::Template; use hyper::{Body, Request, Response}; use time::{macros::format_description, OffsetDateTime}; @@ -24,6 +24,8 @@ struct UserTemplate { /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, /// and all fetched posts being filtered). all_posts_filtered: bool, + /// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW) + all_posts_hidden_nsfw: bool, } // FUNCTIONS @@ -58,13 +60,14 @@ pub async fn profile(req: Request) -> Result, String> { redirect_url, is_filtered: true, all_posts_filtered: false, + all_posts_hidden_nsfw: 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"; template(UserTemplate { user, posts, @@ -76,6 +79,7 @@ pub async fn profile(req: Request) -> Result, String> { redirect_url, is_filtered: false, all_posts_filtered, + all_posts_hidden_nsfw, }) } // If there is an error show error page diff --git a/templates/search.html b/templates/search.html index 7625133..4f0f44e 100644 --- a/templates/search.html +++ b/templates/search.html @@ -56,6 +56,11 @@ {% endif %} {% endif %} + + {% if all_posts_hidden_nsfw %} +
All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.
+ {% 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 b42b9d1..686c2a6 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -46,6 +46,10 @@ {% endif %} + {% if all_posts_hidden_nsfw %} +
All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.
+ {% 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 f073ce6..27cdd94 100644 --- a/templates/user.html +++ b/templates/user.html @@ -32,6 +32,10 @@ + {% if all_posts_hidden_nsfw %} +
All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.
+ {% endif %} + {% if all_posts_filtered %}
(All content on this page has been filtered)
{% else %}