From dea7f3391094f5a4886f7c8e1ba07ffba64f3c45 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 20 Jan 2021 17:38:34 -0800 Subject: [PATCH 1/4] Add code optimizations --- Cargo.lock | 8 ++++---- Cargo.toml | 18 +++++++++--------- src/main.rs | 37 +++++++++++++++++-------------------- src/settings.rs | 9 +++------ static/robots.txt | 2 -- 5 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 static/robots.txt diff --git a/Cargo.lock b/Cargo.lock index ec5d2a2..cd22b1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1485,18 +1485,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.119" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bdd36f49e35b61d49efd8aa7fc068fd295961fd2286d0b2ee9a4c7a14e99cc3" +checksum = "166b2349061381baf54a58e4b13c89369feb0ef2eaa57198899e2312aac30aab" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.119" +version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "552954ce79a059ddd5fd68c271592374bd15cab2274970380c000118aeffe1cd" +checksum = "0ca2a8cb5805ce9e3b95435e3765b7b553cecc762d938d409434338386cb5775" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index d79d1d9..7da7c68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,14 @@ authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] edition = "2018" [dependencies] -base64 = "0.13.0" -actix-web = { version = "3.3.2", features = ["rustls"] } +base64 = "0.13" +actix-web = { version = "3.3", features = ["rustls"] } futures = "0.3" -askama = "0.10.5" -ureq = "2.0.1" -serde = { version = "1.0.118", default_features = false, features = ["derive"] } +askama = "0.10" +ureq = "2.0" +serde = { version = "1.0", default_features = false, features = ["derive"] } serde_json = "1.0" -async-recursion = "0.3.1" -url = "2.2.0" -regex = "1.4.2" -time = "0.2.23" \ No newline at end of file +async-recursion = "0.3" +url = "2.2" +regex = "1.4" +time = "0.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a0f5f37..c705bbb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,15 +53,16 @@ async fn main() -> std::io::Result<()> { .wrap_fn(move |req, srv| { let secure = req.connection_info().scheme() == "https"; let https_url = format!("https://{}{}", req.connection_info().host(), req.uri().to_string()); - srv.call(req).map(move |res: Result| { + srv.call(req).map(move |res: Result| if force_https && !secure { - let redirect: ServiceResponse = - ServiceResponse::new(res.unwrap().request().clone(), HttpResponse::Found().header("Location", https_url).finish()); - Ok(redirect) + Ok(ServiceResponse::new( + res.unwrap().request().to_owned(), + HttpResponse::Found().header("Location", https_url).finish(), + )) } else { res } - }) + ) }) // Append trailing slash and remove double slashes .wrap(middleware::NormalizePath::default()) @@ -106,23 +107,19 @@ async fn main() -> std::io::Result<()> { .route("/{page}/", web::get().to(subreddit::wiki)), ), ) - // Universal services + // Front page + .route("/", web::get().to(subreddit::page)) + .route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page)) + // View Reddit wiki .service( - web::scope("") - // Front page - .route("/", web::get().to(subreddit::page)) - .route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page)) - // View Reddit wiki - .service( - web::scope("/wiki") - .route("/", web::get().to(subreddit::wiki)) - .route("/{page}/", web::get().to(subreddit::wiki)), - ) - // Search all of Reddit - .route("/search/", web::get().to(search::find)) - // Short link for post - .route("/{id:.{5,6}}/", web::get().to(post::item)), + web::scope("/wiki") + .route("/", web::get().to(subreddit::wiki)) + .route("/{page}/", web::get().to(subreddit::wiki)), ) + // Search all of Reddit + .route("/search/", web::get().to(search::find)) + // Short link for post + .route("/{id:.{5,6}}/", web::get().to(post::item)) }) .bind(&address) .unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e)) diff --git a/src/settings.rs b/src/settings.rs index 2fdb72f..3b0fd72 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,6 +1,6 @@ // CRATES use crate::utils::{prefs, Preferences}; -use actix_web::{cookie::Cookie, web::Form, HttpMessage, HttpRequest, HttpResponse}; +use actix_web::{cookie::Cookie, web::Form, HttpRequest, HttpResponse}; use askama::Template; use time::{Duration, OffsetDateTime}; @@ -30,7 +30,7 @@ pub async fn get(req: HttpRequest) -> HttpResponse { } // Set cookies using response "Set-Cookie" header -pub async fn set(req: HttpRequest, form: Form) -> HttpResponse { +pub async fn set(_req: HttpRequest, form: Form) -> HttpResponse { let mut res = HttpResponse::Found(); let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "hide_nsfw"]; @@ -45,10 +45,7 @@ pub async fn set(req: HttpRequest, form: Form) -> HttpResponse { .expires(OffsetDateTime::now_utc() + Duration::weeks(52)) .finish(), ), - None => match HttpMessage::cookie(&req, name.to_owned()) { - Some(cookie) => res.del_cookie(&cookie), - None => &mut res, - }, + None => res.del_cookie(&Cookie::named(name.to_owned())), }; } diff --git a/static/robots.txt b/static/robots.txt deleted file mode 100644 index 14267e9..0000000 --- a/static/robots.txt +++ /dev/null @@ -1,2 +0,0 @@ -User-agent: * -Allow: / \ No newline at end of file From 610fcfbf876e6bb2204d6e87293346461ba7eb1e Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 20 Jan 2021 17:55:04 -0800 Subject: [PATCH 2/4] Fix #67 --- static/style.css | 1 + 1 file changed, 1 insertion(+) diff --git a/static/style.css b/static/style.css index e9f9319..6fef076 100644 --- a/static/style.css +++ b/static/style.css @@ -668,6 +668,7 @@ a.search_subreddit:hover { padding: 10px 0 10px 5px; flex-grow: 1; flex-shrink: 1; + min-width: 0; } .comment_data > * { From 6641e242afeae74b1c1012e92493f22cc585d994 Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Wed, 20 Jan 2021 18:26:58 -0800 Subject: [PATCH 3/4] Allow NSFW searching --- src/search.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/search.rs b/src/search.rs index 35330c5..55f26a7 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, fetch_posts, param, prefs, request, val, Post, Preferences}; +use crate::utils::{error, fetch_posts, param, prefs, request, val, Post, Preferences, cookie}; use actix_web::{HttpRequest, HttpResponse}; use askama::Template; @@ -33,7 +33,8 @@ struct SearchTemplate { // SERVICES pub async fn find(req: HttpRequest) -> HttpResponse { - let path = format!("{}.json?{}", req.path(), req.query_string()); + let nsfw_results = if cookie(&req, "hide_nsfw") != "on" { "&include_over_18=on" } else { "" }; + let path = format!("{}.json?{}{}", req.path(), req.query_string(), nsfw_results); let sub = req.match_info().get("sub").unwrap_or("").to_string(); let sort = if param(&path, "sort").is_empty() { From 2270b6cf9526ea177d0687d00b9a54562e4c96db Mon Sep 17 00:00:00 2001 From: spikecodes <19519553+spikecodes@users.noreply.github.com> Date: Thu, 21 Jan 2021 21:25:51 -0800 Subject: [PATCH 4/4] Reduce post padding --- src/search.rs | 2 +- src/subreddit.rs | 2 +- src/utils.rs | 2 +- static/style.css | 21 +++++++-------------- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/search.rs b/src/search.rs index 55f26a7..62b555d 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, fetch_posts, param, prefs, request, val, Post, Preferences, cookie}; +use crate::utils::{cookie, error, fetch_posts, param, prefs, request, val, Post, Preferences}; use actix_web::{HttpRequest, HttpResponse}; use askama::Template; diff --git a/src/subreddit.rs b/src/subreddit.rs index ea57322..52bcc34 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -98,7 +98,7 @@ async fn subreddit(sub: &str) -> Result { let active: i64 = res["data"]["accounts_active"].as_u64().unwrap_or_default() as i64; // Fetch subreddit icon either from the community_icon or icon_img value - let community_icon: &str = res["data"]["community_icon"].as_str().unwrap_or("").split('?').collect::>()[0]; + let community_icon: &str = res["data"]["community_icon"].as_str().map_or("", |s| s.split('?').collect::>()[0]); let icon = if community_icon.is_empty() { val(&res, "icon_img") } else { community_icon.to_string() }; let sub = Subreddit { diff --git a/src/utils.rs b/src/utils.rs index 0d32c03..9d6db88 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -286,7 +286,7 @@ pub fn time(created: f64) -> (String, String) { // val() function used to parse JSON from Reddit APIs pub fn val(j: &Value, k: &str) -> String { - String::from(j["data"][k].as_str().unwrap_or_default()) + j["data"][k].as_str().unwrap_or_default().to_string() } // Fetch posts of a user or subreddit and return a vector of posts and the "after" value diff --git a/static/style.css b/static/style.css index 7f2bfa4..6c2f914 100644 --- a/static/style.css +++ b/static/style.css @@ -460,7 +460,7 @@ a.search_subreddit:hover { } .post_score { - padding-top: 20px; + padding-top: 16px; font-size: 13px; font-weight: bold; text-align: end; @@ -476,7 +476,7 @@ a.search_subreddit:hover { } .post_header { - margin: 20px 20px 5px 20px; + margin: 15px 20px 5px 15px; grid-area: post_header; } @@ -487,7 +487,7 @@ a.search_subreddit:hover { .post_title { font-size: 16px; line-height: 1.5; - margin: 5px 20px; + margin: 5px 15px; grid-area: post_title; } @@ -548,7 +548,7 @@ a.search_subreddit:hover { .post_body { opacity: 0.9; font-weight: normal; - margin: 5px 20px; + margin: 5px 15px; grid-area: post_body; } @@ -558,7 +558,7 @@ a.search_subreddit:hover { opacity: 0.5; font-size: 14px; grid-area: post_footer; - margin: 5px 20px 20px 20px; + margin: 5px 20px 15px 15px; } .post_comments { @@ -925,20 +925,13 @@ td, th { } .post_score { - background-color: unset; - margin: 5px 0px 20px 20px; + margin: 5px 0px 20px 15px; padding: 0; } - .compact .post_score { - background-color: unset; - margin: 2.5px 0px 15px 15px; - padding: 0; - font-size: 14px; - } + .compact .post_score { padding: 0; } .post_score::before { content: "↑" } - .post:hover > .post_score { background: unset; } .post_header { font-size: 14px; } .post_footer { margin-left: 15px; }