2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2020-12-22 11:12:53 +13:00
|
|
|
use crate::utils::{fetch_posts, format_url, nested_val, request, ErrorTemplate, Params, Post, User};
|
2020-12-15 13:35:04 +13:00
|
|
|
use actix_web::{http::StatusCode, web, HttpResponse, Result};
|
2020-10-26 09:25:59 +13:00
|
|
|
use askama::Template;
|
2020-12-24 19:16:04 +13:00
|
|
|
use chrono::{TimeZone, Utc};
|
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),
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2020-12-30 14:11:47 +13:00
|
|
|
async fn render(username: String, sort: Option<String>, t: Option<String>, ends: (Option<String>, Option<String>)) -> Result<HttpResponse> {
|
2020-12-28 09:36:10 +13:00
|
|
|
let sorting = sort.unwrap_or("new".to_string());
|
|
|
|
|
|
|
|
let before = ends.1.clone().unwrap_or(String::new()); // If there is an after, there must be a before
|
|
|
|
|
2020-12-30 14:11:47 +13:00
|
|
|
let timeframe = match &t { Some(val) => format!("&t={}", val), None => String::new() };
|
|
|
|
|
2020-11-21 19:05:27 +13:00
|
|
|
// Build the Reddit JSON API url
|
2020-12-28 09:36:10 +13:00
|
|
|
let url = match ends.0 {
|
2020-12-30 14:11:47 +13:00
|
|
|
Some(val) => format!("user/{}/.json?sort={}&before={}&count=25&raw_json=1{}", username, sorting, val, timeframe),
|
2020-12-28 09:36:10 +13:00
|
|
|
None => match ends.1 {
|
2020-12-30 14:11:47 +13:00
|
|
|
Some(val) => format!("user/{}/.json?sort={}&after={}&count=25&raw_json=1{}", username, sorting, val, timeframe),
|
|
|
|
None => format!("user/{}/.json?sort={}&raw_json=1{}", username, sorting, timeframe),
|
2020-12-28 09:36:10 +13:00
|
|
|
},
|
|
|
|
};
|
2020-11-21 19:05:27 +13:00
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
let user = user(&username).await;
|
2020-11-21 19:05:27 +13:00
|
|
|
let posts = fetch_posts(url, "Comment".to_string()).await;
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
if user.is_err() || posts.is_err() {
|
|
|
|
let s = ErrorTemplate {
|
|
|
|
message: user.err().unwrap().to_string(),
|
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap();
|
2020-11-26 10:53:30 +13:00
|
|
|
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
2020-11-20 17:42:18 +13:00
|
|
|
} else {
|
2020-12-28 09:36:10 +13:00
|
|
|
let posts_unwrapped = posts.unwrap();
|
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
let s = UserTemplate {
|
|
|
|
user: user.unwrap(),
|
2020-12-28 09:36:10 +13:00
|
|
|
posts: posts_unwrapped.0,
|
2020-12-30 14:11:47 +13:00
|
|
|
sort: (sorting, t.unwrap_or(String::new())),
|
2020-12-28 09:36:10 +13:00
|
|
|
ends: (before, posts_unwrapped.1)
|
2020-11-20 17:42:18 +13:00
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap();
|
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
2020-12-15 13:35:04 +13:00
|
|
|
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
|
2020-12-30 14:11:47 +13:00
|
|
|
render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).await
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// USER
|
2020-11-20 17:42:18 +13:00
|
|
|
async fn user(name: &String) -> Result<User, &'static str> {
|
2020-11-19 15:50:59 +13:00
|
|
|
// Build the Reddit JSON API url
|
2020-12-22 14:17:40 +13:00
|
|
|
let url: String = format!("user/{}/about.json", name);
|
2020-11-20 10:49:32 +13:00
|
|
|
|
2020-11-19 15:50:59 +13:00
|
|
|
// Send a request to the url, receive JSON in response
|
2020-11-20 17:42:18 +13:00
|
|
|
let req = request(url).await;
|
|
|
|
|
|
|
|
// If the Reddit API returns an error, exit this function
|
|
|
|
if req.is_err() {
|
|
|
|
return Err(req.err().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, grab the JSON output from the request
|
|
|
|
let res = req.unwrap();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-12-24 19:16:04 +13:00
|
|
|
// Grab creation date as unix timestamp
|
|
|
|
let created: i64 = res["data"]["created"].as_f64().unwrap().round() as i64;
|
|
|
|
|
2020-11-20 17:42:18 +13:00
|
|
|
// Parse the JSON output into a User struct
|
|
|
|
Ok(User {
|
2020-10-26 09:25:59 +13:00
|
|
|
name: name.to_string(),
|
2020-12-24 17:36:49 +13:00
|
|
|
icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await,
|
2020-11-19 15:50:59 +13:00
|
|
|
karma: res["data"]["total_karma"].as_i64().unwrap(),
|
2020-12-24 19:16:04 +13:00
|
|
|
created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(),
|
2020-11-19 15:50:59 +13:00
|
|
|
banner: nested_val(&res, "subreddit", "banner_img").await,
|
|
|
|
description: nested_val(&res, "subreddit", "public_description").await,
|
2020-11-20 17:42:18 +13:00
|
|
|
})
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|