2020-11-21 19:05:27 +13:00
|
|
|
//
|
|
|
|
// CRATES
|
|
|
|
//
|
2021-01-09 14:50:03 +13:00
|
|
|
use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result};
|
2021-01-01 18:03:44 +13:00
|
|
|
use askama::Template;
|
2021-01-03 17:50:23 +13:00
|
|
|
use base64::encode;
|
2021-01-03 07:58:21 +13:00
|
|
|
use regex::Regex;
|
2021-01-02 19:21:43 +13:00
|
|
|
use serde_json::from_str;
|
2021-01-09 14:35:04 +13:00
|
|
|
use std::collections::HashMap;
|
2021-01-13 07:59:32 +13:00
|
|
|
use time::{OffsetDateTime, Duration};
|
2021-01-01 12:54:13 +13:00
|
|
|
use url::Url;
|
2020-11-21 19:05:27 +13:00
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
//
|
|
|
|
// STRUCTS
|
|
|
|
//
|
2020-11-18 08:37:40 +13:00
|
|
|
// Post flair with text, background color and foreground color
|
|
|
|
pub struct Flair(pub String, pub String, pub String);
|
2020-12-30 16:01:02 +13:00
|
|
|
// Post flags with nsfw and stickied
|
|
|
|
pub struct Flags {
|
|
|
|
pub nsfw: bool,
|
2021-01-01 12:54:13 +13:00
|
|
|
pub stickied: bool,
|
2020-12-30 16:01:02 +13:00
|
|
|
}
|
2020-11-18 08:37:40 +13:00
|
|
|
|
|
|
|
// Post containing content, metadata and media
|
|
|
|
pub struct Post {
|
2021-01-04 10:06:49 +13:00
|
|
|
pub id: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub title: String,
|
|
|
|
pub community: String,
|
|
|
|
pub body: String,
|
|
|
|
pub author: String,
|
2020-12-21 08:29:23 +13:00
|
|
|
pub author_flair: Flair,
|
2021-01-04 10:06:49 +13:00
|
|
|
pub permalink: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub score: String,
|
2021-01-04 10:06:49 +13:00
|
|
|
pub upvote_ratio: i64,
|
2020-12-01 17:33:55 +13:00
|
|
|
pub post_type: String,
|
2020-12-23 15:29:43 +13:00
|
|
|
pub flair: Flair,
|
2020-12-30 16:01:02 +13:00
|
|
|
pub flags: Flags,
|
2021-01-06 15:04:49 +13:00
|
|
|
pub thumbnail: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub media: String,
|
2021-01-12 11:08:12 +13:00
|
|
|
pub domain: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub time: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment with content, post, score and data/time that it was posted
|
|
|
|
pub struct Comment {
|
2020-12-21 17:52:15 +13:00
|
|
|
pub id: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub body: String,
|
|
|
|
pub author: String,
|
2020-12-21 08:29:23 +13:00
|
|
|
pub flair: Flair,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub score: String,
|
2020-11-30 15:50:29 +13:00
|
|
|
pub time: String,
|
2020-12-20 16:54:46 +13:00
|
|
|
pub replies: Vec<Comment>,
|
2020-11-18 08:37:40 +13:00
|
|
|
}
|
|
|
|
|
2021-01-09 17:55:40 +13:00
|
|
|
#[derive(Default)]
|
2020-11-18 08:37:40 +13:00
|
|
|
// User struct containing metadata about user
|
|
|
|
pub struct User {
|
|
|
|
pub name: String,
|
2021-01-01 17:21:56 +13:00
|
|
|
pub title: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub icon: String,
|
|
|
|
pub karma: i64,
|
2020-12-24 19:16:04 +13:00
|
|
|
pub created: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub banner: String,
|
2020-11-30 15:50:29 +13:00
|
|
|
pub description: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
}
|
|
|
|
|
2020-12-29 15:42:46 +13:00
|
|
|
#[derive(Default)]
|
2020-11-18 08:37:40 +13:00
|
|
|
// Subreddit struct containing metadata about community
|
|
|
|
pub struct Subreddit {
|
|
|
|
pub name: String,
|
|
|
|
pub title: String,
|
|
|
|
pub description: String,
|
2020-12-29 15:42:46 +13:00
|
|
|
pub info: String,
|
2020-11-18 08:37:40 +13:00
|
|
|
pub icon: String,
|
2020-11-23 13:43:23 +13:00
|
|
|
pub members: String,
|
2020-11-30 15:50:29 +13:00
|
|
|
pub active: String,
|
2021-01-02 19:21:43 +13:00
|
|
|
pub wiki: bool,
|
2020-11-18 08:37:40 +13:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:49:32 +13:00
|
|
|
// Parser for query params, used in sorting (eg. /r/rust/?sort=hot)
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
pub struct Params {
|
2020-12-30 14:11:47 +13:00
|
|
|
pub t: Option<String>,
|
2021-01-01 12:54:13 +13:00
|
|
|
pub q: Option<String>,
|
2020-11-20 10:49:32 +13:00
|
|
|
pub sort: Option<String>,
|
|
|
|
pub after: Option<String>,
|
2020-11-30 15:50:29 +13:00
|
|
|
pub before: Option<String>,
|
2020-11-20 10:49:32 +13:00
|
|
|
}
|
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
// Error template
|
2021-01-01 18:03:44 +13:00
|
|
|
#[derive(Template)]
|
2020-11-20 17:42:18 +13:00
|
|
|
#[template(path = "error.html", escape = "none")]
|
|
|
|
pub struct ErrorTemplate {
|
2020-11-30 15:50:29 +13:00
|
|
|
pub message: String,
|
2021-01-11 15:15:34 +13:00
|
|
|
pub prefs: Preferences,
|
2020-11-20 17:42:18 +13:00
|
|
|
}
|
|
|
|
|
2021-01-11 15:15:34 +13:00
|
|
|
#[derive(Default)]
|
2021-01-09 14:35:04 +13:00
|
|
|
pub struct Preferences {
|
2021-01-11 15:15:34 +13:00
|
|
|
pub theme: String,
|
2021-01-09 17:55:40 +13:00
|
|
|
pub front_page: String,
|
2021-01-09 14:35:04 +13:00
|
|
|
pub layout: String,
|
2021-01-11 10:08:36 +13:00
|
|
|
pub wide: String,
|
2021-01-09 14:35:04 +13:00
|
|
|
pub hide_nsfw: String,
|
2021-01-09 14:50:03 +13:00
|
|
|
pub comment_sort: String,
|
2021-01-09 14:35:04 +13:00
|
|
|
}
|
|
|
|
|
2020-12-01 18:10:08 +13:00
|
|
|
//
|
2020-12-08 07:53:22 +13:00
|
|
|
// FORMATTING
|
2020-12-01 18:10:08 +13:00
|
|
|
//
|
|
|
|
|
2021-01-09 14:35:04 +13:00
|
|
|
// Build preferences from cookies
|
2021-01-09 14:50:03 +13:00
|
|
|
pub fn prefs(req: HttpRequest) -> Preferences {
|
2021-01-09 14:35:04 +13:00
|
|
|
Preferences {
|
2021-01-11 15:15:34 +13:00
|
|
|
theme: cookie(&req, "theme"),
|
2021-01-09 17:55:40 +13:00
|
|
|
front_page: cookie(&req, "front_page"),
|
2021-01-09 14:50:03 +13:00
|
|
|
layout: cookie(&req, "layout"),
|
2021-01-11 10:08:36 +13:00
|
|
|
wide: cookie(&req, "wide"),
|
2021-01-09 14:50:03 +13:00
|
|
|
hide_nsfw: cookie(&req, "hide_nsfw"),
|
|
|
|
comment_sort: cookie(&req, "comment_sort"),
|
2021-01-09 14:35:04 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 12:54:13 +13:00
|
|
|
// Grab a query param from a url
|
2021-01-02 09:33:57 +13:00
|
|
|
pub fn param(path: &str, value: &str) -> String {
|
2021-01-03 19:37:54 +13:00
|
|
|
let url = Url::parse(format!("https://libredd.it/{}", path).as_str()).unwrap();
|
2021-01-06 15:04:49 +13:00
|
|
|
let pairs: HashMap<_, _> = url.query_pairs().into_owned().collect();
|
2021-01-01 12:54:13 +13:00
|
|
|
pairs.get(value).unwrap_or(&String::new()).to_owned()
|
|
|
|
}
|
|
|
|
|
2021-01-07 18:27:24 +13:00
|
|
|
// Parse Cookie value from request
|
2021-01-09 14:50:03 +13:00
|
|
|
pub fn cookie(req: &HttpRequest, name: &str) -> String {
|
|
|
|
actix_web::HttpMessage::cookie(req, name).unwrap_or_else(|| Cookie::new(name, "")).value().to_string()
|
2021-01-06 15:04:49 +13:00
|
|
|
}
|
2021-01-03 17:50:23 +13:00
|
|
|
|
2020-12-26 15:06:33 +13:00
|
|
|
// Direct urls to proxy if proxy is enabled
|
2021-01-12 14:47:14 +13:00
|
|
|
pub fn format_url(url: &str) -> String {
|
2021-01-09 14:35:04 +13:00
|
|
|
if url.is_empty() || url == "self" || url == "default" || url == "nsfw" || url == "spoiler" {
|
2021-01-05 16:26:41 +13:00
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
format!("/proxy/{}", encode(url).as_str())
|
|
|
|
}
|
2020-12-01 18:10:08 +13:00
|
|
|
}
|
|
|
|
|
2021-01-03 07:58:21 +13:00
|
|
|
// Rewrite Reddit links to Libreddit in body of text
|
2021-01-03 08:09:26 +13:00
|
|
|
pub fn rewrite_url(text: &str) -> String {
|
2021-01-03 07:58:21 +13:00
|
|
|
let re = Regex::new(r#"href="(https://|http://|)(www.|)(reddit).(com)/"#).unwrap();
|
|
|
|
re.replace_all(text, r#"href="/"#).to_string()
|
|
|
|
}
|
|
|
|
|
2020-12-26 15:06:33 +13:00
|
|
|
// Append `m` and `k` for millions and thousands respectively
|
2020-12-08 07:53:22 +13:00
|
|
|
pub fn format_num(num: i64) -> String {
|
2021-01-09 14:35:04 +13:00
|
|
|
if num > 1_000_000 {
|
|
|
|
format!("{}m", num / 1_000_000)
|
2020-12-08 08:36:05 +13:00
|
|
|
} else if num > 1000 {
|
2021-01-09 14:35:04 +13:00
|
|
|
format!("{}k", num / 1_000)
|
2020-12-08 08:36:05 +13:00
|
|
|
} else {
|
|
|
|
num.to_string()
|
|
|
|
}
|
2020-12-08 07:53:22 +13:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:04:49 +13:00
|
|
|
pub async fn media(data: &serde_json::Value) -> (String, String) {
|
|
|
|
let post_type: &str;
|
|
|
|
let url = if !data["preview"]["reddit_video_preview"]["fallback_url"].is_null() {
|
|
|
|
post_type = "video";
|
2021-01-12 14:47:14 +13:00
|
|
|
format_url(data["preview"]["reddit_video_preview"]["fallback_url"].as_str().unwrap_or_default())
|
2021-01-06 15:04:49 +13:00
|
|
|
} else if !data["secure_media"]["reddit_video"]["fallback_url"].is_null() {
|
|
|
|
post_type = "video";
|
2021-01-12 14:47:14 +13:00
|
|
|
format_url(data["secure_media"]["reddit_video"]["fallback_url"].as_str().unwrap_or_default())
|
2021-01-06 15:04:49 +13:00
|
|
|
} else if data["post_hint"].as_str().unwrap_or("") == "image" {
|
2021-01-12 14:47:14 +13:00
|
|
|
let preview = data["preview"]["images"][0].clone();
|
2021-01-06 15:04:49 +13:00
|
|
|
post_type = "image";
|
2021-01-12 14:47:14 +13:00
|
|
|
match preview["variants"]["mp4"].as_object() {
|
|
|
|
Some(gif) => format_url(gif["source"]["url"].as_str().unwrap_or_default()),
|
2021-01-12 15:05:13 +13:00
|
|
|
None => format_url(preview["source"]["url"].as_str().unwrap_or_default()),
|
2021-01-12 14:47:14 +13:00
|
|
|
}
|
2021-01-12 13:35:50 +13:00
|
|
|
} else if data["is_self"].as_bool().unwrap_or_default() {
|
2021-01-12 11:08:12 +13:00
|
|
|
post_type = "self";
|
|
|
|
data["permalink"].as_str().unwrap_or_default().to_string()
|
2021-01-06 15:04:49 +13:00
|
|
|
} else {
|
|
|
|
post_type = "link";
|
|
|
|
data["url"].as_str().unwrap_or_default().to_string()
|
|
|
|
};
|
|
|
|
|
2021-01-12 15:05:13 +13:00
|
|
|
(post_type.to_string(), url)
|
2021-01-06 15:04:49 +13:00
|
|
|
}
|
|
|
|
|
2021-01-13 07:59:32 +13:00
|
|
|
pub fn time(unix_time: i64) -> String {
|
|
|
|
let time = OffsetDateTime::from_unix_timestamp(unix_time);
|
|
|
|
let time_delta = OffsetDateTime::now_utc() - time;
|
|
|
|
if time_delta > Duration::days(1) {
|
|
|
|
time.format("%b %d '%y") // %b %e '%y
|
|
|
|
} else if time_delta.whole_hours() > 0 {
|
|
|
|
format!("{}h ago", time_delta.whole_hours())
|
|
|
|
} else {
|
|
|
|
format!("{}m ago", time_delta.whole_minutes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
//
|
|
|
|
// JSON PARSING
|
|
|
|
//
|
|
|
|
|
2020-11-18 08:37:40 +13:00
|
|
|
// val() function used to parse JSON from Reddit APIs
|
2021-01-02 09:33:57 +13:00
|
|
|
pub fn val(j: &serde_json::Value, k: &str) -> String {
|
2021-01-02 19:21:43 +13:00
|
|
|
String::from(j["data"][k].as_str().unwrap_or_default())
|
2020-11-18 08:37:40 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// nested_val() function used to parse JSON from Reddit APIs
|
2021-01-02 09:33:57 +13:00
|
|
|
pub fn nested_val(j: &serde_json::Value, n: &str, k: &str) -> String {
|
2021-01-02 12:28:13 +13:00
|
|
|
String::from(j["data"][n][k].as_str().unwrap_or_default())
|
2020-11-18 13:03:28 +13:00
|
|
|
}
|
|
|
|
|
2021-01-07 18:27:24 +13:00
|
|
|
// Fetch posts of a user or subreddit and return a vector of posts and the "after" value
|
2021-01-02 09:55:09 +13:00
|
|
|
pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post>, String), &'static str> {
|
2021-01-02 12:28:13 +13:00
|
|
|
let res;
|
|
|
|
let post_list;
|
|
|
|
|
|
|
|
// Send a request to the url
|
|
|
|
match request(&path).await {
|
|
|
|
// If success, receive JSON in response
|
2021-01-08 05:38:05 +13:00
|
|
|
Ok(response) => {
|
|
|
|
res = response;
|
|
|
|
}
|
2021-01-02 12:28:13 +13:00
|
|
|
// If the Reddit API returns an error, exit this function
|
2021-01-02 19:21:43 +13:00
|
|
|
Err(msg) => return Err(msg),
|
2020-11-21 19:05:27 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the list of posts from the JSON response
|
2021-01-02 12:28:13 +13:00
|
|
|
match res["data"]["children"].as_array() {
|
2021-01-02 19:21:43 +13:00
|
|
|
Some(list) => post_list = list,
|
|
|
|
None => return Err("No posts found"),
|
2021-01-02 12:28:13 +13:00
|
|
|
}
|
2020-11-21 19:05:27 +13:00
|
|
|
|
|
|
|
let mut posts: Vec<Post> = Vec::new();
|
|
|
|
|
2021-01-05 16:26:41 +13:00
|
|
|
// For each post from posts list
|
2020-12-23 15:29:43 +13:00
|
|
|
for post in post_list {
|
2021-01-02 12:28:13 +13:00
|
|
|
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap_or_default().round() as i64;
|
|
|
|
let score = post["data"]["score"].as_i64().unwrap_or_default();
|
2021-01-04 10:06:49 +13:00
|
|
|
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
|
2021-01-13 08:02:35 +13:00
|
|
|
let title = val(post, "title");
|
2021-01-06 15:04:49 +13:00
|
|
|
// Determine the type of media along with the media URL
|
2021-01-07 18:27:24 +13:00
|
|
|
let (post_type, media) = media(&post["data"]).await;
|
2021-01-06 15:04:49 +13:00
|
|
|
|
2020-11-21 19:05:27 +13:00
|
|
|
posts.push(Post {
|
2021-01-04 10:06:49 +13:00
|
|
|
id: val(post, "id"),
|
2020-11-21 19:05:27 +13:00
|
|
|
title: if title.is_empty() { fallback_title.to_owned() } else { title },
|
2021-01-02 09:33:57 +13:00
|
|
|
community: val(post, "subreddit"),
|
2021-01-03 08:09:26 +13:00
|
|
|
body: rewrite_url(&val(post, "body_html")),
|
2021-01-02 09:33:57 +13:00
|
|
|
author: val(post, "author"),
|
2020-12-21 08:29:23 +13:00
|
|
|
author_flair: Flair(
|
2021-01-02 09:33:57 +13:00
|
|
|
val(post, "author_flair_text"),
|
|
|
|
val(post, "author_flair_background_color"),
|
|
|
|
val(post, "author_flair_text_color"),
|
2020-12-21 08:29:23 +13:00
|
|
|
),
|
2020-12-08 07:53:22 +13:00
|
|
|
score: format_num(score),
|
2021-01-04 10:06:49 +13:00
|
|
|
upvote_ratio: ratio as i64,
|
2021-01-07 18:27:24 +13:00
|
|
|
post_type,
|
2021-01-12 14:47:14 +13:00
|
|
|
thumbnail: format_url(val(post, "thumbnail").as_str()),
|
2021-01-07 18:27:24 +13:00
|
|
|
media,
|
2021-01-12 11:08:12 +13:00
|
|
|
domain: val(post, "domain"),
|
2020-11-21 19:05:27 +13:00
|
|
|
flair: Flair(
|
2021-01-02 09:33:57 +13:00
|
|
|
val(post, "link_flair_text"),
|
|
|
|
val(post, "link_flair_background_color"),
|
|
|
|
if val(post, "link_flair_text_color") == "dark" {
|
2020-11-21 19:05:27 +13:00
|
|
|
"black".to_string()
|
|
|
|
} else {
|
|
|
|
"white".to_string()
|
|
|
|
},
|
|
|
|
),
|
2020-12-30 16:01:02 +13:00
|
|
|
flags: Flags {
|
2021-01-05 16:26:41 +13:00
|
|
|
nsfw: post["data"]["over_18"].as_bool().unwrap_or_default(),
|
|
|
|
stickied: post["data"]["stickied"].as_bool().unwrap_or_default(),
|
2020-12-30 16:01:02 +13:00
|
|
|
},
|
2021-01-04 10:06:49 +13:00
|
|
|
permalink: val(post, "permalink"),
|
2021-01-13 07:59:32 +13:00
|
|
|
time: time(unix_time),
|
2020-11-21 19:05:27 +13:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:26:41 +13:00
|
|
|
Ok((posts, res["data"]["after"].as_str().unwrap_or_default().to_string()))
|
2020-11-21 19:05:27 +13:00
|
|
|
}
|
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
//
|
|
|
|
// NETWORKING
|
|
|
|
//
|
|
|
|
|
2021-01-05 16:26:41 +13:00
|
|
|
pub async fn error(msg: String) -> HttpResponse {
|
2021-01-11 15:15:34 +13:00
|
|
|
let body = ErrorTemplate {
|
|
|
|
message: msg,
|
|
|
|
prefs: Preferences::default(),
|
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap_or_default();
|
2021-01-03 17:50:23 +13:00
|
|
|
HttpResponse::NotFound().content_type("text/html").body(body)
|
2021-01-01 18:03:44 +13:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:50:59 +13:00
|
|
|
// Make a request to a Reddit API and parse the JSON response
|
2021-01-02 09:55:09 +13:00
|
|
|
pub async fn request(path: &str) -> Result<serde_json::Value, &'static str> {
|
2021-01-12 07:33:48 +13:00
|
|
|
let url = format!("https://www.reddit.com{}", path);
|
|
|
|
|
2021-01-12 14:47:14 +13:00
|
|
|
// Send request using ureq
|
2021-01-12 07:33:48 +13:00
|
|
|
match ureq::get(&url).call() {
|
|
|
|
// If response is success
|
|
|
|
Ok(response) => {
|
|
|
|
// Parse the response from Reddit as JSON
|
|
|
|
match from_str(&response.into_string().unwrap()) {
|
|
|
|
Ok(json) => Ok(json),
|
|
|
|
Err(_) => {
|
2021-01-02 12:28:13 +13:00
|
|
|
#[cfg(debug_assertions)]
|
2021-01-12 07:33:48 +13:00
|
|
|
dbg!(format!("{} - Failed to parse page JSON data", url));
|
|
|
|
Err("Failed to parse page JSON data")
|
2021-01-02 12:28:13 +13:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 16:26:41 +13:00
|
|
|
}
|
2021-01-12 07:33:48 +13:00
|
|
|
// 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) => {
|
2021-01-02 12:28:13 +13:00
|
|
|
#[cfg(debug_assertions)]
|
2021-01-12 07:33:48 +13:00
|
|
|
dbg!(e);
|
2021-01-04 18:31:21 +13:00
|
|
|
Err("Couldn't send request to Reddit")
|
2021-01-02 12:28:13 +13:00
|
|
|
}
|
2020-11-20 17:42:18 +13:00
|
|
|
}
|
2020-11-19 15:50:59 +13:00
|
|
|
}
|