From 6a4191f3b55383e22b5d2a50d218c389f7bfae36 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 9 Sep 2021 17:28:55 -0700 Subject: [PATCH] Fix #272 --- Cargo.lock | 30 +++++++++++++++--------------- Cargo.toml | 2 +- src/main.rs | 12 ++++++++++-- src/search.rs | 11 ++++++++--- src/server.rs | 2 +- src/settings.rs | 2 +- src/subreddit.rs | 4 ++-- src/utils.rs | 5 +++-- 8 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e6c442..26e2034 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -581,9 +581,9 @@ checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "js-sys" -version = "0.3.53" +version = "0.3.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4bf49d50e2961077d9c99f4b7997d770a1114f087c3c2e0069b36c13fc2979d" +checksum = "1866b355d9c878e5e607473cbe3f63282c0b7aad2db1dbebf55076c686918254" dependencies = [ "wasm-bindgen", ] @@ -615,7 +615,7 @@ checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21" [[package]] name = "libreddit" -version = "0.15.1" +version = "0.15.2" dependencies = [ "askama", "async-recursion", @@ -1346,9 +1346,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce9b1b516211d33767048e5d47fa2a381ed8b76fc48d2ce4aa39877f9f183e0" +checksum = "5e68338db6becec24d3c7977b5bf8a48be992c934b5d07177e3931f5dc9b076c" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1356,9 +1356,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfe8dc78e2326ba5f845f4b5bf548401604fa20b1dd1d365fb73b6c1d6364041" +checksum = "f34c405b4f0658583dba0c1c7c9b694f3cac32655db463b56c254a1c75269523" dependencies = [ "bumpalo", "lazy_static", @@ -1371,9 +1371,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44468aa53335841d9d6b6c023eaab07c0cd4bddbcfdee3e2bb1e8d2cb8069fef" +checksum = "b9d5a6580be83b19dc570a8f9c324251687ab2184e57086f71625feb57ec77c8" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1381,9 +1381,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0195807922713af1e67dc66132c7328206ed9766af3858164fb583eedc25fbad" +checksum = "e3775a030dc6f5a0afd8a84981a21cc92a781eb429acef9ecce476d0c9113e92" dependencies = [ "proc-macro2", "quote", @@ -1394,15 +1394,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.76" +version = "0.2.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acdb075a845574a1fa5f09fd77e43f7747599301ea3417a9fbffdeedfc1f4a29" +checksum = "c279e376c7a8e8752a8f1eaa35b7b0bee6bb9fb0cdacfa97cc3f1f289c87e2b4" [[package]] name = "web-sys" -version = "0.3.53" +version = "0.3.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "224b2f6b67919060055ef1a67807367c2066ed520c3862cc013d26cf893a783c" +checksum = "0a84d70d1ec7d2da2d26a5bd78f4bca1b8c3254805363ce743b7a05bc30d195a" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 251e188..0d5b995 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "libreddit" description = " Alternative private front-end to Reddit" license = "AGPL-3.0" repository = "https://github.com/spikecodes/libreddit" -version = "0.15.1" +version = "0.15.2" authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] edition = "2018" diff --git a/src/main.rs b/src/main.rs index b63e9f4..786ce68 100644 --- a/src/main.rs +++ b/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()); diff --git a/src/search.rs b/src/search.rs index 41bb763..2ba8d56 100644 --- a/src/search.rs +++ b/src/search.rs @@ -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) -> Result, 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 { // 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"), diff --git a/src/server.rs b/src/server.rs index 4d74eac..448a971 100644 --- a/src/server.rs +++ b/src/server.rs @@ -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() diff --git a/src/settings.rs b/src/settings.rs index e75fecc..8b12cd8 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -114,7 +114,7 @@ fn set_cookies_method(req: Request, remove_cookies: bool) -> Response { if remove_cookies { - response.remove_cookie(name.to_string()) + response.remove_cookie(name.to_string()); } } }; diff --git a/src/subreddit.rs b/src/subreddit.rs index 4010325..6a4ed10 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -146,7 +146,7 @@ pub async fn add_quarantine_exception(req: Request) -> Result, 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) -> Result, 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()); diff --git a/src/utils.rs b/src/utils.rs index 3526578..c6b9333 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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| {