Merge pull request #2 from spikecodes/master

Merge upstream into code
This commit is contained in:
robrobinbin 2021-01-14 19:29:06 +01:00 committed by GitHub
commit 735f79d80b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 210 additions and 211 deletions

93
Cargo.lock generated
View File

@ -31,7 +31,7 @@ dependencies = [
"futures-util", "futures-util",
"http", "http",
"log", "log",
"rustls 0.18.1", "rustls",
"tokio-rustls", "tokio-rustls",
"trust-dns-proto", "trust-dns-proto",
"trust-dns-resolver", "trust-dns-resolver",
@ -193,10 +193,10 @@ dependencies = [
"actix-service", "actix-service",
"actix-utils", "actix-utils",
"futures-util", "futures-util",
"rustls 0.18.1", "rustls",
"tokio-rustls", "tokio-rustls",
"webpki", "webpki",
"webpki-roots 0.20.0", "webpki-roots",
] ]
[[package]] [[package]]
@ -249,7 +249,7 @@ dependencies = [
"mime", "mime",
"pin-project 1.0.4", "pin-project 1.0.4",
"regex", "regex",
"rustls 0.18.1", "rustls",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
@ -393,7 +393,7 @@ dependencies = [
"mime", "mime",
"percent-encoding", "percent-encoding",
"rand", "rand",
"rustls 0.18.1", "rustls",
"serde", "serde",
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
@ -529,12 +529,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chunked_transfer"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7477065d45a8fe57167bf3cf8bcd3729b54cfcb81cca49bda2d038ea89ae82ca"
[[package]] [[package]]
name = "const_fn" name = "const_fn"
version = "0.4.5" version = "0.4.5"
@ -678,9 +672,9 @@ checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]] [[package]]
name = "futures" name = "futures"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c70be434c505aee38639abccb918163b63158a4b4bb791b45b7023044bdc3c9c" checksum = "309f13e3f4be6d5917178c84db67c0b9a09177ac16d4f9a7313a767a68adaa77"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
@ -692,9 +686,9 @@ dependencies = [
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f01c61843314e95f96cc9245702248733a3a3d744e43e2e755e3c7af8348a0a9" checksum = "7a3b03bd32f6ec7885edeb99acd1e47e20e34fd4dfd3c6deed6fcac8a9d28f6a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-sink", "futures-sink",
@ -702,21 +696,21 @@ dependencies = [
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db8d3b0917ff63a2a96173133c02818fac4a746b0a57569d3baca9ec0e945e08" checksum = "ed8aeae2b6ab243ebabe6f54cd4cf53054d98883d5d326128af7d57a9ca5cd3d"
[[package]] [[package]]
name = "futures-io" name = "futures-io"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e37c1a51b037b80922864b8eed90692c5cd8abd4c71ce49b77146caa47f3253b" checksum = "d41234e71d5e8ca73d01563974ef6f50e516d71e18f1a2f1184742e31f5d469f"
[[package]] [[package]]
name = "futures-macro" name = "futures-macro"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f8719ca0e1f3c5e34f3efe4570ef2c0610ca6da85ae7990d472e9cbfba13664" checksum = "3520e0eb4e704e88d771b92d51273ee212997f0d8282f17f5d8ff1cb39104e42"
dependencies = [ dependencies = [
"proc-macro-hack", "proc-macro-hack",
"proc-macro2", "proc-macro2",
@ -726,24 +720,24 @@ dependencies = [
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6adabac1290109cfa089f79192fb6244ad2c3f1cc2281f3e1dd987592b71feb" checksum = "c72d188479368953c6c8c7140e40d7a4401674ab3b98a41e60e515d6cbdbe5de"
[[package]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92a0843a2ff66823a8f7c77bffe9a09be2b64e533562c412d63075643ec0038" checksum = "08944cea9021170d383287169859c0ca8147d9ec285978393109954448f33cc7"
dependencies = [ dependencies = [
"once_cell", "once_cell",
] ]
[[package]] [[package]]
name = "futures-util" name = "futures-util"
version = "0.3.9" version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "036a2107cdeb57f6d7322f1b6c363dad67cd63ca3b7d1b925bdf75bd5d96cda9" checksum = "d3dd206efbe2ca683b2ce138ccdf61e1b0a63f5816dcedc9d8654c500ba0cea6"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
@ -992,7 +986,6 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"time", "time",
"ureq",
"url", "url",
] ]
@ -1268,9 +1261,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
[[package]] [[package]]
name = "proc-macro-nested" name = "proc-macro-nested"
version = "0.1.6" version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
@ -1420,19 +1413,6 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "rustls"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b"
dependencies = [
"base64 0.13.0",
"log",
"ring",
"sct",
"webpki",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.5" version = "1.0.5"
@ -1769,7 +1749,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a" checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"rustls 0.18.1", "rustls",
"tokio", "tokio",
"webpki", "webpki",
] ]
@ -1910,22 +1890,6 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "ureq"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96014ded8c85822677daee4f909d18acccca744810fd4f8ffc492c284f2324bc"
dependencies = [
"base64 0.13.0",
"chunked_transfer",
"log",
"once_cell",
"rustls 0.19.0",
"url",
"webpki",
"webpki-roots 0.21.0",
]
[[package]] [[package]]
name = "url" name = "url"
version = "2.2.0" version = "2.2.0"
@ -2033,15 +1997,6 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "webpki-roots"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82015b7e0b8bad8185994674a13a93306bea76cf5a16c5a181382fd3a5ec2376"
dependencies = [
"webpki",
]
[[package]] [[package]]
name = "widestring" name = "widestring"
version = "0.4.3" version = "0.4.3"

View File

@ -10,7 +10,6 @@ edition = "2018"
[dependencies] [dependencies]
base64 = "0.13.0" base64 = "0.13.0"
actix-web = { version = "3.3.2", features = ["rustls"] } actix-web = { version = "3.3.2", features = ["rustls"] }
ureq = "2.0.1"
askama = "0.10.5" askama = "0.10.5"
serde = { version = "1.0.118", default_features = false, features = ["derive"] } serde = { version = "1.0.118", default_features = false, features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -18,7 +18,7 @@ async fn style() -> HttpResponse {
async fn robots() -> HttpResponse { async fn robots() -> HttpResponse {
HttpResponse::Ok() HttpResponse::Ok()
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400") .header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
.body(include_str!("../static/robots.txt")) .body("User-agent: *\nAllow: /")
} }
async fn favicon() -> HttpResponse { async fn favicon() -> HttpResponse {
@ -42,7 +42,7 @@ async fn main() -> std::io::Result<()> {
match arg.split('=').collect::<Vec<&str>>()[0] { match arg.split('=').collect::<Vec<&str>>()[0] {
"--address" | "-a" => address = arg.split('=').collect::<Vec<&str>>()[1].to_string(), "--address" | "-a" => address = arg.split('=').collect::<Vec<&str>>()[1].to_string(),
// "--redirect-https" | "-r" => https = true, // "--redirect-https" | "-r" => https = true,
_ => {} _ => (),
} }
} }
@ -51,55 +51,61 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
// REDIRECT TO HTTPS // Redirect to HTTPS
// .wrap(middleware::DefaultHeaders::new().header("Strict-Transport-Security", "max-age=31536000")) // .wrap_fn(|req, srv| { let fut = srv.call(req); async { let mut res = fut.await?; if https {} Ok(res) } })
// .wrap_fn(|req, srv| { // Append trailing slash and remove double slashes
// let fut = srv.call(req);
// async {
// let mut res = fut.await?;
// if https {
// res.headers_mut().insert(
// actix_web::http::header::STRICT_TRANSPORT_SECURITY, actix_web::http::HeaderValue::from_static("max-age=31536000;"),
// );
// }
// Ok(res)
// }
// })
// TRAILING SLASH MIDDLEWARE
.wrap(middleware::NormalizePath::default()) .wrap(middleware::NormalizePath::default())
// DEFAULT SERVICE // Default service in case no routes match
.default_service(web::get().to(|| utils::error("Nothing here".to_string()))) .default_service(web::get().to(|| utils::error("Nothing here".to_string())))
// GENERAL SERVICES // Read static files
.route("/style.css/", web::get().to(style)) .route("/style.css/", web::get().to(style))
.route("/favicon.ico/", web::get().to(favicon)) .route("/favicon.ico/", web::get().to(favicon))
.route("/thumbnail.svg/", web::get().to(thumbnail)) .route("/thumbnail.svg/", web::get().to(thumbnail))
.route("/robots.txt/", web::get().to(robots)) .route("/robots.txt/", web::get().to(robots))
// SETTINGS SERVICE // Proxy media through Libreddit
.route("/settings/", web::get().to(settings::get))
.route("/settings/", web::post().to(settings::set))
// PROXY SERVICE
.route("/proxy/{url:.*}/", web::get().to(proxy::handler)) .route("/proxy/{url:.*}/", web::get().to(proxy::handler))
// SEARCH SERVICES // Browse user profile
.route("/search/", web::get().to(search::find)) .route("/{scope:u|user}/{username}/", web::get().to(user::profile))
.route("r/{sub}/search/", web::get().to(search::find)) // Configure settings
// USER SERVICES .service(web::resource("/settings/").route(web::get().to(settings::get)).route(web::post().to(settings::set)))
.route("/u/{username}/", web::get().to(user::profile)) // Subreddit services
.route("/user/{username}/", web::get().to(user::profile)) .service(
// WIKI SERVICES web::scope("/r/{sub}")
.route("/wiki/", web::get().to(subreddit::wiki)) // See posts and info about subreddit
.route("/wiki/{page}/", web::get().to(subreddit::wiki)) .route("/", web::get().to(subreddit::page))
.route("/r/{sub}/wiki/", web::get().to(subreddit::wiki)) .route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
.route("/r/{sub}/wiki/{page}/", web::get().to(subreddit::wiki)) // View post on subreddit
// SUBREDDIT SERVICES .service(
.route("/r/{sub}/", web::get().to(subreddit::page)) web::scope("/comments/{id}/{title}")
.route("/r/{sub}/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page)) .route("/", web::get().to(post::item))
// POPULAR SERVICES .route("/{comment_id}/", web::get().to(post::item)),
.route("/", web::get().to(subreddit::page)) )
.route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page)) // Search inside subreddit
// POST SERVICES .route("/search/", web::get().to(search::find))
.route("/{id:.{5,6}}/", web::get().to(post::item)) // View wiki of subreddit
.route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::item)) .service(
.route("/r/{sub}/comments/{id}/{title}/{comment_id}/", web::get().to(post::item)) web::scope("/wiki")
.route("/", web::get().to(subreddit::wiki))
.route("/{page}/", web::get().to(subreddit::wiki)),
),
)
// Universal services
.service(
web::scope("")
// Front page
.route("/", web::get().to(subreddit::page))
.route("/{sort:best|hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
// View Reddit wiki
.service(
web::scope("/wiki")
.route("/", web::get().to(subreddit::wiki))
.route("/{page}/", web::get().to(subreddit::wiki)),
)
// Search all of Reddit
.route("/search/", web::get().to(search::find))
// Short link for post
.route("/{id:.{5,6}}/", web::get().to(post::item)),
)
}) })
.bind(&address) .bind(&address)
.unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e)) .unwrap_or_else(|e| panic!("Cannot bind to the address {}: {}", address, e))

View File

@ -1,5 +1,5 @@
// CRATES // CRATES
use crate::utils::{cookie, error, format_num, format_url, media, parse_rich_flair, param, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences}; use crate::utils::{cookie, error, format_num, format_url, media, param, parse_rich_flair, prefs, request, rewrite_url, val, Comment, Flags, Flair, Post, Preferences};
use actix_web::{HttpRequest, HttpResponse}; use actix_web::{HttpRequest, HttpResponse};
use async_recursion::async_recursion; use async_recursion::async_recursion;
@ -57,7 +57,7 @@ pub async fn item(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok().content_type("text/html").body(s) HttpResponse::Ok().content_type("text/html").body(s)
} }
// If the Reddit API returns an error, exit and send error page to user // If the Reddit API returns an error, exit and send error page to user
Err(msg) => error(msg.to_string()).await, Err(msg) => error(msg).await,
} }
} }
@ -82,8 +82,12 @@ async fn parse_post(json: &serde_json::Value) -> Post {
community: val(post, "subreddit"), community: val(post, "subreddit"),
body: rewrite_url(&val(post, "selftext_html")), body: rewrite_url(&val(post, "selftext_html")),
author: val(post, "author"), author: val(post, "author"),
author_flair: Flair{ author_flair: Flair {
flair_parts: parse_rich_flair(val(post, "author_flair_type"), post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str()), flair_parts: parse_rich_flair(
val(post, "author_flair_type"),
post["data"]["author_flair_richtext"].as_array(),
post["data"]["author_flair_text"].as_str(),
),
background_color: val(post, "author_flair_background_color"), background_color: val(post, "author_flair_background_color"),
foreground_color: val(post, "author_flair_text_color"), foreground_color: val(post, "author_flair_text_color"),
}, },
@ -92,8 +96,12 @@ async fn parse_post(json: &serde_json::Value) -> Post {
upvote_ratio: ratio as i64, upvote_ratio: ratio as i64,
post_type, post_type,
thumbnail: format_url(val(post, "thumbnail").as_str()), thumbnail: format_url(val(post, "thumbnail").as_str()),
flair: Flair{ flair: Flair {
flair_parts: parse_rich_flair(val(post, "link_flair_type"), post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str()), flair_parts: parse_rich_flair(
val(post, "link_flair_type"),
post["data"]["link_flair_richtext"].as_array(),
post["data"]["link_flair_text"].as_str(),
),
background_color: val(post, "link_flair_background_color"), background_color: val(post, "link_flair_background_color"),
foreground_color: if val(post, "link_flair_text_color") == "dark" { foreground_color: if val(post, "link_flair_text_color") == "dark" {
"black".to_string() "black".to_string()
@ -145,8 +153,12 @@ async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
score: format_num(score), score: format_num(score),
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"), time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
replies, replies,
flair: Flair{ flair: Flair {
flair_parts: parse_rich_flair(val(&comment, "author_flair_type"), comment["data"]["author_flair_richtext"].as_array(), comment["data"]["author_flair_text"].as_str()), flair_parts: parse_rich_flair(
val(&comment, "author_flair_type"),
comment["data"]["author_flair_richtext"].as_array(),
comment["data"]["author_flair_text"].as_str(),
),
background_color: val(&comment, "author_flair_background_color"), background_color: val(&comment, "author_flair_background_color"),
foreground_color: val(&comment, "author_flair_text_color"), foreground_color: val(&comment, "author_flair_text_color"),
}, },

View File

@ -41,9 +41,9 @@ pub async fn handler(web::Path(b64): web::Path<String>) -> Result<HttpResponse>
Err(error::ErrorForbidden("Resource must be from Reddit")) Err(error::ErrorForbidden("Resource must be from Reddit"))
} }
} }
Err(_) => Err(error::ErrorBadRequest("Can't parse base64 into URL")), _ => Err(error::ErrorBadRequest("Can't parse base64 into URL")),
} }
} }
Err(_) => Err(error::ErrorBadRequest("Can't decode base64")), _ => Err(error::ErrorBadRequest("Can't decode base64")),
} }
} }

View File

@ -90,6 +90,6 @@ pub async fn find(req: HttpRequest) -> HttpResponse {
.render() .render()
.unwrap(), .unwrap(),
), ),
Err(msg) => error(msg.to_string()).await, Err(msg) => error(msg).await,
} }
} }

View File

@ -58,33 +58,33 @@ pub async fn page(req: HttpRequest) -> HttpResponse {
.unwrap(); .unwrap();
HttpResponse::Ok().content_type("text/html").body(s) HttpResponse::Ok().content_type("text/html").body(s)
} }
Err(msg) => error(msg.to_string()).await, Err(msg) => error(msg).await,
} }
} }
pub async fn wiki(req: HttpRequest) -> HttpResponse { pub async fn wiki(req: HttpRequest) -> HttpResponse {
let sub = req.match_info().get("sub").unwrap_or("reddit.com"); let sub = req.match_info().get("sub").unwrap_or("reddit.com").to_string();
let page = req.match_info().get("page").unwrap_or("index"); let page = req.match_info().get("page").unwrap_or("index").to_string();
let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page); let path: String = format!("/r/{}/wiki/{}.json?raw_json=1", sub, page);
match request(&path).await { match request(&path).await {
Ok(res) => { Ok(res) => {
let s = WikiTemplate { let s = WikiTemplate {
sub: sub.to_string(), sub,
wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap_or_default()), wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap_or_default()),
page: page.to_string(), page,
prefs: prefs(req), prefs: prefs(req),
} }
.render() .render()
.unwrap(); .unwrap();
HttpResponse::Ok().content_type("text/html").body(s) HttpResponse::Ok().content_type("text/html").body(s)
} }
Err(msg) => error(msg.to_string()).await, Err(msg) => error(msg).await,
} }
} }
// SUBREDDIT // SUBREDDIT
async fn subreddit(sub: &str) -> Result<Subreddit, &'static str> { async fn subreddit(sub: &str) -> Result<Subreddit, String> {
// Build the Reddit JSON API url // Build the Reddit JSON API url
let path: String = format!("/r/{}/about.json?raw_json=1", sub); let path: String = format!("/r/{}/about.json?raw_json=1", sub);

View File

@ -1,5 +1,5 @@
// CRATES // CRATES
use crate::utils::{error, fetch_posts, format_url, nested_val, param, prefs, request, Post, Preferences, User}; use crate::utils::{error, fetch_posts, format_url, param, prefs, request, Post, Preferences, User};
use actix_web::{HttpRequest, HttpResponse, Result}; use actix_web::{HttpRequest, HttpResponse, Result};
use askama::Template; use askama::Template;
use time::OffsetDateTime; use time::OffsetDateTime;
@ -42,12 +42,12 @@ pub async fn profile(req: HttpRequest) -> HttpResponse {
HttpResponse::Ok().content_type("text/html").body(s) HttpResponse::Ok().content_type("text/html").body(s)
} }
// If there is an error show error page // If there is an error show error page
Err(msg) => error(msg.to_string()).await, Err(msg) => error(msg).await,
} }
} }
// USER // USER
async fn user(name: &str) -> Result<User, &'static str> { async fn user(name: &str) -> Result<User, String> {
// Build the Reddit JSON API path // Build the Reddit JSON API path
let path: String = format!("/user/{}/about.json", name); let path: String = format!("/user/{}/about.json", name);
@ -58,15 +58,18 @@ async fn user(name: &str) -> Result<User, &'static str> {
// Grab creation date as unix timestamp // Grab creation date as unix timestamp
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64; let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
// nested_val function used to parse JSON from Reddit APIs
let about = |item| res["data"]["subreddit"][item].as_str().unwrap_or_default().to_string();
// Parse the JSON output into a User struct // Parse the JSON output into a User struct
Ok(User { Ok(User {
name: name.to_string(), name: name.to_string(),
title: nested_val(&res, "subreddit", "title"), title: about("title"),
icon: format_url(nested_val(&res, "subreddit", "icon_img").as_str()), icon: format_url(about("icon_img").as_str()),
karma: res["data"]["total_karma"].as_i64().unwrap_or(0), karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"), created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
banner: nested_val(&res, "subreddit", "banner_img"), banner: about("banner_img"),
description: nested_val(&res, "subreddit", "public_description"), description: about("public_description"),
}) })
} }
// If the Reddit API returns an error, exit this function // If the Reddit API returns an error, exit this function

View File

@ -7,21 +7,21 @@ use base64::encode;
use regex::Regex; use regex::Regex;
use serde_json::{from_str, Value}; use serde_json::{from_str, Value};
use std::collections::HashMap; use std::collections::HashMap;
use time::{OffsetDateTime, Duration}; use time::{Duration, OffsetDateTime};
use url::Url; use url::Url;
// //
// STRUCTS // STRUCTS
// //
// Post flair with content, background color and foreground color // Post flair with content, background color and foreground color
pub struct Flair{ pub struct Flair {
pub flair_parts: Vec<FlairPart>, pub flair_parts: Vec<FlairPart>,
pub background_color: String, pub background_color: String,
pub foreground_color: String, pub foreground_color: String,
} }
pub struct FlairPart{ pub struct FlairPart {
pub flair_part_type: String, pub flair_part_type: String,
pub value: String, pub value: String,
} }
@ -101,7 +101,7 @@ pub struct Params {
#[derive(Template)] #[derive(Template)]
#[template(path = "error.html", escape = "none")] #[template(path = "error.html", escape = "none")]
pub struct ErrorTemplate { pub struct ErrorTemplate {
pub message: String, pub msg: String,
pub prefs: Preferences, pub prefs: Preferences,
} }
@ -169,7 +169,7 @@ pub fn format_num(num: i64) -> String {
} }
} }
pub async fn media(data: &serde_json::Value) -> (String, String) { pub async fn media(data: &Value) -> (String, String) {
let post_type: &str; let post_type: &str;
let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() { let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
post_type = "video"; post_type = "video";
@ -183,11 +183,11 @@ pub async fn media(data: &serde_json::Value) -> (String, String) {
Some(gif) => { Some(gif) => {
post_type = "gif"; post_type = "gif";
format_url(gif["source"]["url"].as_str().unwrap_or_default()) format_url(gif["source"]["url"].as_str().unwrap_or_default())
}, }
None => { None => {
post_type = "image"; post_type = "image";
format_url(preview["source"]["url"].as_str().unwrap_or_default()) format_url(preview["source"]["url"].as_str().unwrap_or_default())
}, }
} }
} else if data["is_self"].as_bool().unwrap_or_default() { } else if data["is_self"].as_bool().unwrap_or_default() {
post_type = "self"; post_type = "self";
@ -201,37 +201,42 @@ pub async fn media(data: &serde_json::Value) -> (String, String) {
} }
pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, text_flair: Option<&str>) -> Vec<FlairPart> { pub fn parse_rich_flair(flair_type: String, rich_flair: Option<&Vec<Value>>, text_flair: Option<&str>) -> Vec<FlairPart> {
let mut result: Vec<FlairPart> = Vec::new(); match flair_type.as_str() {
if flair_type == "richtext" && !rich_flair.is_none() { "richtext" => match rich_flair {
for part in rich_flair.unwrap() { Some(rich) => rich
let flair_part_type = part["e"].as_str().unwrap_or_default().to_string(); .iter()
let value = if flair_part_type == "text" { .map(|part| {
part["t"].as_str().unwrap_or_default().to_string() let value = |name: &str| part[name].as_str().unwrap_or_default();
FlairPart {
} else if flair_part_type == "emoji" { flair_part_type: value("e").to_string(),
format_url(part["u"].as_str().unwrap_or_default()) value: match value("e") {
} else { "text" => value("t").to_string(),
"".to_string() "emoji" => format_url(value("u")),
}; _ => String::new(),
result.push(FlairPart { },
flair_part_type, }
value, })
}); .collect::<Vec<FlairPart>>(),
} None => Vec::new(),
} else if flair_type == "text" && !text_flair.is_none() { },
result.push(FlairPart { "text" => match text_flair {
flair_part_type: "text".to_string(), Some(text) => vec![FlairPart {
value: text_flair.unwrap().to_string(), flair_part_type: "text".to_string(),
}); value: text.to_string(),
}],
None => Vec::new(),
},
_ => Vec::new(),
} }
result
} }
pub fn time(unix_time: i64) -> String { pub fn time(unix_time: i64) -> String {
let time = OffsetDateTime::from_unix_timestamp(unix_time); let time = OffsetDateTime::from_unix_timestamp(unix_time);
let time_delta = OffsetDateTime::now_utc() - time; let time_delta = OffsetDateTime::now_utc() - time;
if time_delta > Duration::days(1) { if time_delta > Duration::days(30) {
time.format("%b %d '%y") // %b %e '%y time.format("%b %d '%y") // %b %e '%y
} else if time_delta.whole_days() > 0 {
format!("{}d ago", time_delta.whole_days())
} else if time_delta.whole_hours() > 0 { } else if time_delta.whole_hours() > 0 {
format!("{}h ago", time_delta.whole_hours()) format!("{}h ago", time_delta.whole_hours())
} else { } else {
@ -244,17 +249,12 @@ pub fn time(unix_time: i64) -> String {
// //
// val() function used to parse JSON from Reddit APIs // val() function used to parse JSON from Reddit APIs
pub fn val(j: &serde_json::Value, k: &str) -> String { pub fn val(j: &Value, k: &str) -> String {
String::from(j["data"][k].as_str().unwrap_or_default()) String::from(j["data"][k].as_str().unwrap_or_default())
} }
// nested_val() function used to parse JSON from Reddit APIs
pub fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
String::from(j["data"][n][k].as_str().unwrap_or_default())
}
// 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_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> { pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), String> {
let res; let res;
let post_list; let post_list;
@ -271,7 +271,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
// Fetch the list of posts from the JSON response // Fetch the list of posts from the JSON response
match res["data"]["children"].as_array() { match res["data"]["children"].as_array() {
Some(list) => post_list = list, Some(list) => post_list = list,
None => return Err("No posts found"), None => return Err("No posts found".to_string()),
} }
let mut posts: Vec<Post> = Vec::new(); let mut posts: Vec<Post> = Vec::new();
@ -292,8 +292,12 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
community: val(post, "subreddit"), community: val(post, "subreddit"),
body: rewrite_url(&val(post, "body_html")), body: rewrite_url(&val(post, "body_html")),
author: val(post, "author"), author: val(post, "author"),
author_flair: Flair{ author_flair: Flair {
flair_parts: parse_rich_flair(val(post, "author_flair_type"), post["data"]["author_flair_richtext"].as_array(), post["data"]["author_flair_text"].as_str()), flair_parts: parse_rich_flair(
val(post, "author_flair_type"),
post["data"]["author_flair_richtext"].as_array(),
post["data"]["author_flair_text"].as_str(),
),
background_color: val(post, "author_flair_background_color"), background_color: val(post, "author_flair_background_color"),
foreground_color: val(post, "author_flair_text_color"), foreground_color: val(post, "author_flair_text_color"),
}, },
@ -303,8 +307,12 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
thumbnail: format_url(val(post, "thumbnail").as_str()), thumbnail: format_url(val(post, "thumbnail").as_str()),
media, media,
domain: val(post, "domain"), domain: val(post, "domain"),
flair: Flair{ flair: Flair {
flair_parts: parse_rich_flair(val(post, "link_flair_type"), post["data"]["link_flair_richtext"].as_array(), post["data"]["link_flair_text"].as_str()), flair_parts: parse_rich_flair(
val(post, "link_flair_type"),
post["data"]["link_flair_richtext"].as_array(),
post["data"]["link_flair_text"].as_str(),
),
background_color: val(post, "link_flair_background_color"), background_color: val(post, "link_flair_background_color"),
foreground_color: if val(post, "link_flair_text_color") == "dark" { foreground_color: if val(post, "link_flair_text_color") == "dark" {
"black".to_string() "black".to_string()
@ -330,7 +338,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
pub async fn error(msg: String) -> HttpResponse { pub async fn error(msg: String) -> HttpResponse {
let body = ErrorTemplate { let body = ErrorTemplate {
message: msg, msg,
prefs: Preferences::default(), prefs: Preferences::default(),
} }
.render() .render()
@ -339,34 +347,50 @@ pub async fn error(msg: String) -> HttpResponse {
} }
// Make a request to a Reddit API and parse the JSON response // Make a request to a Reddit API and parse the JSON response
pub async fn request(path: &str) -> Result<serde_json::Value, &'static str> { pub async fn request(path: &str) -> Result<Value, String> {
let url = format!("https://www.reddit.com{}", path); let url = format!("https://www.reddit.com{}", path);
// Send request using ureq // Send request using awc
match ureq::get(&url).call() { async fn send(url: &str) -> Result<String, (bool, String)> {
// If response is success let client = actix_web::client::Client::default();
Ok(response) => { let response = client.get(url).send().await;
// Parse the response from Reddit as JSON
match from_str(&response.into_string().unwrap()) { match response {
Ok(json) => Ok(json), Ok(mut payload) => {
Err(_) => { // Get first number of response HTTP status code
#[cfg(debug_assertions)] match payload.status().to_string().chars().next() {
dbg!(format!("{} - Failed to parse page JSON data", url)); // If success
Err("Failed to parse page JSON data") Some('2') => Ok(String::from_utf8(payload.body().limit(20_000_000).await.unwrap().to_vec()).unwrap()),
// If redirection
Some('3') => Err((true, payload.headers().get("location").unwrap().to_str().unwrap().to_string())),
// Otherwise
_ => Err((false, "Page not found".to_string())),
} }
} }
} Err(_) => Err((false, "Couldn't send request to Reddit".to_string())),
// If response is error
Err(ureq::Error::Status(_, _)) => {
#[cfg(debug_assertions)]
dbg!(format!("{} - Page not found", url));
Err("Page not found")
}
// If failed to send request
Err(e) => {
#[cfg(debug_assertions)]
dbg!(e);
Err("Couldn't send request to Reddit")
} }
} }
fn err(u: String, m: String) -> Result<Value, String> {
#[cfg(debug_assertions)]
dbg!(format!("{} - {}", u, m));
Err(m)
};
fn json(url: String, body: String) -> Result<Value, String> {
match from_str(body.as_str()) {
Ok(json) => Ok(json),
Err(_) => err(url, "Failed to parse page JSON data".to_string()),
}
}
match send(&url).await {
Ok(body) => json(url, body),
Err((true, location)) => match send(location.as_str()).await {
Ok(body) => json(url, body),
Err((true, location)) => err(url, location),
Err((_, msg)) => err(url, msg),
},
Err((_, msg)) => err(url, msg),
}
} }

View File

@ -1,6 +1,6 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}Error: {{ message }}{% endblock %} {% block title %}Error: {{ msg }}{% endblock %}
{% block sortstyle %}{% endblock %} {% block sortstyle %}{% endblock %}
{% block content %} {% block content %}
<h1 style="text-align: center; font-size: 50px;">{{ message }}</h1> <h1 style="text-align: center; font-size: 50px;">{{ msg }}</h1>
{% endblock %} {% endblock %}