Update to v0.22.8

This commit is contained in:
spikecodes 2022-05-20 19:20:44 -07:00
parent 0ec8e4e9a2
commit 428dc58e3c
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
7 changed files with 68 additions and 62 deletions

64
Cargo.lock generated
View File

@ -461,9 +461,9 @@ dependencies = [
[[package]] [[package]]
name = "http-body" name = "http-body"
version = "0.4.4" version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ff4f84919677303da5f147645dbea6b1881f368d03ac84e1dc09031ebd7b2c6" checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
@ -559,9 +559,9 @@ dependencies = [
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.1" version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
@ -580,13 +580,13 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.125" version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]] [[package]]
name = "libreddit" name = "libreddit"
version = "0.22.7" version = "0.22.8"
dependencies = [ dependencies = [
"askama", "askama",
"async-recursion", "async-recursion",
@ -702,9 +702,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.10.0" version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" checksum = "7b10983b38c53aebdf33f542c6275b0f58a238129d00c4ae0e6fb59738d783ca"
[[package]] [[package]]
name = "opaque-debug" name = "opaque-debug"
@ -773,11 +773,11 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.38" version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9027b48e9d4c9175fa2218adf3557f91c1137021739951d4932f5f8268ac48aa" checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
dependencies = [ dependencies = [
"unicode-xid", "unicode-ident",
] ]
[[package]] [[package]]
@ -800,9 +800,9 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.5.5" version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -811,9 +811,9 @@ dependencies = [
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.25" version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
[[package]] [[package]]
name = "ring" name = "ring"
@ -872,9 +872,9 @@ dependencies = [
[[package]] [[package]]
name = "rustls" name = "rustls"
version = "0.20.5" version = "0.20.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a024a432ae760ab3bff924ad91ce1cfa52cb57ed16e1ef32d0d249cfee1a6c13" checksum = "5aab8ee6c7097ed6057f43c187a62418d0c05a4bd5f18b3571db50ee0f9ce033"
dependencies = [ dependencies = [
"log", "log",
"ring", "ring",
@ -905,9 +905,9 @@ dependencies = [
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.9" version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
[[package]] [[package]]
name = "same-file" name = "same-file"
@ -920,12 +920,12 @@ dependencies = [
[[package]] [[package]]
name = "schannel" name = "schannel"
version = "0.1.19" version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"winapi", "windows-sys",
] ]
[[package]] [[package]]
@ -1056,13 +1056,13 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.94" version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a07e33e919ebcd69113d5be0e4d70c5707004ff45188910106854f38b960df4a" checksum = "fbaf6116ab8924f39d52792136fb74fd60a80194cf1b1c6ffa6453eef1c3f942"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"unicode-xid", "unicode-ident",
] ]
[[package]] [[package]]
@ -1245,6 +1245,12 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992"
[[package]]
name = "unicode-ident"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee"
[[package]] [[package]]
name = "unicode-normalization" name = "unicode-normalization"
version = "0.1.19" version = "0.1.19"
@ -1254,12 +1260,6 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-xid"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.7.1" version = "0.7.1"

View File

@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit" description = " Alternative private front-end to Reddit"
license = "AGPL-3.0" license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit" repository = "https://github.com/spikecodes/libreddit"
version = "0.22.7" version = "0.22.8"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"] authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2021" edition = "2021"
@ -12,7 +12,7 @@ askama = { version = "0.11.1", default-features = false }
async-recursion = "1.0.0" async-recursion = "1.0.0"
cached = "0.34.0" cached = "0.34.0"
clap = { version = "3.1.18", default-features = false, features = ["std"] } 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"] } serde = { version = "1.0.137", features = ["derive"] }
cookie = "0.16.0" cookie = "0.16.0"
futures-lite = "1.12.0" futures-lite = "1.12.0"
@ -23,4 +23,4 @@ serde_json = "1.0.81"
tokio = { version = "1.18.2", features = ["full"] } tokio = { version = "1.18.2", features = ["full"] }
time = "0.3.9" time = "0.3.9"
url = "2.2.2" url = "2.2.2"
rust-embed="6.4.0" rust-embed = "6.4.0"

View File

@ -11,7 +11,7 @@ mod user;
mod utils; mod utils;
// Import Crates // Import Crates
use clap::{Command, Arg}; use clap::{Arg, Command};
use futures_lite::FutureExt; use futures_lite::FutureExt;
use hyper::{header::HeaderValue, Body, Request, Response}; use hyper::{header::HeaderValue, Body, Request, Response};
@ -88,7 +88,7 @@ async fn resource(body: &str, content_type: &str, cache: bool) -> Result<Respons
async fn style() -> Result<Response<Body>, String> { async fn style() -> Result<Response<Body>, String> {
let mut res = include_str!("../static/style.css").to_string(); let mut res = include_str!("../static/style.css").to_string();
for file in ThemeAssets::iter() { for file in ThemeAssets::iter() {
res.push_str("\n"); res.push('\n');
let theme = ThemeAssets::get(file.as_ref()).unwrap(); let theme = ThemeAssets::get(file.as_ref()).unwrap();
res.push_str(std::str::from_utf8(theme.data.as_ref()).unwrap()); res.push_str(std::str::from_utf8(theme.data.as_ref()).unwrap());
} }

View File

@ -100,7 +100,10 @@ async fn parse_post(json: &serde_json::Value) -> Post {
let permalink = val(post, "permalink"); let permalink = val(post, "permalink");
let body = if val(post, "removed_by_category") == "moderator" { let body = if val(post, "removed_by_category") == "moderator" {
format!("<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}\">view removed post</a></p></div>", permalink) format!(
"<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}\">view removed post</a></p></div>",
permalink
)
} else { } else {
rewrite_urls(&val(post, "selftext_html")) 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 highlighted = id == highlighted_comment;
let body = if val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]" { let body = if val(&comment, "author") == "[deleted]" && val(&comment, "body") == "[removed]" {
format!("<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}{}\">view removed comment</a></p></div>", post_link, id) format!(
"<div class=\"md\"><p>[removed] — <a href=\"https://www.reveddit.com{}{}\">view removed comment</a></p></div>",
post_link, id
)
} else { } else {
rewrite_urls(&val(&comment, "body_html")) rewrite_urls(&val(&comment, "body_html"))
}; };

View File

@ -97,7 +97,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
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());
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str())); 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); let filters = get_filters(&req);
// If all requested subs are filtered, we don't need to fetch posts. // If all requested subs are filtered, we don't need to fetch posts.

View File

@ -5,7 +5,7 @@ use crate::server::RequestExt;
use crate::utils::{error, filter_posts, format_url, get_filters, param, template, Post, Preferences, User}; use crate::utils::{error, filter_posts, format_url, get_filters, param, template, Post, Preferences, User};
use askama::Template; use askama::Template;
use hyper::{Body, Request, Response}; use hyper::{Body, Request, Response};
use time::{OffsetDateTime, macros::format_description}; use time::{macros::format_description, OffsetDateTime};
// STRUCTS // STRUCTS
#[derive(Template)] #[derive(Template)]

View File

@ -6,12 +6,12 @@ use askama::Template;
use cookie::Cookie; use cookie::Cookie;
use hyper::{Body, Request, Response}; use hyper::{Body, Request, Response};
use regex::Regex; use regex::Regex;
use rust_embed::RustEmbed;
use serde_json::Value; use serde_json::Value;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::str::FromStr; use std::str::FromStr;
use time::{Duration, OffsetDateTime, macros::format_description}; use time::{macros::format_description, Duration, OffsetDateTime};
use url::Url; use url::Url;
use rust_embed::RustEmbed;
// Post flair with content, background color and foreground color // Post flair with content, background color and foreground color
pub struct Flair { pub struct Flair {
@ -218,24 +218,19 @@ pub struct Post {
impl Post { impl Post {
// Fetch posts of a user or subreddit and return a vector of posts and the "after" value // 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<Self>, String), String> { pub async fn fetch(path: &str, quarantine: bool) -> Result<(Vec<Self>, String), String> {
let res;
let post_list;
// Send a request to the url // 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 // If success, receive JSON in response
Ok(response) => { Ok(response) => response,
res = response;
}
// If the Reddit API returns an error, exit this function // If the Reddit API returns an error, exit this function
Err(msg) => return Err(msg), Err(msg) => return Err(msg),
} };
// Fetch the list of posts from the JSON response // Fetch the list of posts from the JSON response
match res["data"]["children"].as_array() { let post_list = match res["data"]["children"].as_array() {
Some(list) => post_list = list, Some(list) => list,
None => return Err("No posts found".to_string()), None => return Err("No posts found".to_string()),
} };
let mut posts: Vec<Self> = Vec::new(); let mut posts: Vec<Self> = Vec::new();
@ -622,12 +617,11 @@ pub fn format_url(url: &str) -> String {
// Rewrite Reddit links to Libreddit in body of text // Rewrite Reddit links to Libreddit in body of text
pub fn rewrite_urls(input_text: &str) -> String { pub fn rewrite_urls(input_text: &str) -> String {
let text1 = Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#)
let text1 = .map_or(String::new(), |re| re.replace_all(input_text, r#"href="/"#).to_string())
Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#) // Remove (html-encoded) "\" from URLs.
.map_or(String::new(), |re| re.replace_all(input_text, r#"href="/"#).to_string()) .replace("%5C", "")
// Remove (html-encoded) "\" from URLs. .replace('\\', "");
.replace("%5C", "").replace(r"\", "");
// Rewrite external media previews to Libreddit // Rewrite external media previews to Libreddit
Regex::new(r"https://external-preview\.redd\.it(.*)[^?]").map_or(String::new(), |re| { 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()) 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 // val() function used to parse JSON from Reddit APIs
@ -742,7 +741,8 @@ mod tests {
#[test] #[test]
fn rewrite_urls_removes_backslashes() { fn rewrite_urls_removes_backslashes() {
let comment_body_html = r#"<a href=\"https://www.reddit.com/r/linux%5C_gaming/comments/x/just%5C_a%5C_test%5C/\">https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/</a>"#; let comment_body_html =
r#"<a href=\"https://www.reddit.com/r/linux%5C_gaming/comments/x/just%5C_a%5C_test%5C/\">https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/</a>"#;
assert_eq!( assert_eq!(
rewrite_urls(comment_body_html), rewrite_urls(comment_body_html),
r#"<a href="https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/">https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/</a>"# r#"<a href="https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/">https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/</a>"#