2020-10-25 13:25:59 -07:00
|
|
|
// CRATES
|
2021-03-17 15:30:33 -07:00
|
|
|
use crate::client::json;
|
2021-03-11 20:15:26 -08:00
|
|
|
use crate::esc;
|
2021-03-17 15:30:33 -07:00
|
|
|
use crate::server::RequestExt;
|
2021-11-25 21:02:04 -07:00
|
|
|
use crate::utils::{error, filter_posts, format_url, get_filters, param, template, Post, Preferences, User};
|
2020-10-25 13:25:59 -07:00
|
|
|
use askama::Template;
|
2021-03-17 15:30:33 -07:00
|
|
|
use hyper::{Body, Request, Response};
|
2021-12-29 11:38:35 -08:00
|
|
|
use time::{OffsetDateTime, macros::format_description};
|
2020-11-17 11:37:40 -08:00
|
|
|
|
2020-10-25 13:25:59 -07:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "user.html", escape = "none")]
|
|
|
|
struct UserTemplate {
|
|
|
|
user: User,
|
|
|
|
posts: Vec<Post>,
|
2020-12-29 17:11:47 -08:00
|
|
|
sort: (String, String),
|
2020-12-27 12:36:10 -08:00
|
|
|
ends: (String, String),
|
2021-01-08 17:35:04 -08:00
|
|
|
prefs: Preferences,
|
2021-05-10 01:25:52 +00:00
|
|
|
url: String,
|
2021-11-25 21:02:04 -07:00
|
|
|
/// Whether the user themself is filtered.
|
|
|
|
is_filtered: bool,
|
|
|
|
/// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place,
|
|
|
|
/// and all fetched posts being filtered).
|
|
|
|
all_posts_filtered: bool,
|
2020-10-25 13:25:59 -07:00
|
|
|
}
|
|
|
|
|
2021-01-02 20:50:23 -08:00
|
|
|
// FUNCTIONS
|
2021-03-17 15:30:33 -07:00
|
|
|
pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
|
2020-12-31 15:54:13 -08:00
|
|
|
// Build the Reddit JSON API path
|
2021-03-17 21:40:55 -07:00
|
|
|
let path = format!(
|
|
|
|
"/user/{}.json?{}&raw_json=1",
|
2021-03-26 20:00:47 -07:00
|
|
|
req.param("name").unwrap_or_else(|| "reddit".to_string()),
|
2021-03-17 21:40:55 -07:00
|
|
|
req.uri().query().unwrap_or_default()
|
|
|
|
);
|
2021-11-25 21:02:04 -07:00
|
|
|
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
|
2020-12-27 12:36:10 -08:00
|
|
|
|
2020-12-31 15:54:13 -08:00
|
|
|
// Retrieve other variables from Libreddit request
|
2021-05-16 16:53:39 +01:00
|
|
|
let sort = param(&path, "sort").unwrap_or_default();
|
2021-03-17 15:30:33 -07:00
|
|
|
let username = req.param("name").unwrap_or_default();
|
2021-11-25 21:02:04 -07:00
|
|
|
let user = user(&username).await.unwrap_or_default();
|
2020-11-20 22:05:27 -08:00
|
|
|
|
2021-11-25 21:02:04 -07:00
|
|
|
let filters = get_filters(&req);
|
|
|
|
if filters.contains(&["u_", &username].concat()) {
|
|
|
|
template(UserTemplate {
|
|
|
|
user,
|
|
|
|
posts: Vec::new(),
|
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
|
|
|
|
prefs: Preferences::new(req),
|
|
|
|
url,
|
|
|
|
is_filtered: true,
|
|
|
|
all_posts_filtered: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Request user posts/comments from Reddit
|
|
|
|
match Post::fetch(&path, false).await {
|
|
|
|
Ok((mut posts, after)) => {
|
|
|
|
let all_posts_filtered = filter_posts(&mut posts, &filters);
|
2021-01-15 15:05:55 -08:00
|
|
|
|
2021-11-25 21:02:04 -07:00
|
|
|
template(UserTemplate {
|
|
|
|
user,
|
|
|
|
posts,
|
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), after),
|
|
|
|
prefs: Preferences::new(req),
|
|
|
|
url,
|
|
|
|
is_filtered: false,
|
|
|
|
all_posts_filtered,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// If there is an error show error page
|
|
|
|
Err(msg) => error(req, msg).await,
|
2021-01-01 22:21:43 -08:00
|
|
|
}
|
2020-11-19 20:42:18 -08:00
|
|
|
}
|
2020-10-25 13:25:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// USER
|
2021-01-14 09:53:54 -08:00
|
|
|
async fn user(name: &str) -> Result<User, String> {
|
2020-12-31 20:21:56 -08:00
|
|
|
// Build the Reddit JSON API path
|
2021-02-09 09:38:52 -08:00
|
|
|
let path: String = format!("/user/{}/about.json?raw_json=1", name);
|
2020-12-31 21:03:44 -08:00
|
|
|
|
2021-01-01 15:28:13 -08:00
|
|
|
// Send a request to the url
|
2021-05-20 12:24:06 -07:00
|
|
|
json(path, false).await.map(|res| {
|
|
|
|
// Grab creation date as unix timestamp
|
2021-12-29 11:38:35 -08:00
|
|
|
let created_unix = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
|
|
|
|
let created = OffsetDateTime::from_unix_timestamp(created_unix).unwrap_or(OffsetDateTime::UNIX_EPOCH);
|
2021-01-13 19:53:52 -08:00
|
|
|
|
2021-05-20 12:24:06 -07:00
|
|
|
// Closure used to parse JSON from Reddit APIs
|
|
|
|
let about = |item| res["data"]["subreddit"][item].as_str().unwrap_or_default().to_string();
|
2021-01-08 17:35:04 -08:00
|
|
|
|
2021-05-20 12:24:06 -07:00
|
|
|
// Parse the JSON output into a User struct
|
|
|
|
User {
|
2021-11-14 17:45:18 -07:00
|
|
|
name: res["data"]["name"].as_str().unwrap_or(name).to_owned(),
|
2021-05-20 12:24:06 -07:00
|
|
|
title: esc!(about("title")),
|
|
|
|
icon: format_url(&about("icon_img")),
|
|
|
|
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
|
2021-12-29 12:48:57 -08:00
|
|
|
created: created.format(format_description!("[month repr:short] [day] '[year repr:last_two]")).unwrap_or_default(),
|
2021-05-20 12:24:06 -07:00
|
|
|
banner: esc!(about("banner_img")),
|
|
|
|
description: about("public_description"),
|
2021-01-01 22:21:43 -08:00
|
|
|
}
|
2021-05-20 12:24:06 -07:00
|
|
|
})
|
2020-11-29 18:50:29 -08:00
|
|
|
}
|