Allow bypassing nsfw gate for posts
On instances that are not sfw-only, the nsfw gate for posts can now be bypassed.
This commit is contained in:
parent
e25622dac2
commit
e046144bf3
@ -3,7 +3,7 @@
|
|||||||
use crate::client::json;
|
use crate::client::json;
|
||||||
use crate::server::RequestExt;
|
use crate::server::RequestExt;
|
||||||
use crate::subreddit::{can_access_quarantine, quarantine};
|
use crate::subreddit::{can_access_quarantine, quarantine};
|
||||||
use crate::utils::{error, filter_posts, get_filters, nsfw_landing, parse_post, setting, template, Post, Preferences};
|
use crate::utils::{error, filter_posts, get_filters, nsfw_landing, parse_post, template, Post, Preferences};
|
||||||
|
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use hyper::{Body, Request, Response};
|
use hyper::{Body, Request, Response};
|
||||||
@ -67,11 +67,12 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let post = parse_post(&response[0]["data"]["children"][0]).await;
|
let post = parse_post(&response[0]["data"]["children"][0]).await;
|
||||||
|
|
||||||
|
let req_url = req.uri().to_string();
|
||||||
// Return landing page if this post if this Reddit deems this post
|
// Return landing page if this post if this Reddit deems this post
|
||||||
// NSFW, but we have also disabled the display of NSFW content
|
// NSFW, but we have also disabled the display of NSFW content
|
||||||
// or if the instance is SFW-only.
|
// or if the instance is SFW-only
|
||||||
if post.nsfw && (setting(&req, "show_nsfw") != "on" || crate::utils::sfw_only()) {
|
if post.nsfw && crate::utils::should_be_nsfw_gated(&req, &req_url) {
|
||||||
return Ok(nsfw_landing(req).await.unwrap_or_default());
|
return Ok(nsfw_landing(req, req_url).await.unwrap_or_default());
|
||||||
}
|
}
|
||||||
|
|
||||||
let filters = get_filters(&req);
|
let filters = get_filters(&req);
|
||||||
@ -195,14 +196,13 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
after = response[1]["data"]["after"].as_str().unwrap_or_default().to_string();
|
after = response[1]["data"]["after"].as_str().unwrap_or_default().to_string();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let url = req.uri().to_string();
|
|
||||||
|
|
||||||
template(DuplicatesTemplate {
|
template(DuplicatesTemplate {
|
||||||
params: DuplicatesParams { before, after, sort },
|
params: DuplicatesParams { before, after, sort },
|
||||||
post,
|
post,
|
||||||
duplicates,
|
duplicates,
|
||||||
prefs: Preferences::new(&req),
|
prefs: Preferences::new(&req),
|
||||||
url,
|
url: req_url,
|
||||||
num_posts_filtered,
|
num_posts_filtered,
|
||||||
all_posts_filtered,
|
all_posts_filtered,
|
||||||
})
|
})
|
||||||
@ -234,3 +234,4 @@ async fn parse_duplicates(json: &serde_json::Value, filters: &HashSet<String>) -
|
|||||||
let (num_posts_filtered, all_posts_filtered) = filter_posts(&mut duplicates, filters);
|
let (num_posts_filtered, all_posts_filtered) = filter_posts(&mut duplicates, filters);
|
||||||
(duplicates, num_posts_filtered, all_posts_filtered)
|
(duplicates, num_posts_filtered, all_posts_filtered)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,15 +56,15 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
// Parse the JSON into Post and Comment structs
|
// Parse the JSON into Post and Comment structs
|
||||||
let post = parse_post(&response[0]["data"]["children"][0]).await;
|
let post = parse_post(&response[0]["data"]["children"][0]).await;
|
||||||
|
|
||||||
|
let req_url = req.uri().to_string();
|
||||||
// Return landing page if this post if this Reddit deems this post
|
// Return landing page if this post if this Reddit deems this post
|
||||||
// NSFW, but we have also disabled the display of NSFW content
|
// NSFW, but we have also disabled the display of NSFW content
|
||||||
// or if the instance is SFW-only.
|
// or if the instance is SFW-only.
|
||||||
if post.nsfw && (setting(&req, "show_nsfw") != "on" || crate::utils::sfw_only()) {
|
if post.nsfw && crate::utils::should_be_nsfw_gated(&req, &req_url) {
|
||||||
return Ok(nsfw_landing(req).await.unwrap_or_default());
|
return Ok(nsfw_landing(req, req_url).await.unwrap_or_default());
|
||||||
}
|
}
|
||||||
|
|
||||||
let comments = parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &get_filters(&req), &req);
|
let comments = parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &get_filters(&req), &req);
|
||||||
let url = req.uri().to_string();
|
|
||||||
|
|
||||||
// Use the Post and Comment structs to generate a website to show users
|
// Use the Post and Comment structs to generate a website to show users
|
||||||
template(PostTemplate {
|
template(PostTemplate {
|
||||||
@ -73,7 +73,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
sort,
|
sort,
|
||||||
prefs: Preferences::new(&req),
|
prefs: Preferences::new(&req),
|
||||||
single_thread,
|
single_thread,
|
||||||
url,
|
url: req_url,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// If the Reddit API returns an error, exit and send error page to user
|
// If the Reddit API returns an error, exit and send error page to user
|
||||||
@ -190,3 +190,4 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str,
|
|||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,10 +97,11 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let req_url = req.uri().to_string();
|
||||||
// Return landing page if this post if this is NSFW community but the user
|
// Return landing page if this post if this is NSFW community but the user
|
||||||
// has disabled the display of NSFW content or if the instance is SFW-only.
|
// has disabled the display of NSFW content or if the instance is SFW-only.
|
||||||
if sub.nsfw && (setting(&req, "show_nsfw") != "on" || crate::utils::sfw_only()) {
|
if sub.nsfw && crate::utils::should_be_nsfw_gated(&req, &req_url) {
|
||||||
return Ok(nsfw_landing(req).await.unwrap_or_default());
|
return Ok(nsfw_landing(req, req_url).await.unwrap_or_default());
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = format!("/r/{}/{}.json?{}&raw_json=1", sub_name.clone(), sort, req.uri().query().unwrap_or_default());
|
let path = format!("/r/{}/{}.json?{}&raw_json=1", sub_name.clone(), sort, req.uri().query().unwrap_or_default());
|
||||||
@ -433,3 +434,4 @@ async fn subreddit(sub: &str, quarantined: bool) -> Result<Subreddit, String> {
|
|||||||
nsfw: res["data"]["over18"].as_bool().unwrap_or_default(),
|
nsfw: res["data"]["over18"].as_bool().unwrap_or_default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,11 +50,12 @@ pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
|
|||||||
// Retrieve info from user about page.
|
// Retrieve info from user about page.
|
||||||
let user = user(&username).await.unwrap_or_default();
|
let user = user(&username).await.unwrap_or_default();
|
||||||
|
|
||||||
|
let req_url = req.uri().to_string();
|
||||||
// Return landing page if this post if this Reddit deems this user NSFW,
|
// Return landing page if this post if this Reddit deems this user NSFW,
|
||||||
// but we have also disabled the display of NSFW content or if the instance
|
// but we have also disabled the display of NSFW content or if the instance
|
||||||
// is SFW-only.
|
// is SFW-only.
|
||||||
if user.nsfw && (setting(&req, "show_nsfw") != "on" || crate::utils::sfw_only()) {
|
if user.nsfw && crate::utils::should_be_nsfw_gated(&req, &req_url) {
|
||||||
return Ok(nsfw_landing(req).await.unwrap_or_default());
|
return Ok(nsfw_landing(req, req_url).await.unwrap_or_default());
|
||||||
}
|
}
|
||||||
|
|
||||||
let filters = get_filters(&req);
|
let filters = get_filters(&req);
|
||||||
|
16
src/utils.rs
16
src/utils.rs
@ -893,11 +893,21 @@ pub fn sfw_only() -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determines if a request shoud redirect to a nsfw landing gate.
|
||||||
|
pub fn should_be_nsfw_gated(req: &Request<Body>, req_url: &String) -> bool {
|
||||||
|
let sfw_instance = sfw_only();
|
||||||
|
let gate_nsfw = (setting(&req, "show_nsfw") != "on") || sfw_instance;
|
||||||
|
|
||||||
|
// Nsfw landing gate should not be bypassed on a sfw only instance,
|
||||||
|
let bypass_gate = !sfw_instance && req_url.ends_with("&bypass_nsfw_landing");
|
||||||
|
|
||||||
|
gate_nsfw && !bypass_gate
|
||||||
|
}
|
||||||
|
|
||||||
/// Renders the landing page for NSFW content when the user has not enabled
|
/// Renders the landing page for NSFW content when the user has not enabled
|
||||||
/// "show NSFW posts" in settings.
|
/// "show NSFW posts" in settings.
|
||||||
pub async fn nsfw_landing(req: Request<Body>) -> Result<Response<Body>, String> {
|
pub async fn nsfw_landing(req: Request<Body>, req_url: String) -> Result<Response<Body>, String> {
|
||||||
let res_type: ResourceType;
|
let res_type: ResourceType;
|
||||||
let url = req.uri().to_string();
|
|
||||||
|
|
||||||
// Determine from the request URL if the resource is a subreddit, a user
|
// Determine from the request URL if the resource is a subreddit, a user
|
||||||
// page, or a post.
|
// page, or a post.
|
||||||
@ -916,7 +926,7 @@ pub async fn nsfw_landing(req: Request<Body>) -> Result<Response<Body>, String>
|
|||||||
res,
|
res,
|
||||||
res_type,
|
res_type,
|
||||||
prefs: Preferences::new(&req),
|
prefs: Preferences::new(&req),
|
||||||
url,
|
url: req_url,
|
||||||
}
|
}
|
||||||
.render()
|
.render()
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
@ -19,10 +19,12 @@
|
|||||||
{% if crate::utils::sfw_only() %}
|
{% if crate::utils::sfw_only() %}
|
||||||
This instance of Libreddit is SFW-only.</p>
|
This instance of Libreddit is SFW-only.</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
Enable "Show NSFW posts" in <a href="/settings">settings</a> to view this {% if res_type == crate::utils::ResourceType::Subreddit %}subreddit{% else if res_type == crate::utils::ResourceType::User %}user's posts or comments{% else if res_type == crate::utils::ResourceType::Post %}post{% endif %}.
|
Enable "Show NSFW posts" in <a href="/settings">settings</a> to view this {% if res_type == crate::utils::ResourceType::Subreddit %}subreddit{% else if res_type == crate::utils::ResourceType::User %}user's posts or comments{% else if res_type == crate::utils::ResourceType::Post %}post{% endif %}. <br>
|
||||||
|
{% if res_type == crate::utils::ResourceType::Post %} You can also bypass this gate by clicking on this <a href="{{url}}&bypass_nsfw_landing">link</a>.{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block footer %}
|
{% block footer %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user