Implement "posts hidden because of NSFW"

This commit is contained in:
sigaloid 2022-05-12 19:17:54 -04:00
parent 3facaefb53
commit 4c26faaba8
6 changed files with 30 additions and 5 deletions

View File

@ -42,6 +42,8 @@ struct SearchTemplate {
/// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place,
/// and all fetched posts being filtered). /// and all fetched posts being filtered).
all_posts_filtered: bool, all_posts_filtered: bool,
/// Whether any posts were hidden because they are NSFW (and user has disabled show NSFW)
any_posts_hidden_nsfw: bool,
} }
// SERVICES // SERVICES
@ -100,12 +102,13 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
url, url,
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
any_posts_hidden_nsfw: 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 any_posts_hidden_nsfw = posts.iter().any(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on";
template(SearchTemplate { template(SearchTemplate {
posts, posts,
subreddits, subreddits,
@ -123,6 +126,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
url, url,
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,
any_posts_hidden_nsfw,
}) })
} }
Err(msg) => { Err(msg) => {

View File

@ -25,6 +25,8 @@ struct SubredditTemplate {
/// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place,
/// and all fetched posts being filtered). /// and all fetched posts being filtered).
all_posts_filtered: bool, all_posts_filtered: bool,
/// Whether any posts were hidden because they are NSFW (and user has disabled show NSFW)
any_posts_hidden_nsfw: bool,
} }
#[derive(Template)] #[derive(Template)]
@ -112,12 +114,13 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
redirect_url, redirect_url,
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
any_posts_hidden_nsfw: 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 any_posts_hidden_nsfw = posts.iter().any(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on";
template(SubredditTemplate { template(SubredditTemplate {
sub, sub,
posts, posts,
@ -128,6 +131,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
redirect_url, redirect_url,
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,
any_posts_hidden_nsfw,
}) })
} }
Err(msg) => match msg.as_str() { Err(msg) => match msg.as_str() {

View File

@ -2,10 +2,10 @@
use crate::client::json; use crate::client::json;
use crate::esc; use crate::esc;
use crate::server::RequestExt; 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 askama::Template;
use hyper::{Body, Request, Response}; use hyper::{Body, Request, Response};
use time::{OffsetDateTime, macros::format_description}; use time::{macros::format_description, OffsetDateTime};
// STRUCTS // STRUCTS
#[derive(Template)] #[derive(Template)]
@ -25,6 +25,8 @@ struct UserTemplate {
/// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place, /// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place,
/// and all fetched posts being filtered). /// and all fetched posts being filtered).
all_posts_filtered: bool, all_posts_filtered: bool,
/// Whether any posts were hidden because they are NSFW (and user has disabled show NSFW)
any_posts_hidden_nsfw: bool,
} }
// FUNCTIONS // FUNCTIONS
@ -59,13 +61,14 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
redirect_url, redirect_url,
is_filtered: true, is_filtered: true,
all_posts_filtered: false, all_posts_filtered: false,
any_posts_hidden_nsfw: 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 any_posts_hidden_nsfw = posts.iter().any(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on";
template(UserTemplate { template(UserTemplate {
user, user,
posts, posts,
@ -77,6 +80,7 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
redirect_url, redirect_url,
is_filtered: false, is_filtered: false,
all_posts_filtered, all_posts_filtered,
any_posts_hidden_nsfw,
}) })
} }
// If there is an error show error page // If there is an error show error page

View File

@ -56,6 +56,11 @@
</div> </div>
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if any_posts_hidden_nsfw %}
<center>Some posts were hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</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 if is_filtered %} {% else if is_filtered %}

View File

@ -46,6 +46,10 @@
</form> </form>
{% endif %} {% endif %}
{% if any_posts_hidden_nsfw %}
<center>Some posts were hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</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

@ -32,6 +32,10 @@
</button> </button>
</form> </form>
{% if any_posts_hidden_nsfw %}
<center>Some posts were hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</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 %}