libreddit/src/user.rs

82 lines
2.6 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// CRATES
2021-01-01 06:03:44 +01:00
use crate::utils::{error, fetch_posts, format_url, nested_val, param, request, Post, User};
use actix_web::{HttpRequest, 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-12-30 02:11:47 +01:00
sort: (String, String),
2020-12-27 21:36:10 +01:00
ends: (String, String),
2020-10-25 21:25:59 +01:00
}
2021-01-01 00:54:13 +01:00
pub async fn profile(req: HttpRequest) -> Result<HttpResponse> {
// Build the Reddit JSON API path
let path = format!("{}.json?{}&raw_json=1", req.path(), req.query_string());
2020-12-27 21:36:10 +01:00
2021-01-01 00:54:13 +01:00
// Retrieve other variables from Libreddit request
let sort = param(&path, "sort").await;
let username = req.match_info().get("username").unwrap_or("").to_string();
2020-11-21 07:05:27 +01:00
2021-01-01 00:54:13 +01:00
// Request user profile data and user posts/comments from Reddit
2020-11-20 05:42:18 +01:00
let user = user(&username).await;
2021-01-01 00:54:13 +01:00
let posts = fetch_posts(path.clone(), "Comment".to_string()).await;
2020-10-26 04:57:19 +01:00
2021-01-01 00:54:13 +01:00
// If there is an error show error page
2020-11-20 05:42:18 +01:00
if user.is_err() || posts.is_err() {
2021-01-01 06:03:44 +01:00
error(user.err().unwrap().to_string()).await
2020-11-20 05:42:18 +01:00
} else {
2020-12-27 21:36:10 +01:00
let posts_unwrapped = posts.unwrap();
2021-01-01 00:54:13 +01:00
2020-11-20 05:42:18 +01:00
let s = UserTemplate {
user: user.unwrap(),
2020-12-27 21:36:10 +01:00
posts: posts_unwrapped.0,
2021-01-01 00:54:13 +01:00
sort: (sort, param(&path, "t").await),
ends: (param(&path, "after").await, posts_unwrapped.1),
2020-11-20 05:42:18 +01:00
}
.render()
.unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2020-10-25 21:25:59 +01:00
}
// SERVICES
2021-01-01 00:54:13 +01:00
// pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
// render(username, params.sort.clone(), params.t.clone(), (params.before.clone(), params.after.clone())).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> {
// Build the Reddit JSON API path
let path: String = format!("user/{}/about.json", name);
2021-01-01 06:03:44 +01:00
2020-11-19 03:50:59 +01:00
// Send a request to the url, receive JSON in response
let req = request(path).await;
2021-01-01 06:03:44 +01:00
2020-11-20 05:42:18 +01:00
// If the Reddit API returns an error, exit this function
if req.is_err() {
return Err(req.err().unwrap());
}
2021-01-01 06:03:44 +01:00
2020-11-20 05:42:18 +01:00
// 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(),
title: nested_val(&res, "subreddit", "title").await,
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
}