diff --git a/Cargo.lock b/Cargo.lock index dea7b0c..dbef214 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,9 +461,9 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ff4f84919677303da5f147645dbea6b1881f368d03ac84e1dc09031ebd7b2c6" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ "bytes", "http", @@ -559,9 +559,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" +checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" [[package]] name = "js-sys" @@ -580,13 +580,13 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.125" +version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "libreddit" -version = "0.22.7" +version = "0.22.8" dependencies = [ "askama", "async-recursion", @@ -702,9 +702,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" +checksum = "7b10983b38c53aebdf33f542c6275b0f58a238129d00c4ae0e6fb59738d783ca" [[package]] name = "opaque-debug" @@ -773,11 +773,11 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "proc-macro2" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" +checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] @@ -800,9 +800,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.5" +version = "1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" +checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1" dependencies = [ "aho-corasick", "memchr", @@ -811,9 +811,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.25" +version = "0.6.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" [[package]] name = "ring" @@ -872,9 +872,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.20.5" +version = "0.20.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a024a432ae760ab3bff924ad91ce1cfa52cb57ed16e1ef32d0d249cfee1a6c13" +checksum = "5aab8ee6c7097ed6057f43c187a62418d0c05a4bd5f18b3571db50ee0f9ce033" dependencies = [ "log", "ring", @@ -905,9 +905,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" +checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" [[package]] name = "same-file" @@ -920,12 +920,12 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.19" +version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" dependencies = [ "lazy_static", - "winapi", + "windows-sys", ] [[package]] @@ -1056,13 +1056,13 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.94" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a07e33e919ebcd69113d5be0e4d70c5707004ff45188910106854f38b960df4a" +checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] @@ -1245,6 +1245,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +[[package]] +name = "unicode-ident" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" + [[package]] name = "unicode-normalization" version = "0.1.19" @@ -1254,12 +1260,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-xid" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04" - [[package]] name = "untrusted" version = "0.7.1" diff --git a/Cargo.toml b/Cargo.toml index 0745fb4..d7033de 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.22.7" +version = "0.22.8" authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] edition = "2021" @@ -12,7 +12,7 @@ askama = { version = "0.11.1", default-features = false } async-recursion = "1.0.0" cached = "0.34.0" clap = { version = "3.1.18", default-features = false, features = ["std"] } -regex = "1.5.5" +regex = "1.5.6" serde = { version = "1.0.137", features = ["derive"] } cookie = "0.16.0" futures-lite = "1.12.0" @@ -23,4 +23,4 @@ serde_json = "1.0.81" tokio = { version = "1.18.2", features = ["full"] } time = "0.3.9" url = "2.2.2" -rust-embed="6.4.0" +rust-embed = "6.4.0" diff --git a/src/main.rs b/src/main.rs index dc78a0b..b6f7889 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ mod user; mod utils; // Import Crates -use clap::{Command, Arg}; +use clap::{Arg, Command}; use futures_lite::FutureExt; use hyper::{header::HeaderValue, Body, Request, Response}; @@ -88,7 +88,7 @@ async fn resource(body: &str, content_type: &str, cache: bool) -> Result Result, String> { let mut res = include_str!("../static/style.css").to_string(); for file in ThemeAssets::iter() { - res.push_str("\n"); + res.push('\n'); let theme = ThemeAssets::get(file.as_ref()).unwrap(); res.push_str(std::str::from_utf8(theme.data.as_ref()).unwrap()); } diff --git a/src/post.rs b/src/post.rs index 2ebc313..f9d6e74 100644 --- a/src/post.rs +++ b/src/post.rs @@ -100,7 +100,10 @@ async fn parse_post(json: &serde_json::Value) -> Post { let permalink = val(post, "permalink"); let body = if val(post, "removed_by_category") == "moderator" { - format!("

[removed] — view removed post

", permalink) + format!( + "

[removed] — view removed post

", + permalink + ) } else { rewrite_urls(&val(post, "selftext_html")) }; @@ -199,7 +202,10 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, let highlighted = id == highlighted_comment; let body = if val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]" { - format!("

[removed] — view removed comment

", post_link, id) + format!( + "

[removed] — view removed comment

", + post_link, id + ) } else { rewrite_urls(&val(&comment, "body_html")) }; diff --git a/src/subreddit.rs b/src/subreddit.rs index ca519b9..fcd0d2a 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -97,7 +97,7 @@ pub async fn community(req: Request) -> Result, String> { let path = format!("/r/{}/{}.json?{}&raw_json=1", sub_name.clone(), sort, req.uri().query().unwrap_or_default()); let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str())); - let redirect_url = url[1..].replace('?', "%3F").replace('&', "%26").replace('+',"%2B"); + let redirect_url = url[1..].replace('?', "%3F").replace('&', "%26").replace('+', "%2B"); let filters = get_filters(&req); // If all requested subs are filtered, we don't need to fetch posts. diff --git a/src/user.rs b/src/user.rs index a5a11d0..f83ee98 100644 --- a/src/user.rs +++ b/src/user.rs @@ -5,7 +5,7 @@ use crate::server::RequestExt; use crate::utils::{error, filter_posts, format_url, get_filters, param, template, Post, Preferences, User}; use askama::Template; use hyper::{Body, Request, Response}; -use time::{OffsetDateTime, macros::format_description}; +use time::{macros::format_description, OffsetDateTime}; // STRUCTS #[derive(Template)] diff --git a/src/utils.rs b/src/utils.rs index 419e384..60afc6f 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,12 +6,12 @@ use askama::Template; use cookie::Cookie; use hyper::{Body, Request, Response}; use regex::Regex; +use rust_embed::RustEmbed; use serde_json::Value; use std::collections::{HashMap, HashSet}; use std::str::FromStr; -use time::{Duration, OffsetDateTime, macros::format_description}; +use time::{macros::format_description, Duration, OffsetDateTime}; use url::Url; -use rust_embed::RustEmbed; // Post flair with content, background color and foreground color pub struct Flair { @@ -218,24 +218,19 @@ pub struct Post { impl Post { // Fetch posts of a user or subreddit and return a vector of posts and the "after" value pub async fn fetch(path: &str, quarantine: bool) -> Result<(Vec, String), String> { - let res; - let post_list; - // Send a request to the url - match json(path.to_string(), quarantine).await { + let res = match json(path.to_string(), quarantine).await { // If success, receive JSON in response - Ok(response) => { - res = response; - } + Ok(response) => response, // If the Reddit API returns an error, exit this function Err(msg) => return Err(msg), - } + }; // Fetch the list of posts from the JSON response - match res["data"]["children"].as_array() { - Some(list) => post_list = list, + let post_list = match res["data"]["children"].as_array() { + Some(list) => list, None => return Err("No posts found".to_string()), - } + }; let mut posts: Vec = Vec::new(); @@ -622,12 +617,11 @@ 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()) - // Remove (html-encoded) "\" from URLs. - .replace("%5C", "").replace(r"\", ""); + 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()) + // Remove (html-encoded) "\" from URLs. + .replace("%5C", "") + .replace('\\', ""); // Rewrite external media previews to Libreddit Regex::new(r"https://external-preview\.redd\.it(.*)[^?]").map_or(String::new(), |re| { @@ -671,7 +665,12 @@ pub fn time(created: f64) -> (String, String) { format!("{}m ago", time_delta.whole_minutes()) }; - (rel_time, time.format(format_description!("[month repr:short] [day] [year], [hour]:[minute]:[second] UTC")).unwrap_or_default()) + ( + rel_time, + time + .format(format_description!("[month repr:short] [day] [year], [hour]:[minute]:[second] UTC")) + .unwrap_or_default(), + ) } // val() function used to parse JSON from Reddit APIs @@ -742,7 +741,8 @@ mod tests { #[test] fn rewrite_urls_removes_backslashes() { - let comment_body_html = r#"https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/"#; + let comment_body_html = + r#"https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/"#; assert_eq!( rewrite_urls(comment_body_html), r#"https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/"#