libreddit/src/user.rs

79 lines
2.3 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// CRATES
2020-12-21 23:12:53 +01:00
use crate::utils::{fetch_posts, format_url, nested_val, request, ErrorTemplate, Params, Post, User};
2020-12-15 01:35:04 +01:00
use actix_web::{http::StatusCode, web, HttpResponse, Result};
2020-10-25 21:25:59 +01:00
use askama::Template;
2020-12-24 07:16:04 +01:00
use chrono::{TimeZone, Utc};
2020-11-17 20:37:40 +01:00
2020-10-25 21:25:59 +01:00
// STRUCTS
#[derive(Template)]
#[template(path = "user.html", escape = "none")]
struct UserTemplate {
user: User,
posts: Vec<Post>,
2020-10-26 04:57:19 +01:00
sort: String,
2020-10-25 21:25:59 +01:00
}
async fn render(username: String, sort: String) -> Result<HttpResponse> {
2020-11-21 07:05:27 +01:00
// Build the Reddit JSON API url
2020-12-22 02:17:40 +01:00
let url: String = format!("user/{}/.json?sort={}", username, sort);
2020-11-21 07:05:27 +01:00
2020-11-20 05:42:18 +01:00
let user = user(&username).await;
2020-11-21 07:05:27 +01:00
let posts = fetch_posts(url, "Comment".to_string()).await;
2020-10-26 04:57:19 +01:00
2020-11-20 05:42:18 +01:00
if user.is_err() || posts.is_err() {
let s = ErrorTemplate {
message: user.err().unwrap().to_string(),
}
.render()
.unwrap();
2020-11-25 22:53:30 +01:00
Ok(HttpResponse::Ok().status(StatusCode::NOT_FOUND).content_type("text/html").body(s))
2020-11-20 05:42:18 +01:00
} else {
let s = UserTemplate {
user: user.unwrap(),
2020-11-21 07:05:27 +01:00
posts: posts.unwrap().0,
2020-11-20 05:42:18 +01:00
sort: sort,
}
.render()
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2020-10-25 21:25:59 +01:00
}
// SERVICES
2020-12-15 01:35:04 +01:00
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
2020-11-18 01:03:28 +01:00
match &params.sort {
Some(sort) => render(username, sort.to_string()).await,
None => render(username, "hot".to_string()).await,
}
2020-10-25 21:25:59 +01:00
}
// USER
2020-11-20 05:42:18 +01:00
async fn user(name: &String) -> Result<User, &'static str> {
2020-11-19 03:50:59 +01:00
// Build the Reddit JSON API url
2020-12-22 02:17:40 +01:00
let url: String = format!("user/{}/about.json", name);
2020-11-19 22:49:32 +01:00
2020-11-19 03:50:59 +01:00
// Send a request to the url, receive JSON in response
2020-11-20 05:42:18 +01: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 04:57:19 +01:00
2020-12-24 07:16:04 +01:00
// Grab creation date as unix timestamp
let created: i64 = res["data"]["created"].as_f64().unwrap().round() as i64;
2020-11-20 05:42:18 +01:00
// Parse the JSON output into a User struct
Ok(User {
2020-10-25 21:25:59 +01:00
name: name.to_string(),
2020-12-24 05:36:49 +01:00
icon: format_url(nested_val(&res, "subreddit", "icon_img").await).await,
2020-11-19 03:50:59 +01:00
karma: res["data"]["total_karma"].as_i64().unwrap(),
2020-12-24 07:16:04 +01:00
created: Utc.timestamp(created, 0).format("%b %e, %Y").to_string(),
2020-11-19 03:50:59 +01:00
banner: nested_val(&res, "subreddit", "banner_img").await,
description: nested_val(&res, "subreddit", "public_description").await,
2020-11-20 05:42:18 +01:00
})
2020-11-30 03:50:29 +01:00
}