-
-
Notifications
You must be signed in to change notification settings - Fork 176
Clean links - remove superfluous tracking elements from URLs #460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e265867
3b1445c
5065fb6
edd1f0e
3057fa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use crate::config::{self, get_setting}; | |
// CRATES | ||
// | ||
use crate::{client::json, server::RequestExt}; | ||
use clearurls::UrlCleaner; | ||
use cookie::Cookie; | ||
use hyper::{Body, Request, Response}; | ||
use libflate::deflate::{Decoder, Encoder}; | ||
|
@@ -23,6 +24,7 @@ use std::env; | |
use std::io::{Read, Write}; | ||
use std::str::FromStr; | ||
use std::string::ToString; | ||
use std::sync::Mutex; | ||
use time::{macros::format_description, Duration, OffsetDateTime}; | ||
use url::Url; | ||
|
||
|
@@ -670,6 +672,8 @@ pub struct Preferences { | |
pub hide_score: String, | ||
#[revision(start = 1)] | ||
pub remove_default_feeds: String, | ||
#[revision(start = 1)] | ||
pub clean_urls: String, | ||
Comment on lines
+675
to
+676
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please review |
||
} | ||
|
||
fn serialize_vec_with_plus<S>(vec: &[String], serializer: S) -> Result<S::Ok, S::Error> | ||
|
@@ -728,6 +732,7 @@ impl Preferences { | |
hide_awards: setting(req, "hide_awards"), | ||
hide_score: setting(req, "hide_score"), | ||
remove_default_feeds: setting(req, "remove_default_feeds"), | ||
clean_urls: setting(req, "clean_urls"), | ||
} | ||
} | ||
|
||
|
@@ -1075,6 +1080,22 @@ pub fn format_url(url: &str) -> String { | |
} | ||
} | ||
|
||
// Remove tracking query params | ||
static URL_CLEANER: Lazy<Mutex<UrlCleaner>> = Lazy::new(|| Mutex::new(UrlCleaner::from_embedded_rules().expect("Failed to initialize UrlCleaner"))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we use a mutex? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
||
pub fn clean_url(url: String) -> String { | ||
let is_external_url = match Url::parse(url.as_str()) { | ||
Ok(parsed_url) => parsed_url.domain().is_some(), | ||
_ => false, | ||
}; | ||
let mut cleaned_url = url.clone(); | ||
if is_external_url { | ||
let cleaner = URL_CLEANER.lock().unwrap(); | ||
cleaned_url = cleaner.clear_single_url_str(cleaned_url.as_str()).expect("Unable to clean the URL.").as_ref().to_owned(); | ||
} | ||
cleaned_url | ||
} | ||
Comment on lines
+1086
to
+1097
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should handle the failed cases better - specifically (once you remove the mutex) the expect for the |
||
|
||
static REGEX_BULLET: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^- (.*)$").unwrap()); | ||
static REGEX_BULLET_CONSECUTIVE_LINES: Lazy<Regex> = Lazy::new(|| Regex::new(r"</ul>\n<ul>").unwrap()); | ||
|
||
|
@@ -1423,13 +1444,11 @@ pub async fn nsfw_landing(req: Request<Body>, req_url: String) -> Result<Respons | |
pub fn url_path_basename(path: &str) -> String { | ||
let url_result = Url::parse(format!("https://libredd.it/{path}").as_str()); | ||
|
||
if url_result.is_err() { | ||
path.to_string() | ||
} else { | ||
let mut url = url_result.unwrap(); | ||
if let Ok(mut url) = url_result { | ||
url.path_segments_mut().unwrap().pop_if_empty(); | ||
|
||
url.path_segments().unwrap().next_back().unwrap().to_string() | ||
} else { | ||
path.to_string() | ||
} | ||
} | ||
|
||
|
@@ -1541,10 +1560,11 @@ mod tests { | |
hide_awards: "off".to_owned(), | ||
hide_score: "off".to_owned(), | ||
remove_default_feeds: "off".to_owned(), | ||
clean_urls: "off".to_owned(), | ||
}; | ||
let urlencoded = serde_urlencoded::to_string(prefs).expect("Failed to serialize Prefs"); | ||
|
||
assert_eq!(urlencoded, "theme=laserwave&front_page=default&layout=compact&wide=on&blur_spoiler=on&show_nsfw=off&blur_nsfw=on&hide_hls_notification=off&video_quality=best&hide_sidebar_and_summary=off&use_hls=on&autoplay_videos=on&fixed_navbar=on&disable_visit_reddit_confirmation=on&comment_sort=confidence&post_sort=top&subscriptions=memes%2Bmildlyinteresting&filters=&hide_awards=off&hide_score=off&remove_default_feeds=off"); | ||
assert_eq!(urlencoded, "theme=laserwave&front_page=default&layout=compact&wide=on&blur_spoiler=on&show_nsfw=off&blur_nsfw=on&hide_hls_notification=off&video_quality=best&hide_sidebar_and_summary=off&use_hls=on&autoplay_videos=on&fixed_navbar=on&disable_visit_reddit_confirmation=on&comment_sort=confidence&post_sort=top&subscriptions=memes%2Bmildlyinteresting&filters=&hide_awards=off&hide_score=off&remove_default_feeds=off&clean_urls=off"); | ||
} | ||
} | ||
|
||
|
@@ -1631,7 +1651,7 @@ fn test_rewriting_bullet_list() { | |
How`s your monitor by the way? Any IPS bleed whatsoever? I either got lucky or the panel is pretty good, 0 bleed for me, just the usual IPS glow. How about the pixels? I see the pixels even at one meter away, especially on Microsoft Edge's icon for example, the blue background is just blocky, don't know why.</p> | ||
</div>"#; | ||
let output = r#"<div class="md"><p>Hi, I've bought this very same monitor and found no calibration whatsoever. I have an ICC profile that has been set up since I've installed its driver from the LG website and it works ok. I also used <a href="http://www.lagom.nl/lcd-test/">http://www.lagom.nl/lcd-test/</a> to calibrate it. After some good tinkering I've found the following settings + the color profile from the driver gets me past all the tests perfectly: | ||
<ul><li>Brightness 50 (still have to settle on this one, it's personal preference, it controls the backlight, not the colors)</li><li>Contrast 70 (which for me was the default one)</li><li>Picture mode Custom</li><li>Super resolution + Off (it looks horrible anyway)</li><li>Sharpness 50 (default one I think)</li><li>Black level High (low messes up gray colors)</li><li>DFC Off </li><li>Response Time Middle (personal preference, <a href="https://www.blurbusters.com/">https://www.blurbusters.com/</a> show horrible overdrive with it on high)</li><li>Freesync doesn't matter</li><li>Black stabilizer 50</li><li>Gamma setting on 0 </li><li>Color Temp Medium</li></ul> | ||
<ul><li>Brightness 50 (still have to settle on this one, it's personal preference, it controls the backlight, not the colors)</li><li>Contrast 70 (which for me was the default one)</li><li>Picture mode Custom</li><li>Super resolution + Off (it looks horrible anyway)</li><li>Sharpness 50 (default one I think)</li><li>Black level High (low messes up gray colors)</li><li>DFC Off</li><li>Response Time Middle (personal preference, <a href="https://www.blurbusters.com/">https://www.blurbusters.com/</a> show horrible overdrive with it on high)</li><li>Freesync doesn't matter</li><li>Black stabilizer 50</li><li>Gamma setting on 0</li><li>Color Temp Medium</li></ul> | ||
How`s your monitor by the way? Any IPS bleed whatsoever? I either got lucky or the panel is pretty good, 0 bleed for me, just the usual IPS glow. How about the pixels? I see the pixels even at one meter away, especially on Microsoft Edge's icon for example, the blue background is just blocky, don't know why.</p> | ||
</div>"#; | ||
|
||
|
@@ -1654,9 +1674,9 @@ fn test_default_prefs_serialization_loop_bincode() { | |
} | ||
|
||
static KNOWN_GOOD_CONFIGS: &[&str] = &[ | ||
"ఴӅβØØҞÉဏႢձĬ༧ȒʯऌԔӵ୮༏", | ||
"ਧՊΥÀÃǎƱГ۸ඣമĖฤ႙ʟาúໜϾௐɥঀĜໃહཞઠѫҲɂఙ࿔DzઉƲӟӻĻฅΜδ໖ԜǗဖငƦơ৶Ą௩ԹʛใЛʃශаΏ", | ||
"ਧԩΥÀÃΊ౭൩ඔႠϼҭöҪƸռઇԾॐნɔາǒՍҰच௨ಖມŃЉŐདƦ๙ϩএఠȝഽйʮჯඒϰळՋ௮ສ৵ऎΦѧਹಧଟƙŃ३î༦ŌပղयƟแҜ།", | ||
"ਧӐΥºÃΦĴгౡୡϤҚԷŽဎՐΧΣೡຽဒ೨ʛĽତ๘Ӓǹভµɾ൦ॴцৱ௬చΣҭжҭȱȾཊజĊȔ௸७ƘȂј۰ȥėǨԯၻíႽਈႴ۹ଆ", | ||
"ਧҫടºÃǒɣυໃਣөŕǁజ८ௐɪDžઘႴ౨ඛႻຫǪၼդɍ৪Êѕ୶ʭѹŪҚຊೱѰງიŠСঌາඌĨğਜડ࿅ଠಲೱҋŇƞਭăʁझшȖǾཔ௧ந۞ສÚ", | ||
"ਧҫടºÃǒɿဧϯljഔค๖۞ԆНȦ൨ĭ྅ҤƍตཧႯƅशञঊମਇȕමзқଽijჰଐՋບӎՓஶཕ૭ଛกήऋĜɀಱӔԩझԩîဓŒԬũլಙટщೞຝ৪༎", | ||
Comment on lines
-1657
to
+1679
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should leave the old configs there, so that we can confirm backwards compatibility is preserved. |
||
]; | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can leave this unpatched (as well as :213-214) - I'd rather atomic PR's and fixed this in
main
- ignore the CI/CD checks