2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2021-03-18 11:30:33 +13:00
|
|
|
use crate::client::json;
|
|
|
|
use crate::server::RequestExt;
|
2023-01-03 22:39:45 +13:00
|
|
|
use crate::utils::{error, filter_posts, format_url, get_filters, nsfw_landing, param, setting, template, Post, Preferences, User};
|
2020-10-26 09:25:59 +13:00
|
|
|
use askama::Template;
|
2021-03-18 11:30:33 +13:00
|
|
|
use hyper::{Body, Request, Response};
|
2022-05-21 14:20:44 +12:00
|
|
|
use time::{macros::format_description, OffsetDateTime};
|
2020-11-18 08:37:40 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
2022-05-21 17:28:31 +12:00
|
|
|
#[template(path = "user.html")]
|
2020-10-26 09:25:59 +13:00
|
|
|
struct UserTemplate {
|
|
|
|
user: User,
|
|
|
|
posts: Vec<Post>,
|
2020-12-30 14:11:47 +13:00
|
|
|
sort: (String, String),
|
2020-12-28 09:36:10 +13:00
|
|
|
ends: (String, String),
|
2022-03-14 08:06:27 +13:00
|
|
|
/// "overview", "comments", or "submitted"
|
|
|
|
listing: String,
|
2021-01-09 14:35:04 +13:00
|
|
|
prefs: Preferences,
|
2021-05-10 13:25:52 +12:00
|
|
|
url: String,
|
2022-03-14 08:06:27 +13:00
|
|
|
redirect_url: String,
|
2021-11-26 17:02:04 +13:00
|
|
|
/// Whether the user themself is filtered.
|
|
|
|
is_filtered: bool,
|
|
|
|
/// Whether all fetched posts are filtered (to differentiate between no posts fetched in the first place,
|
|
|
|
/// and all fetched posts being filtered).
|
|
|
|
all_posts_filtered: bool,
|
2022-11-08 16:54:49 +13:00
|
|
|
/// Whether all posts were hidden because they are NSFW (and user has disabled show NSFW)
|
|
|
|
all_posts_hidden_nsfw: bool,
|
2023-01-01 15:11:59 +13:00
|
|
|
no_posts: bool,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
// FUNCTIONS
|
2021-03-18 11:30:33 +13:00
|
|
|
pub async fn profile(req: Request<Body>) -> Result<Response<Body>, String> {
|
2022-03-14 08:06:27 +13:00
|
|
|
let listing = req.param("listing").unwrap_or_else(|| "overview".to_string());
|
|
|
|
|
2021-01-01 12:54:13 +13:00
|
|
|
// Build the Reddit JSON API path
|
2021-03-18 17:40:55 +13:00
|
|
|
let path = format!(
|
2022-03-14 08:06:27 +13:00
|
|
|
"/user/{}/{}.json?{}&raw_json=1",
|
2021-03-27 16:00:47 +13:00
|
|
|
req.param("name").unwrap_or_else(|| "reddit".to_string()),
|
2022-03-14 08:06:27 +13:00
|
|
|
listing,
|
|
|
|
req.uri().query().unwrap_or_default(),
|
2021-03-18 17:40:55 +13:00
|
|
|
);
|
2021-11-26 17:02:04 +13:00
|
|
|
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
|
2022-03-14 08:06:27 +13:00
|
|
|
let redirect_url = url[1..].replace('?', "%3F").replace('&', "%26");
|
2020-12-28 09:36:10 +13:00
|
|
|
|
2021-01-01 12:54:13 +13:00
|
|
|
// Retrieve other variables from Libreddit request
|
2021-05-17 03:53:39 +12:00
|
|
|
let sort = param(&path, "sort").unwrap_or_default();
|
2021-03-18 11:30:33 +13:00
|
|
|
let username = req.param("name").unwrap_or_default();
|
2023-01-03 22:39:45 +13:00
|
|
|
|
|
|
|
// Retrieve info from user about page.
|
2021-11-26 17:02:04 +13:00
|
|
|
let user = user(&username).await.unwrap_or_default();
|
2020-11-21 19:05:27 +13:00
|
|
|
|
2023-03-24 07:09:33 +13:00
|
|
|
let req_url = req.uri().to_string();
|
2023-01-03 22:39:45 +13:00
|
|
|
// Return landing page if this post if this Reddit deems this user NSFW,
|
|
|
|
// but we have also disabled the display of NSFW content or if the instance
|
|
|
|
// is SFW-only.
|
2023-03-23 19:18:35 +13:00
|
|
|
if user.nsfw && crate::utils::should_be_nsfw_gated(&req, &req_url) {
|
|
|
|
return Ok(nsfw_landing(req, req_url).await.unwrap_or_default());
|
2023-01-03 22:39:45 +13:00
|
|
|
}
|
|
|
|
|
2021-11-26 17:02:04 +13:00
|
|
|
let filters = get_filters(&req);
|
|
|
|
if filters.contains(&["u_", &username].concat()) {
|
|
|
|
template(UserTemplate {
|
|
|
|
user,
|
|
|
|
posts: Vec::new(),
|
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), "".to_string()),
|
2022-03-14 08:06:27 +13:00
|
|
|
listing,
|
2023-01-02 15:39:38 +13:00
|
|
|
prefs: Preferences::new(&req),
|
2021-11-26 17:02:04 +13:00
|
|
|
url,
|
2022-03-14 08:06:27 +13:00
|
|
|
redirect_url,
|
2021-11-26 17:02:04 +13:00
|
|
|
is_filtered: true,
|
|
|
|
all_posts_filtered: false,
|
2022-11-08 16:54:49 +13:00
|
|
|
all_posts_hidden_nsfw: false,
|
2023-01-01 15:11:59 +13:00
|
|
|
no_posts: false,
|
2021-11-26 17:02:04 +13:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Request user posts/comments from Reddit
|
|
|
|
match Post::fetch(&path, false).await {
|
|
|
|
Ok((mut posts, after)) => {
|
2022-11-10 05:16:51 +13:00
|
|
|
let (_, all_posts_filtered) = filter_posts(&mut posts, &filters);
|
2023-01-01 15:11:59 +13:00
|
|
|
let no_posts = posts.is_empty();
|
|
|
|
let all_posts_hidden_nsfw = !no_posts && (posts.iter().all(|p| p.flags.nsfw) && setting(&req, "show_nsfw") != "on");
|
2021-11-26 17:02:04 +13:00
|
|
|
template(UserTemplate {
|
|
|
|
user,
|
|
|
|
posts,
|
|
|
|
sort: (sort, param(&path, "t").unwrap_or_default()),
|
|
|
|
ends: (param(&path, "after").unwrap_or_default(), after),
|
2022-03-14 08:06:27 +13:00
|
|
|
listing,
|
2023-01-02 15:39:38 +13:00
|
|
|
prefs: Preferences::new(&req),
|
2021-11-26 17:02:04 +13:00
|
|
|
url,
|
2022-03-14 08:06:27 +13:00
|
|
|
redirect_url,
|
2021-11-26 17:02:04 +13:00
|
|
|
is_filtered: false,
|
|
|
|
all_posts_filtered,
|
2022-11-08 16:54:49 +13:00
|
|
|
all_posts_hidden_nsfw,
|
2023-01-01 15:11:59 +13:00
|
|
|
no_posts,
|
2021-11-26 17:02:04 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
// If there is an error show error page
|
|
|
|
Err(msg) => error(req, msg).await,
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2020-11-20 17:42:18 +13:00
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// USER
|
2021-01-15 06:53:54 +13:00
|
|
|
async fn user(name: &str) -> Result<User, String> {
|
2021-01-01 17:21:56 +13:00
|
|
|
// Build the Reddit JSON API path
|
2021-02-10 06:38:52 +13:00
|
|
|
let path: String = format!("/user/{}/about.json?raw_json=1", name);
|
2021-01-01 18:03:44 +13:00
|
|
|
|
2021-01-02 12:28:13 +13:00
|
|
|
// Send a request to the url
|
2021-05-21 07:24:06 +12:00
|
|
|
json(path, false).await.map(|res| {
|
|
|
|
// Grab creation date as unix timestamp
|
2021-12-30 08:38:35 +13:00
|
|
|
let created_unix = res["data"]["created"].as_f64().unwrap_or(0.0).round() as i64;
|
|
|
|
let created = OffsetDateTime::from_unix_timestamp(created_unix).unwrap_or(OffsetDateTime::UNIX_EPOCH);
|
2021-01-14 16:53:52 +13:00
|
|
|
|
2021-05-21 07:24:06 +12:00
|
|
|
// Closure used to parse JSON from Reddit APIs
|
|
|
|
let about = |item| res["data"]["subreddit"][item].as_str().unwrap_or_default().to_string();
|
2021-01-09 14:35:04 +13:00
|
|
|
|
2021-05-21 07:24:06 +12:00
|
|
|
// Parse the JSON output into a User struct
|
|
|
|
User {
|
2021-11-15 13:45:18 +13:00
|
|
|
name: res["data"]["name"].as_str().unwrap_or(name).to_owned(),
|
2022-05-21 17:28:31 +12:00
|
|
|
title: about("title"),
|
2021-05-21 07:24:06 +12:00
|
|
|
icon: format_url(&about("icon_img")),
|
|
|
|
karma: res["data"]["total_karma"].as_i64().unwrap_or(0),
|
2021-12-30 09:48:57 +13:00
|
|
|
created: created.format(format_description!("[month repr:short] [day] '[year repr:last_two]")).unwrap_or_default(),
|
2022-05-21 17:28:31 +12:00
|
|
|
banner: about("banner_img"),
|
2021-05-21 07:24:06 +12:00
|
|
|
description: about("public_description"),
|
2023-01-03 22:39:45 +13:00
|
|
|
nsfw: res["data"]["subreddit"]["over_18"].as_bool().unwrap_or_default(),
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2021-05-21 07:24:06 +12:00
|
|
|
})
|
2020-11-30 15:50:29 +13:00
|
|
|
}
|
2023-06-07 07:28:36 +12:00
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn test_fetching_user() {
|
|
|
|
let user = user("spez").await;
|
|
|
|
assert!(user.is_ok());
|
|
|
|
assert!(user.unwrap().karma > 100);
|
|
|
|
}
|