2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
|
|
|
use actix_web::{get, web, HttpResponse, Result};
|
|
|
|
use askama::Template;
|
|
|
|
|
2020-11-18 08:37:40 +13:00
|
|
|
#[path = "utils.rs"]
|
|
|
|
mod utils;
|
2020-11-21 19:05:27 +13:00
|
|
|
use utils::{nested_val, request, fetch_posts, ErrorTemplate, Params, Post, User};
|
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-10-26 16:57:19 +13:00
|
|
|
sort: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn render(username: String, sort: String) -> Result<HttpResponse> {
|
2020-11-21 19:05:27 +13:00
|
|
|
// Build the Reddit JSON API url
|
|
|
|
let url: String = format!("https://www.reddit.com/user/{}/.json?sort={}", username, sort);
|
|
|
|
|
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-21 17:43:20 +13:00
|
|
|
Ok(HttpResponse::Ok().status(actix_web::http::StatusCode::NOT_FOUND).content_type("text/html").body(s))
|
2020-11-20 17:42:18 +13:00
|
|
|
} else {
|
|
|
|
let s = UserTemplate {
|
|
|
|
user: user.unwrap(),
|
2020-11-21 19:05:27 +13:00
|
|
|
posts: posts.unwrap().0,
|
2020-11-20 17:42:18 +13:00
|
|
|
sort: sort,
|
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap();
|
|
|
|
Ok(HttpResponse::Ok().content_type("text/html").body(s))
|
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// SERVICES
|
|
|
|
#[get("/u/{username}")]
|
2020-11-18 13:03:28 +13:00
|
|
|
async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
|
|
|
|
match ¶ms.sort {
|
|
|
|
Some(sort) => render(username, sort.to_string()).await,
|
|
|
|
None => render(username, "hot".to_string()).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-10-26 09:25:59 +13:00
|
|
|
let url: String = format!("https://www.reddit.com/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-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-11-19 15:50:59 +13:00
|
|
|
icon: nested_val(&res, "subreddit", "icon_img").await,
|
|
|
|
karma: res["data"]["total_karma"].as_i64().unwrap(),
|
|
|
|
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-21 19:05:27 +13:00
|
|
|
}
|