Sfoglia il codice sorgente

Implement 'posts hidden because of NSFW'. (Resolves #159) (#619)

Daniel Valentine 2 anni fa
parent
commit
465d9b7ba7
7 ha cambiato i file con 30 aggiunte e 9 eliminazioni
  1. 1 5
      src/client.rs
  2. 5 1
      src/search.rs
  3. 5 1
      src/subreddit.rs
  4. 6 2
      src/user.rs
  5. 5 0
      templates/search.html
  6. 4 0
      templates/subreddit.html
  7. 4 0
      templates/user.html

+ 1 - 5
src/client.rs

@@ -46,11 +46,7 @@ pub async fn canonical_path(path: String) -> Result<Option<String>, String> {
 		res
 			.headers()
 			.get(header::LOCATION)
-			.map(|val| percent_encode(val.as_bytes(), CONTROLS)
-				.to_string()
-				.trim_start_matches(REDDIT_URL_BASE)
-				.to_string()
-			),
+			.map(|val| percent_encode(val.as_bytes(), CONTROLS).to_string().trim_start_matches(REDDIT_URL_BASE).to_string()),
 	)
 }
 

+ 5 - 1
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<Body>) -> Result<Response<Body>, 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<Body>) -> Result<Response<Body>, String> {
 					url,
 					is_filtered: false,
 					all_posts_filtered,
+					all_posts_hidden_nsfw,
 				})
 			}
 			Err(msg) => {

+ 5 - 1
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<Body>) -> Result<Response<Body>, 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<Body>) -> Result<Response<Body>, String> {
 					redirect_url,
 					is_filtered: false,
 					all_posts_filtered,
+					all_posts_hidden_nsfw,
 				})
 			}
 			Err(msg) => match msg.as_str() {

+ 6 - 2
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<Body>) -> Result<Response<Body>, 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<Body>) -> Result<Response<Body>, String> {
 					redirect_url,
 					is_filtered: false,
 					all_posts_filtered,
+					all_posts_hidden_nsfw,
 				})
 			}
 			// If there is an error show error page

+ 5 - 0
templates/search.html

@@ -56,6 +56,11 @@
 		</div>
 		{% endif %}
 		{% endif %}
+
+		{% if all_posts_hidden_nsfw %}
+		<center>All posts are hidden because they are NSFW. Enable "Show NSFW posts" in settings to view.</center>
+		{% endif %}
+
 		{% if all_posts_filtered %}
 			<center>(All content on this page has been filtered)</center>
 		{% else if is_filtered %}

+ 4 - 0
templates/subreddit.html

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

+ 4 - 0
templates/user.html

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