2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2021-03-18 11:30:33 +13:00
|
|
|
use crate::client::json;
|
2021-03-12 17:15:26 +13:00
|
|
|
use crate::esc;
|
2021-03-18 11:30:33 +13:00
|
|
|
use crate::server::RequestExt;
|
|
|
|
use crate::utils::{error, format_url, param, template, Post, Preferences, User};
|
2020-10-26 09:25:59 +13:00
|
|
|
use askama::Template;
|
2021-03-18 11:30:33 +13:00
|
|
|
use hyper::{Body, Request, Response};
|
2021-01-06 17:01:21 +13:00
|
|
|
use time::OffsetDateTime;
|
2020-11-18 08:37:40 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "user.html", escape = "none")]
|
|
|
|
struct UserTemplate {
|
|
|
|
user: User,
|
|
|
|
posts: Vec<Post>,
|
2020-12-30 14:11:47 +13:00
|
|
|
sort: (String, String),
|
2020-12-28 09:36:10 +13:00
|
|
|
ends: (String, String),
|
2021-01-09 14:35:04 +13:00
|
|
|
prefs: Preferences,
|
2021-05-10 13:25:52 +12:00
|
|
|
url: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
// FUNCTIONS
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
|
2021-01-01 12:54:13 +13:00
|
|
|
// Build the Reddit JSON API path
|
2021-03-18 17:40:55 +13:00
|
|
|
let path = format!(
|
|
|
|
"/user/{}.json?{}&raw_json=1",
|
2021-03-27 16:00:47 +13:00
|
|
|
req.param("name").unwrap_or_else(|| "reddit".to_string()),
|
2021-03-18 17:40:55 +13:00
|
|
|
req.uri().query().unwrap_or_default()
|
|
|
|
);
|
2020-12-28 09:36:10 +13:00
|
|
|
|
2021-01-01 12:54:13 +13:00
|
|
|
// Retrieve other variables from Libreddit request
|
2021-05-17 03:53:39 +12:00
|
|
|
let sort = param(&path, "sort").unwrap_or_default();
|
2021-03-18 11:30:33 +13:00
|
|
|
let username = req.param("name").unwrap_or_default();
|
2020-11-21 19:05:27 +13:00
|
|
|
|
2021-01-16 12:05:55 +13:00
|
|
|
// Request user posts/comments from Reddit
|
2021-05-17 03:53:39 +12:00
|
|
|
let posts = Post::fetch(&path, "Comment".to_string(), false).await;
|
2021-05-10 13:25:52 +12:00
|
|
|
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
|
2021-01-02 19:21:43 +13:00
|
|
|
|
2021-01-02 12:28:13 +13:00
|
|
|
match posts {
|
2021-01-07 18:27:24 +13:00
|
|
|
Ok((posts, after)) => {
|
2021-01-16 12:05:55 +13:00
|
|
|
// If you can get user posts, also request user data
|
|
|
|
let user = user(&username).await.unwrap_or_default();
|
|
|
|
|
2021-02-10 06:38:52 +13:00
|
|
|
template(UserTemplate {
|
2021-01-09 18:57:36 +13:00
|
|
|
user,
|
2021-01-07 18:27:24 +13:00
|
|
|
posts,
|
2021-05-17 03:53:39 +12:00
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), after),
|
2021-02-25 18:29:23 +13:00
|
|
|
prefs: Preferences::new(req),
|
2021-05-10 13:25:52 +12:00
|
|
|
url,
|
2021-02-10 06:38:52 +13:00
|
|
|
})
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2021-01-02 12:28:13 +13:00
|
|
|
// If there is an error show error page
|
2021-02-21 15:36:30 +13:00
|
|
|
Err(msg) => error(req, msg).await,
|
2020-11-20 17:42:18 +13:00
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// USER
|
2021-01-15 06:53:54 +13:00
|
|
|
async fn user(name: &str) -> Result<User, String> {
|
2021-01-01 17:21:56 +13:00
|
|
|
// Build the Reddit JSON API path
|
2021-02-10 06:38:52 +13:00
|
|
|
let path: String = format!("/user/{}/about.json?raw_json=1", name);
|
2021-01-01 18:03:44 +13:00
|
|
|
|
2021-01-02 12:28:13 +13:00
|
|
|
// Send a request to the url
|
2021-05-21 07:24:06 +12:00
|
|
|
json(path, false).await.map(|res| {
|
|
|
|
// Grab creation date as unix timestamp
|
|
|
|
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
|
2021-01-14 16:53:52 +13:00
|
|
|
|
2021-05-21 07:24:06 +12: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-09 14:35:04 +13:00
|
|
|
|
2021-05-21 07:24:06 +12:00
|
|
|
// Parse the JSON output into a User struct
|
|
|
|
User {
|
|
|
|
name: name.to_string(),
|
|
|
|
title: esc!(about("title")),
|
|
|
|
icon: format_url(&about("icon_img")),
|
|
|
|
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
|
|
|
|
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
|
|
|
|
banner: esc!(about("banner_img")),
|
|
|
|
description: about("public_description"),
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2021-05-21 07:24:06 +12:00
|
|
|
})
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|