libreddit/src/user.rs

85 lines
2.4 KiB
Rust
Raw Normal View History

2020-10-25 21:25:59 +01:00
// CRATES
2021-03-17 23:30:33 +01:00
use crate::client::json;
2021-03-12 05:15:26 +01:00
use crate::esc;
2021-03-17 23:30:33 +01:00
use crate::server::RequestExt;
use crate::utils::{error, format_url, param, template, Post, Preferences, User};
2020-10-25 21:25:59 +01:00
use askama::Template;
2021-03-17 23:30:33 +01:00
use hyper::{Body, Request, Response};
2021-01-06 05:01:21 +01:00
use time::OffsetDateTime;
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),
2021-01-09 02:35:04 +01:00
prefs: Preferences,
2020-10-25 21:25:59 +01:00
}
2021-01-03 05:50:23 +01:00
// FUNCTIONS
2021-03-17 23:30:33 +01:00
pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
2021-01-01 00:54:13 +01:00
// Build the Reddit JSON API path
2021-03-18 05:40:55 +01:00
let path = format!(
"/user/{}.json?{}&raw_json=1",
2021-03-27 04:00:47 +01:00
req.param("name").unwrap_or_else(|| "reddit".to_string()),
2021-03-18 05:40:55 +01:00
req.uri().query().unwrap_or_default()
);
2020-12-27 21:36:10 +01:00
2021-01-01 00:54:13 +01:00
// Retrieve other variables from Libreddit request
2021-01-01 21:33:57 +01:00
let sort = param(&path, "sort");
2021-03-17 23:30:33 +01:00
let username = req.param("name").unwrap_or_default();
2020-11-21 07:05:27 +01:00
2021-01-16 00:05:55 +01:00
// Request user posts/comments from Reddit
2021-02-25 06:29:23 +01:00
let posts = Post::fetch(&path, "Comment".to_string()).await;
2021-01-02 07:21:43 +01:00
2021-01-02 00:28:13 +01:00
match posts {
2021-01-07 06:27:24 +01:00
Ok((posts, after)) => {
2021-01-16 00:05:55 +01:00
// If you can get user posts, also request user data
let user = user(&username).await.unwrap_or_default();
template(UserTemplate {
user,
2021-01-07 06:27:24 +01:00
posts,
2021-01-02 00:28:13 +01:00
sort: (sort, param(&path, "t")),
2021-01-07 06:27:24 +01:00
ends: (param(&path, "after"), after),
2021-02-25 06:29:23 +01:00
prefs: Preferences::new(req),
})
2021-01-02 07:21:43 +01:00
}
2021-01-02 00:28:13 +01:00
// If there is an error show error page
2021-02-21 03:36:30 +01:00
Err(msg) => error(req, msg).await,
2020-11-20 05:42:18 +01:00
}
2020-10-25 21:25:59 +01:00
}
// USER
2021-01-14 18:53:54 +01:00
async fn user(name: &str) -> Result<User, String> {
// Build the Reddit JSON API path
let path: String = format!("/user/{}/about.json?raw_json=1", name);
2021-01-01 06:03:44 +01:00
2021-01-02 00:28:13 +01:00
// Send a request to the url
2021-03-17 23:30:33 +01:00
match json(path).await {
2021-01-02 00:28:13 +01:00
// If success, receive JSON in response
2021-01-09 02:35:04 +01:00
Ok(res) => {
// Grab creation date as unix timestamp
2021-03-09 16:22:17 +01:00
let created: i64 = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
2021-03-12 05:15:26 +01:00
// Closure used to parse JSON from Reddit APIs
2021-01-14 00:55:10 +01:00
let about = |item| res["data"]["subreddit"][item].as_str().unwrap_or_default().to_string();
2021-01-09 02:35:04 +01:00
// Parse the JSON output into a User struct
Ok(User {
name: name.to_string(),
2021-03-12 05:15:26 +01:00
title: esc!(about("title")),
2021-02-20 22:59:16 +01:00
icon: format_url(&about("icon_img")),
2021-01-09 02:35:04 +01:00
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
created: OffsetDateTime::from_unix_timestamp(created).format("%b %d '%y"),
2021-03-12 05:15:26 +01:00
banner: esc!(about("banner_img")),
2021-01-14 00:55:10 +01:00
description: about("public_description"),
2021-01-09 02:35:04 +01:00
})
2021-01-02 07:21:43 +01:00
}
2021-01-02 00:28:13 +01:00
// If the Reddit API returns an error, exit this function
2021-01-02 07:21:43 +01:00
Err(msg) => return Err(msg),
2020-11-20 05:42:18 +01:00
}
2020-11-30 03:50:29 +01:00
}