Rewrite Reddit post links to Libreddit equivalents

This commit is contained in:
spikecodes 2021-12-26 21:18:20 -08:00
parent 150ebe38f3
commit 7aeabfc4bc
No known key found for this signature in database
GPG key ID: 004CECFF9B463BCB
10 changed files with 112 additions and 175 deletions

View file

@ -91,7 +91,7 @@ fn request(url: String, quarantine: bool) -> Boxed<Result<Response<Body>, String
.get("Location")
.map(|val| {
let new_url = val.to_str().unwrap_or_default();
format!("{}{}raw_json=1", new_url, if new_url.contains("?") { "&" } else { "?" })
format!("{}{}raw_json=1", new_url, if new_url.contains('?') { "&" } else { "?" })
})
.unwrap_or_default()
.to_string(),

View file

@ -1,13 +1,6 @@
// Global specifiers
#![forbid(unsafe_code)]
#![warn(clippy::pedantic, clippy::all)]
#![allow(
clippy::needless_pass_by_value,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::manual_find_map,
clippy::unused_async
)]
#![allow(clippy::cmp_owned)]
// Reference local files
mod post;

View file

@ -78,7 +78,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
// If all requested subs are filtered, we don't need to fetch posts.
if sub.split("+").all(|s| filters.contains(s)) {
if sub.split('+').all(|s| filters.contains(s)) {
template(SearchTemplate {
posts: Vec::new(),
subreddits,

View file

@ -101,7 +101,7 @@ pub async fn community(req: Request<Body>) -> Result<Response<Body>, String> {
let filters = get_filters(&req);
// If all requested subs are filtered, we don't need to fetch posts.
if sub_name.split("+").all(|s| filters.contains(s)) {
if sub_name.split('+').all(|s| filters.contains(s)) {
template(SubredditTemplate {
sub,
posts: Vec::new(),

View file

@ -21,6 +21,7 @@ pub struct Flair {
}
// Part of flair, either emoji or text
#[derive(Clone)]
pub struct FlairPart {
pub flair_part_type: String,
pub value: String,
@ -154,18 +155,12 @@ impl Media {
let source = &data["preview"]["images"][0]["source"];
let url = if post_type == "self" || post_type == "link" {
url_val.as_str().unwrap_or_default().to_string()
} else {
format_url(url_val.as_str().unwrap_or_default())
};
let alt_url = alt_url_val.map_or(String::new(), |val| format_url(val.as_str().unwrap_or_default()));
(
post_type.to_string(),
Self {
url,
url: format_url(url_val.as_str().unwrap_or_default()),
alt_url,
width: source["width"].as_i64().unwrap_or_default(),
height: source["height"].as_i64().unwrap_or_default(),
@ -265,13 +260,13 @@ impl Post {
let title = esc!(post, "title");
// Determine the type of media along with the media URL
let (post_type, media, gallery) = Media::parse(&data).await;
let (post_type, media, gallery) = Media::parse(data).await;
let awards = Awards::parse(&data["all_awardings"]);
// selftext_html is set for text posts when browsing.
let mut body = rewrite_urls(&val(post, "selftext_html"));
if body == "" {
body = rewrite_urls(&val(post, "body_html"))
if body.is_empty() {
body = rewrite_urls(&val(post, "body_html"));
}
posts.push(Self {
@ -498,7 +493,7 @@ impl Preferences {
/// Gets a `HashSet` of filters from the cookie in the given `Request`.
pub fn get_filters(req: &Request<Body>) -> HashSet<String> {
setting(&req, "filters").split('+').map(String::from).filter(|s| !s.is_empty()).collect::<HashSet<String>>()
setting(req, "filters").split('+').map(String::from).filter(|s| !s.is_empty()).collect::<HashSet<String>>()
}
/// Filters a `Vec<Post>` by the given `HashSet` of filters (each filter being a subreddit name or a user name). If a
@ -600,6 +595,7 @@ pub fn format_url(url: &str) -> String {
}
match domain {
"www.reddit.com" => capture(r"https://www\.reddit\.com/(.*)", "/", 1),
"v.redd.it" => chain!(
capture(r"https://v\.redd\.it/(.*)/DASH_([0-9]{2,4}(\.mp4|$|\?source=fallback))", "/vid/", 2),
capture(r"https://v\.redd\.it/(.+)/(HLSPlaylist\.m3u8.*)$", "/hls/", 2)