redsunlib/src/user.rs

98 lines
2.8 KiB
Rust
Raw Normal View History

2020-10-26 09:25:59 +13:00
// CRATES
use actix_web::{get, web, HttpResponse, Result};
use askama::Template;
use chrono::{TimeZone, Utc};
2020-11-18 08:37:40 +13:00
#[path = "utils.rs"]
mod utils;
2020-11-19 15:50:59 +13:00
use utils::{nested_val, val, Flair, Params, Post, User, request};
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> {
let user: User = user(&username).await;
let posts: Vec<Post> = posts(username, &sort).await;
2020-10-26 16:57:19 +13:00
let s = UserTemplate { user: user, posts: posts, sort: sort }.render().unwrap();
2020-10-26 09:25:59 +13:00
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
// 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 &params.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
async fn user(name: &String) -> User {
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-19 15:50:59 +13:00
// Send a request to the url, receive JSON in response
let res = request(url).await;
2020-10-26 16:57:19 +13:00
2020-10-26 09:25:59 +13:00
User {
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-10-26 09:25:59 +13:00
}
}
// POSTS
async fn posts(sub: String, sort: &String) -> Vec<Post> {
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/u/{}/.json?sort={}", sub, sort);
2020-10-26 16:57:19 +13:00
2020-11-19 15:50:59 +13:00
// Send a request to the url, receive JSON in response
let res = request(url).await;
let post_list = res["data"]["children"].as_array().unwrap();
2020-10-26 09:25:59 +13:00
let mut posts: Vec<Post> = Vec::new();
2020-10-26 16:57:19 +13:00
2020-10-26 09:25:59 +13:00
for post in post_list.iter() {
2020-11-18 08:37:40 +13:00
let img = if val(post, "thumbnail").await.starts_with("https:/") {
val(post, "thumbnail").await
2020-10-26 16:57:19 +13:00
} else {
String::new()
};
2020-10-26 09:25:59 +13:00
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap().round() as i64;
let score = post["data"]["score"].as_i64().unwrap();
2020-11-18 08:37:40 +13:00
let title = val(post, "title").await;
2020-10-26 09:25:59 +13:00
posts.push(Post {
2020-11-19 13:31:46 +13:00
title: if title.is_empty() { "Comment".to_string() } else { title },
2020-11-18 08:37:40 +13:00
community: val(post, "subreddit").await,
body: String::new(),
author: val(post, "author").await,
2020-10-26 16:57:19 +13:00
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
2020-11-18 08:37:40 +13:00
media: img,
url: val(post, "permalink").await,
2020-10-26 16:57:19 +13:00
time: Utc.timestamp(unix_time, 0).format("%b %e '%y").to_string(),
2020-11-17 15:49:08 +13:00
flair: Flair(
2020-11-18 08:37:40 +13:00
val(post, "link_flair_text").await,
val(post, "link_flair_background_color").await,
if val(post, "link_flair_text_color").await == "dark" {
2020-11-17 17:36:36 +13:00
"black".to_string()
} else {
"white".to_string()
},
2020-11-17 15:49:08 +13:00
),
2020-10-26 09:25:59 +13:00
});
}
posts
2020-10-26 16:57:19 +13:00
}