From 4f6a14739b0cdea5057a983d1920e3b8b60edfe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89li=20Marshal?= <835958+EMarshal@users.noreply.github.com> Date: Sun, 7 Apr 2024 14:43:39 -0600 Subject: [PATCH 01/77] Update hide HLS env var to match documentation --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 21a3adc..2ce360c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -125,7 +125,7 @@ impl Config { default_show_nsfw: parse("REDLIB_DEFAULT_SHOW_NSFW"), default_blur_nsfw: parse("REDLIB_DEFAULT_BLUR_NSFW"), default_use_hls: parse("REDLIB_DEFAULT_USE_HLS"), - default_hide_hls_notification: parse("REDLIB_DEFAULT_HIDE_HLS"), + default_hide_hls_notification: parse("REDLIB_DEFAULT_HIDE_HLS_NOTIFICATION"), default_hide_awards: parse("REDLIB_DEFAULT_HIDE_AWARDS"), default_hide_score: parse("REDLIB_DEFAULT_HIDE_SCORE"), default_subscriptions: parse("REDLIB_DEFAULT_SUBSCRIPTIONS"), From 9bd540d659d3850960fd431cad66ba288a28f18b Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 7 Apr 2024 17:23:24 -0400 Subject: [PATCH 02/77] Stop post footer text from disappear at exactly 480px --- static/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/style.css b/static/style.css index 5e8236d..bd168bb 100644 --- a/static/style.css +++ b/static/style.css @@ -1098,7 +1098,7 @@ a.search_subreddit:hover { display: auto; } -@media screen and (min-width: 480px) { +@media screen and (min-width: 481px) { #post_links > li.mobile_item { display: none; } From 6d83b07aaa9db110b5e1ff610e3cfa1d0e2d257e Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Tue, 9 Apr 2024 18:33:13 -0400 Subject: [PATCH 03/77] Update embedding Reddit preview links to include captions where applicable --- src/utils.rs | 26 +++++++++++++++++++++----- static/style.css | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 63e679d..e0ce24c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -901,14 +901,30 @@ pub fn rewrite_urls(input_text: &str) -> String { let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + let mut image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); + if !image_text.is_empty() { + image_text.remove(0); + image_text.pop(); + image_text.pop(); + image_text.pop(); + image_text.pop(); + } - let image_to_replace = format!(">", ">"); - let image_replacement = format!(""); + let image_to_replace = format!("

"); + + image_text = image_text.replace("\\"", "\""); + + let mut _image_replacement = String::new(); + + if REDDIT_PREVIEW_REGEX.find(&image_text).is_none() { + _image_replacement = format!("
{image_text}
"); + } else { + _image_replacement = format!("
"); + } text1 = REDDIT_PREVIEW_REGEX .replace(&text1, formatted_url) - .replace(&image_to_replace, &image_replacement) + .replace(&image_to_replace, &_image_replacement) .to_string() } } @@ -1169,6 +1185,6 @@ fn test_rewriting_image_links() {

https://preview.redd.it/bdfdxkjj2xo31.png?width=2560&format=png&auto=webp&s=d0fa420ece27605e882e89cb4711d75d774322ac

caption 1

caption 2

"#; - let output = r#"

"#; + let output = r#"
caption 1
caption 2
"#; assert_eq!(rewrite_urls(input), output); } diff --git a/static/style.css b/static/style.css index a85ab75..6c6a546 100644 --- a/static/style.css +++ b/static/style.css @@ -187,6 +187,15 @@ nav #redlib { vertical-align: -2px; } +figure { + margin: 0; +} + +figcaption { + margin-top: 5px; + text-align: center; +} + #settings_link { opacity: 0.8; margin-left: 10px; @@ -979,10 +988,6 @@ a.search_subreddit:hover { vertical-align: bottom; } -.gallery figcaption { - margin-top: 5px; -} - .gallery .outbound_url { color: var(--accent); text-overflow: ellipsis; @@ -1010,6 +1015,9 @@ a.search_subreddit:hover { .post_body img { max-width: 100%; + display: block; + margin-left: auto; + margin-right: auto; } .post_poll { From 2c8f5a7ac16abf88d46132a43777eba7310c4344 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Tue, 9 Apr 2024 18:51:32 -0400 Subject: [PATCH 04/77] Make figure margins only apply to comments to bring embedded previews more in line with gallery posts --- static/style.css | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/static/style.css b/static/style.css index 6c6a546..a533dd6 100644 --- a/static/style.css +++ b/static/style.css @@ -187,10 +187,6 @@ nav #redlib { vertical-align: -2px; } -figure { - margin: 0; -} - figcaption { margin-top: 5px; text-align: center; @@ -1195,6 +1191,10 @@ a.search_subreddit:hover { } } +.comment figure { + margin: 0; +} + .comment_left, .comment_right { display: flex; flex-direction: column; From e581f432ddb00febbc4c0e396614a98dc28de396 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Wed, 10 Apr 2024 10:47:24 -0400 Subject: [PATCH 05/77] Use substring instead of .remove and .pop, change image_text to image_caption to better reflect its usage, only replace quotes in image_caption when needed, and add comments for what some of the code does --- src/utils.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index e0ce24c..6f70fa5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -901,23 +901,26 @@ pub fn rewrite_urls(input_text: &str) -> String { let formatted_url = format_url(REDDIT_PREVIEW_REGEX.find(&text1).map(|x| x.as_str()).unwrap_or_default()); let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let mut image_text = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - if !image_text.is_empty() { - image_text.remove(0); - image_text.pop(); - image_text.pop(); - image_text.pop(); - image_text.pop(); - } + let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - let image_to_replace = format!("

"); + /* Remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. + This makes it possible to enclose it in a
later on without having stray HTML breaking it */ + image_caption = image_caption[1..image_caption.len() - 4].to_string(); - image_text = image_text.replace("\\"", "\""); + // image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them + let image_to_replace = format!("

"); + // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); - if REDDIT_PREVIEW_REGEX.find(&image_text).is_none() { - _image_replacement = format!("
{image_text}
"); + /* We don't want to show a caption that's just the image's link, so we check if we find a Reddit preview link within the image's caption. + If we don't find one we must have actual text, so we include a
block that contains it. + Otherwise we don't include the
block as we don't need it. */ + if REDDIT_PREVIEW_REGEX.find(&image_caption).is_none() { + // Without this " would show as \" instead. "\"" is how the quotes are formatted within image_text beforehand + image_caption = image_caption.replace("\\"", "\""); + + _image_replacement = format!("
{image_caption}
"); } else { _image_replacement = format!("
"); } From 3f863c8991648b60b0a650bedf4c91dc9a7576ee Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 14 Apr 2024 17:26:43 -0400 Subject: [PATCH 06/77] Prevent panic if image_caption is empty, don't replace

's in case text is inside them with the image, update test to reflect change in image replacing --- src/utils.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 6f70fa5..d65478a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -903,12 +903,16 @@ pub fn rewrite_urls(input_text: &str) -> String { let image_url = REDLIB_PREVIEW_LINK_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); let mut image_caption = REDLIB_PREVIEW_TEXT_REGEX.find(&formatted_url).map_or("", |m| m.as_str()).to_string(); - /* Remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. + /* As long as image_caption isn't empty remove first and last four characters of image_text to leave us with just the text in the caption without any HTML. This makes it possible to enclose it in a

later on without having stray HTML breaking it */ - image_caption = image_caption[1..image_caption.len() - 4].to_string(); + if !image_caption.is_empty() { + image_caption = image_caption[1..image_caption.len() - 4].to_string(); + } // image_url contains > at the end of it, and right above this we remove image_text's front >, leaving us with just a single > between them - let image_to_replace = format!("

"); + let image_to_replace = format!(""); + + //println!("{}", image_to_replace); // _image_replacement needs to be in scope for the replacement at the bottom of the loop let mut _image_replacement = String::new(); @@ -928,6 +932,7 @@ pub fn rewrite_urls(input_text: &str) -> String { text1 = REDDIT_PREVIEW_REGEX .replace(&text1, formatted_url) .replace(&image_to_replace, &_image_replacement) + .replace("

", "") .to_string() } } @@ -1183,11 +1188,7 @@ async fn test_fetching_ws() { #[test] fn test_rewriting_image_links() { - let input = r#"

https://preview.redd.it/zq21ggkj2xo31.png?width=2560&format=png&auto=webp&s=539d8050628ec1190cac26468fe99cc66b6071ab

-

https://preview.redd.it/vty9ocij2xo31.png?width=2560&format=png&auto=webp&s=fc7c7ef993a5e9ef656d5f5d9cf8290a0a1df877

-

https://preview.redd.it/bdfdxkjj2xo31.png?width=2560&format=png&auto=webp&s=d0fa420ece27605e882e89cb4711d75d774322ac

-

caption 1

-

caption 2

"#; - let output = r#"
caption 1
caption 2
"#; + let input = r#"

caption 1

"#; + let output = r#"

caption 1

"#; assert_eq!(rewrite_urls(input), output); } From 6484ebf8976f99002ddb1c2202046f921a31f1b3 Mon Sep 17 00:00:00 2001 From: Butter Cat Date: Sun, 14 Apr 2024 17:32:10 -0400 Subject: [PATCH 07/77] Fix failing check --- src/utils.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils.rs b/src/utils.rs index d65478a..d1b84a3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1188,7 +1188,8 @@ async fn test_fetching_ws() { #[test] fn test_rewriting_image_links() { - let input = r#"

caption 1

"#; + let input = + r#"

caption 1

"#; let output = r#"

caption 1

"#; assert_eq!(rewrite_urls(input), output); } From 565b50646f6db84e243da43adb5b86c266b4e616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Karsl=C4=B1?= Date: Thu, 25 Apr 2024 14:01:06 +0300 Subject: [PATCH 08/77] added geo_filter query param to /r/popular endpoint --- src/subreddit.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/subreddit.rs b/src/subreddit.rs index 569f84c..4df81b4 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -7,6 +7,8 @@ use askama::Template; use cookie::Cookie; use hyper::{Body, Request, Response}; +use once_cell::sync::Lazy; +use regex::Regex; use time::{Duration, OffsetDateTime}; // STRUCTS @@ -50,10 +52,13 @@ struct WallTemplate { url: String, } +static GEO_FILTER_MATCH: Lazy = Lazy::new(|| Regex::new(r"geo_filter=(?\w+)").unwrap()); + // SERVICES pub async fn community(req: Request) -> Result, String> { // Build Reddit API path let root = req.uri().path() == "/"; + let query = req.uri().query().unwrap_or_default().to_string(); let subscribed = setting(&req, "subscriptions"); let front_page = setting(&req, "front_page"); let post_sort = req.cookie("post_sort").map_or_else(|| "hot".to_string(), |c| c.value().to_string()); @@ -107,7 +112,11 @@ pub async fn community(req: Request) -> Result, String> { let mut params = String::from("&raw_json=1"); if sub_name == "popular" { - params.push_str("&geo_filter=GLOBAL"); + let geo_filter = match GEO_FILTER_MATCH.captures(&query) { + Some(geo_filter) => geo_filter["region"].to_string(), + None => "GLOBAL".to_owned(), + }; + params.push_str(&format!("&geo_filter={geo_filter}")); } let path = format!("/r/{sub_name}/{sort}.json?{}{params}", req.uri().query().unwrap_or_default()); From b6f5831d10deac25bb8f263d33f412923154920e Mon Sep 17 00:00:00 2001 From: Ales Lerch <13370338+axeII@users.noreply.github.com> Date: Mon, 13 May 2024 23:49:59 +0200 Subject: [PATCH 09/77] feat: adds hide summary sidebar option --- .env.example | 2 ++ Cargo.lock | 2 +- src/config.rs | 6 ++++++ src/settings.rs | 3 ++- src/utils.rs | 2 ++ templates/settings.html | 13 +++++++++---- templates/subreddit.html | 2 +- 7 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 84f2ed2..b74e45c 100644 --- a/.env.example +++ b/.env.example @@ -38,6 +38,8 @@ REDLIB_DEFAULT_AUTOPLAY_VIDEOS=off REDLIB_DEFAULT_SUBSCRIPTIONS= # Hide awards by default REDLIB_DEFAULT_HIDE_AWARDS=off +# Hide sidebar and summary +REDLIB_DEFAULT_HIDE_SIDEBAR_AND_SUMMARY=off # Disable the confirmation before visiting Reddit REDLIB_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION=off # Hide score by default diff --git a/Cargo.lock b/Cargo.lock index 3b357a1..5f8bd94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1047,7 +1047,7 @@ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" [[package]] name = "redlib" -version = "0.31.0" +version = "0.31.2" dependencies = [ "askama", "base64", diff --git a/src/config.rs b/src/config.rs index 21a3adc..418d2f5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -68,6 +68,10 @@ pub struct Config { #[serde(alias = "LIBREDDIT_DEFAULT_HIDE_AWARDS")] pub(crate) default_hide_awards: Option, + #[serde(rename = "REDLIB_DEFAULT_HIDE_SIDEBAR_AND_SUMMARY")] + #[serde(alias = "LIBREDDIT_DEFAULT_HIDE_SIDEBAR_AND_SUMMARY")] + pub(crate) default_hide_sidebar_and_summary: Option, + #[serde(rename = "REDLIB_DEFAULT_HIDE_SCORE")] #[serde(alias = "LIBREDDIT_DEFAULT_HIDE_SCORE")] pub(crate) default_hide_score: Option, @@ -127,6 +131,7 @@ impl Config { default_use_hls: parse("REDLIB_DEFAULT_USE_HLS"), default_hide_hls_notification: parse("REDLIB_DEFAULT_HIDE_HLS"), default_hide_awards: parse("REDLIB_DEFAULT_HIDE_AWARDS"), + default_hide_sidebar_and_summary: parse("REDLIB_DEFAULT_HIDE_SIDEBAR_AND_SUMMARY"), default_hide_score: parse("REDLIB_DEFAULT_HIDE_SCORE"), default_subscriptions: parse("REDLIB_DEFAULT_SUBSCRIPTIONS"), default_disable_visit_reddit_confirmation: parse("REDLIB_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION"), @@ -151,6 +156,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option { "REDLIB_DEFAULT_HIDE_HLS_NOTIFICATION" => config.default_hide_hls_notification.clone(), "REDLIB_DEFAULT_WIDE" => config.default_wide.clone(), "REDLIB_DEFAULT_HIDE_AWARDS" => config.default_hide_awards.clone(), + "REDLIB_DEFAULT_HIDE_SIDEBAR_AND_SUMMARY" => config.default_hide_awards.clone(), "REDLIB_DEFAULT_HIDE_SCORE" => config.default_hide_score.clone(), "REDLIB_DEFAULT_SUBSCRIPTIONS" => config.default_subscriptions.clone(), "REDLIB_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION" => config.default_disable_visit_reddit_confirmation.clone(), diff --git a/src/settings.rs b/src/settings.rs index 6edb059..6964675 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -19,7 +19,7 @@ struct SettingsTemplate { // CONSTANTS -const PREFS: [&str; 15] = [ +const PREFS: [&str; 16] = [ "theme", "front_page", "layout", @@ -31,6 +31,7 @@ const PREFS: [&str; 15] = [ "use_hls", "hide_hls_notification", "autoplay_videos", + "hide_sidebar_and_summary", "fixed_navbar", "hide_awards", "hide_score", diff --git a/src/utils.rs b/src/utils.rs index 63e679d..cd4c4ae 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -577,6 +577,7 @@ pub struct Preferences { pub show_nsfw: String, pub blur_nsfw: String, pub hide_hls_notification: String, + pub hide_sidebar_and_summary: String, pub use_hls: String, pub autoplay_videos: String, pub fixed_navbar: String, @@ -611,6 +612,7 @@ impl Preferences { layout: setting(req, "layout"), wide: setting(req, "wide"), show_nsfw: setting(req, "show_nsfw"), + hide_sidebar_and_summary: setting(req, "hide_sidebar_and_summary"), blur_nsfw: setting(req, "blur_nsfw"), use_hls: setting(req, "use_hls"), hide_hls_notification: setting(req, "hide_hls_notification"), diff --git a/templates/settings.html b/templates/settings.html index e57d250..496b9b1 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -71,11 +71,16 @@ -
+
- - -
+ + +
+
+ + + +
diff --git a/templates/subreddit.html b/templates/subreddit.html index 0c27ed3..66afb87 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -82,7 +82,7 @@
{% endif %} - {% if is_filtered || (!sub.name.is_empty() && sub.name != "all" && sub.name != "popular" && !sub.name.contains("+")) %} + {% if is_filtered || (!sub.name.is_empty() && sub.name != "all" && sub.name != "popular" && !sub.name.contains("+")) && prefs.hide_sidebar_and_summary != "on" %}