mirror of
https://github.com/redlib-org/redlib.git
synced 2025-05-15 22:42:52 +00:00
Fix #272
This commit is contained in:
parent
668493b72c
commit
6a4191f3b5
8 changed files with 41 additions and 27 deletions
12
src/main.rs
12
src/main.rs
|
@ -1,7 +1,13 @@
|
|||
// Global specifiers
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(clippy::pedantic, clippy::all)]
|
||||
#![allow(clippy::needless_pass_by_value, clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::manual_find_map, clippy::unused_async)]
|
||||
#![allow(
|
||||
clippy::needless_pass_by_value,
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_possible_wrap,
|
||||
clippy::manual_find_map,
|
||||
clippy::unused_async
|
||||
)]
|
||||
|
||||
// Reference local files
|
||||
mod post;
|
||||
|
@ -156,7 +162,9 @@ async fn main() {
|
|||
app
|
||||
.at("/manifest.json")
|
||||
.get(|_| resource(include_str!("../static/manifest.json"), "application/json", false).boxed());
|
||||
app.at("/robots.txt").get(|_| resource("User-agent: *\nDisallow: /u/\nDisallow: /user/", "text/plain", true).boxed());
|
||||
app
|
||||
.at("/robots.txt")
|
||||
.get(|_| resource("User-agent: *\nDisallow: /u/\nDisallow: /user/", "text/plain", true).boxed());
|
||||
app.at("/favicon.ico").get(|_| favicon().boxed());
|
||||
app.at("/logo.png").get(|_| pwa_logo().boxed());
|
||||
app.at("/Inter.var.woff2").get(|_| font().boxed());
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// CRATES
|
||||
use crate::utils::{catch_random, error, format_num, format_url, param, setting, template, val, Post, Preferences};
|
||||
use crate::utils::{catch_random, error, format_num, format_url, param, redirect, setting, template, val, Post, Preferences};
|
||||
use crate::{
|
||||
client::json,
|
||||
subreddit::{can_access_quarantine, quarantine},
|
||||
|
@ -42,13 +42,18 @@ struct SearchTemplate {
|
|||
pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||
let nsfw_results = if setting(&req, "show_nsfw") == "on" { "&include_over_18=on" } else { "" };
|
||||
let path = format!("{}.json?{}{}", req.uri().path(), req.uri().query().unwrap_or_default(), nsfw_results);
|
||||
let query = param(&path, "q").unwrap_or_default();
|
||||
|
||||
if query.is_empty() {
|
||||
return Ok(redirect("/".to_string()));
|
||||
}
|
||||
|
||||
let sub = req.param("sub").unwrap_or_default();
|
||||
let quarantined = can_access_quarantine(&req, &sub);
|
||||
// Handle random subreddits
|
||||
if let Ok(random) = catch_random(&sub, "/find").await {
|
||||
return Ok(random);
|
||||
}
|
||||
let query = param(&path, "q").unwrap_or_default();
|
||||
|
||||
let sort = param(&path, "sort").unwrap_or_else(|| "relevance".to_string());
|
||||
|
||||
|
@ -98,7 +103,7 @@ async fn search_subreddits(q: &str) -> Vec<Subreddit> {
|
|||
// Fetch subreddit icon either from the community_icon or icon_img value
|
||||
let icon = subreddit["data"]["community_icon"]
|
||||
.as_str()
|
||||
.map_or_else(|| val(&subreddit, "icon_img"), ToString::to_string);
|
||||
.map_or_else(|| val(subreddit, "icon_img"), ToString::to_string);
|
||||
|
||||
Subreddit {
|
||||
name: val(subreddit, "display_name_prefixed"),
|
||||
|
|
|
@ -207,7 +207,7 @@ impl Server {
|
|||
// Bind server to address specified above. Gracefully shut down if CTRL+C is pressed
|
||||
let server = HyperServer::bind(address).serve(make_svc).with_graceful_shutdown(async {
|
||||
// Wait for the CTRL+C signal
|
||||
tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler")
|
||||
tokio::signal::ctrl_c().await.expect("Failed to install CTRL+C signal handler");
|
||||
});
|
||||
|
||||
server.boxed()
|
||||
|
|
|
@ -114,7 +114,7 @@ fn set_cookies_method(req: Request<Body>, remove_cookies: bool) -> Response<Body
|
|||
),
|
||||
None => {
|
||||
if remove_cookies {
|
||||
response.remove_cookie(name.to_string())
|
||||
response.remove_cookie(name.to_string());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -146,7 +146,7 @@ pub async fn add_quarantine_exception(req: Request<Body>) -> Result<Response<Bod
|
|||
|
||||
pub fn can_access_quarantine(req: &Request<Body>, sub: &str) -> bool {
|
||||
// Determine if the subreddit can be accessed
|
||||
setting(&req, &format!("allow_quaran_{}", sub.to_lowercase())).parse().unwrap_or_default()
|
||||
setting(req, &format!("allow_quaran_{}", sub.to_lowercase())).parse().unwrap_or_default()
|
||||
}
|
||||
|
||||
// Sub or unsub by setting subscription cookie using response "Set-Cookie" header
|
||||
|
@ -196,7 +196,7 @@ pub async fn subscriptions(req: Request<Body>) -> Result<Response<Body>, String>
|
|||
// Add each sub name to the subscribed list
|
||||
sub_list.push(part.to_owned());
|
||||
// Reorder sub names alphabettically
|
||||
sub_list.sort_by_key(|a| a.to_lowercase())
|
||||
sub_list.sort_by_key(|a| a.to_lowercase());
|
||||
} else if action.contains(&"unsubscribe".to_string()) {
|
||||
// Remove sub name from subscribed list
|
||||
sub_list.retain(|s| s.to_lowercase() != part.to_lowercase());
|
||||
|
|
|
@ -249,7 +249,7 @@ impl Post {
|
|||
let title = esc!(post, "title");
|
||||
|
||||
// Determine the type of media along with the media URL
|
||||
let (post_type, media, gallery) = Media::parse(&data).await;
|
||||
let (post_type, media, gallery) = Media::parse(data).await;
|
||||
|
||||
posts.push(Self {
|
||||
id: val(post, "id"),
|
||||
|
@ -518,7 +518,8 @@ pub fn format_url(url: &str) -> String {
|
|||
|
||||
// Rewrite Reddit links to Libreddit in body of text
|
||||
pub fn rewrite_urls(input_text: &str) -> String {
|
||||
let text1 = Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#).map_or(String::new(), |re| re.replace_all(input_text, r#"href="/"#).to_string());
|
||||
let text1 =
|
||||
Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#).map_or(String::new(), |re| re.replace_all(input_text, r#"href="/"#).to_string());
|
||||
|
||||
// Rewrite external media previews to Libreddit
|
||||
Regex::new(r"https://external-preview\.redd\.it(.*)[^?]").map_or(String::new(), |re| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue