diff --git a/Cargo.lock b/Cargo.lock index 98725c4..900d853 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1013,6 +1013,7 @@ dependencies = [ "async-recursion", "base64 0.13.0", "chrono", + "regex", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index e1eb619..4a02b7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,5 @@ serde = "1.0.117" serde_json = "1.0" chrono = "0.4.19" async-recursion = "0.3.1" -url = "2.2.0" \ No newline at end of file +url = "2.2.0" +regex = "1" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6a3553b..b76d5c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,8 @@ async fn main() -> std::io::Result<()> { .route("/u/{username}/", web::get().to(user::profile)) .route("/user/{username}/", web::get().to(user::profile)) // WIKI SERVICES + .route("/wiki/", web::get().to(subreddit::wiki)) + .route("/wiki/{page}/", web::get().to(subreddit::wiki)) .route("/r/{sub}/wiki/", web::get().to(subreddit::wiki)) .route("/r/{sub}/wiki/{page}/", web::get().to(subreddit::wiki)) // SUBREDDIT SERVICES diff --git a/src/post.rs b/src/post.rs index c33359e..ed12e3b 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, format_num, format_url, param, request, val, Comment, Flags, Flair, Post}; +use crate::utils::{error, format_num, format_url, param, request, rewrite_url, val, Comment, Flags, Flair, Post}; use actix_web::{HttpRequest, HttpResponse, Result}; use async_recursion::async_recursion; @@ -79,7 +79,7 @@ async fn parse_post(json: &serde_json::Value) -> Result { let post = Post { title: val(post_data, "title"), community: val(post_data, "subreddit"), - body: val(post_data, "selftext_html"), + body: rewrite_url(&val(post_data, "selftext_html")).await, author: val(post_data, "author"), author_flair: Flair( val(post_data, "author_flair_text"), @@ -125,7 +125,7 @@ async fn parse_comments(json: &serde_json::Value) -> Result, &'stat } let score = comment["data"]["score"].as_i64().unwrap_or(0); - let body = val(comment, "body_html"); + let body = rewrite_url(&val(comment, "body_html")).await; let replies: Vec = if comment["data"]["replies"].is_object() { parse_comments(&comment["data"]["replies"]).await.unwrap_or_default() diff --git a/src/subreddit.rs b/src/subreddit.rs index 7c33fd4..7cf00dc 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -1,5 +1,5 @@ // CRATES -use crate::utils::{error, fetch_posts, format_num, format_url, param, request, val, Post, Subreddit}; +use crate::utils::{error, fetch_posts, format_num, format_url, param, request, rewrite_url, val, Post, Subreddit}; use actix_web::{HttpRequest, HttpResponse, Result}; use askama::Template; @@ -18,7 +18,7 @@ struct SubredditTemplate { struct WikiTemplate { sub: String, wiki: String, - page: String + page: String, } // SERVICES @@ -50,7 +50,7 @@ pub async fn page(req: HttpRequest) -> Result { } pub async fn wiki(req: HttpRequest) -> Result { - let sub = req.match_info().get("sub").unwrap(); + let sub = req.match_info().get("sub").unwrap_or("reddit.com"); let page = req.match_info().get("page").unwrap_or("index"); let path: String = format!("r/{}/wiki/{}.json?raw_json=1", sub, page); @@ -58,8 +58,8 @@ pub async fn wiki(req: HttpRequest) -> Result { Ok(res) => { let s = WikiTemplate { sub: sub.to_string(), - wiki: res["data"]["content_html"].as_str().unwrap().to_string(), - page: page.to_string() + wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap()).await, + page: page.to_string(), } .render() .unwrap(); @@ -90,7 +90,7 @@ async fn subreddit(sub: &str) -> Result { name: val(&res, "display_name"), title: val(&res, "title"), description: val(&res, "public_description"), - info: val(&res, "description_html").replace("\\", ""), + info: rewrite_url(&val(&res, "description_html").replace("\\", "")).await, icon: format_url(icon).await, members: format_num(members), active: format_num(active), diff --git a/src/utils.rs b/src/utils.rs index 0d6b18c..137804d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,6 +4,7 @@ use actix_web::{http::StatusCode, HttpResponse, Result}; use askama::Template; use chrono::{TimeZone, Utc}; +use regex::Regex; use serde_json::from_str; use url::Url; // use surf::{client, get, middleware::Redirect}; @@ -114,6 +115,12 @@ pub async fn format_url(url: String) -> String { return url.to_string(); } +// Rewrite Reddit links to Libreddit in body of text +pub async fn rewrite_url(text: &str) -> String { + let re = Regex::new(r#"href="(https://|http://|)(www.|)(reddit).(com)/"#).unwrap(); + re.replace_all(text, r#"href="/"#).to_string() +} + // Append `m` and `k` for millions and thousands respectively pub fn format_num(num: i64) -> String { if num > 1000000 { @@ -175,7 +182,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec