2020-10-26 09:25:59 +13:00
|
|
|
// CRATES
|
2021-01-06 17:01:21 +13:00
|
|
|
use crate::utils::{cookie, error, format_num, format_url, media, param, request, rewrite_url, val, Comment, Flags, Flair, Post};
|
2021-01-07 18:27:24 +13:00
|
|
|
use actix_web::{HttpRequest, HttpResponse};
|
2020-12-15 13:35:04 +13:00
|
|
|
|
2020-12-20 16:54:46 +13:00
|
|
|
use async_recursion::async_recursion;
|
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
use askama::Template;
|
2021-01-06 17:01:21 +13:00
|
|
|
use time::OffsetDateTime;
|
2020-11-30 15:50:29 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
// STRUCTS
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "post.html", escape = "none")]
|
|
|
|
struct PostTemplate {
|
|
|
|
comments: Vec<Comment>,
|
|
|
|
post: Post,
|
2020-10-26 16:57:19 +13:00
|
|
|
sort: String,
|
2021-01-06 15:04:49 +13:00
|
|
|
layout: String,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
2021-01-03 17:50:23 +13:00
|
|
|
pub async fn item(req: HttpRequest) -> HttpResponse {
|
2021-01-01 12:54:13 +13:00
|
|
|
let path = format!("{}.json?{}&raw_json=1", req.path(), req.query_string());
|
2021-01-02 09:33:57 +13:00
|
|
|
let sort = param(&path, "sort");
|
2021-01-01 12:54:13 +13:00
|
|
|
|
2020-12-22 18:40:06 +13:00
|
|
|
// Log the post ID being fetched in debug mode
|
|
|
|
#[cfg(debug_assertions)]
|
2021-01-03 19:46:02 +13:00
|
|
|
dbg!(req.match_info().get("id").unwrap_or(""));
|
2021-01-02 19:21:43 +13:00
|
|
|
|
2020-11-21 18:04:35 +13:00
|
|
|
// Send a request to the url, receive JSON in response
|
2021-01-02 12:28:13 +13:00
|
|
|
match request(&path).await {
|
2021-01-01 12:54:13 +13:00
|
|
|
// Otherwise, grab the JSON output from the request
|
2021-01-02 12:28:13 +13:00
|
|
|
Ok(res) => {
|
|
|
|
// Parse the JSON into Post and Comment structs
|
2021-01-07 18:27:24 +13:00
|
|
|
let post = parse_post(&res[0]).await;
|
|
|
|
let comments = parse_comments(&res[1]).await;
|
2021-01-02 12:28:13 +13:00
|
|
|
|
|
|
|
// Use the Post and Comment structs to generate a website to show users
|
2021-01-06 15:04:49 +13:00
|
|
|
let s = PostTemplate {
|
|
|
|
comments,
|
|
|
|
post,
|
|
|
|
sort,
|
|
|
|
layout: cookie(req, "layout"),
|
|
|
|
}
|
|
|
|
.render()
|
|
|
|
.unwrap();
|
2021-01-03 17:50:23 +13:00
|
|
|
HttpResponse::Ok().content_type("text/html").body(s)
|
2021-01-02 19:21:43 +13:00
|
|
|
}
|
2021-01-02 12:28:13 +13:00
|
|
|
// If the Reddit API returns an error, exit and send error page to user
|
2021-01-02 19:21:43 +13:00
|
|
|
Err(msg) => error(msg.to_string()).await,
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:52:57 +13:00
|
|
|
// POSTS
|
2021-01-07 18:27:24 +13:00
|
|
|
async fn parse_post(json: &serde_json::Value) -> Post {
|
2020-12-23 15:29:43 +13:00
|
|
|
// Retrieve post (as opposed to comments) from JSON
|
2021-01-04 10:06:49 +13:00
|
|
|
let post: &serde_json::Value = &json["data"]["children"][0];
|
2020-10-26 09:25:59 +13:00
|
|
|
|
2020-12-23 15:29:43 +13:00
|
|
|
// Grab UTC time as unix timestamp
|
2021-01-04 18:31:21 +13:00
|
|
|
let unix_time: i64 = post["data"]["created_utc"].as_f64().unwrap_or_default().round() as i64;
|
2021-01-04 10:06:49 +13:00
|
|
|
// Parse post score and upvote ratio
|
2021-01-04 18:31:21 +13:00
|
|
|
let score = post["data"]["score"].as_i64().unwrap_or_default();
|
2021-01-04 10:06:49 +13:00
|
|
|
let ratio: f64 = post["data"]["upvote_ratio"].as_f64().unwrap_or(1.0) * 100.0;
|
2020-10-26 09:25:59 +13:00
|
|
|
|
2020-12-23 15:29:43 +13:00
|
|
|
// Determine the type of media along with the media URL
|
2021-01-07 18:27:24 +13:00
|
|
|
let (post_type, media) = media(&post["data"]).await;
|
2020-12-01 17:33:55 +13:00
|
|
|
|
2020-12-23 15:29:43 +13:00
|
|
|
// Build a post using data parsed from Reddit post API
|
2021-01-07 18:27:24 +13:00
|
|
|
Post {
|
2021-01-04 10:06:49 +13:00
|
|
|
id: val(post, "id"),
|
|
|
|
title: val(post, "title"),
|
|
|
|
community: val(post, "subreddit"),
|
|
|
|
body: rewrite_url(&val(post, "selftext_html")),
|
|
|
|
author: val(post, "author"),
|
2020-12-21 08:29:23 +13:00
|
|
|
author_flair: Flair(
|
2021-01-04 10:06:49 +13:00
|
|
|
val(post, "author_flair_text"),
|
|
|
|
val(post, "author_flair_background_color"),
|
|
|
|
val(post, "author_flair_text_color"),
|
2020-12-21 08:29:23 +13:00
|
|
|
),
|
2021-01-04 10:06:49 +13:00
|
|
|
permalink: val(post, "permalink"),
|
2020-12-08 07:32:46 +13:00
|
|
|
score: format_num(score),
|
2021-01-04 10:06:49 +13:00
|
|
|
upvote_ratio: ratio as i64,
|
2021-01-07 18:27:24 +13:00
|
|
|
post_type,
|
2021-01-06 15:04:49 +13:00
|
|
|
thumbnail: format_url(val(post, "thumbnail")),
|
2020-11-17 15:49:08 +13:00
|
|
|
flair: Flair(
|
2021-01-04 10:06:49 +13:00
|
|
|
val(post, "link_flair_text"),
|
|
|
|
val(post, "link_flair_background_color"),
|
|
|
|
if val(post, "link_flair_text_color") == "dark" {
|
2020-11-17 17:36:36 +13:00
|
|
|
"black".to_string()
|
|
|
|
} else {
|
|
|
|
"white".to_string()
|
|
|
|
},
|
2020-12-22 05:38:24 +13:00
|
|
|
),
|
2020-12-30 16:01:02 +13:00
|
|
|
flags: Flags {
|
2021-01-04 10:06:49 +13:00
|
|
|
nsfw: post["data"]["over_18"].as_bool().unwrap_or(false),
|
|
|
|
stickied: post["data"]["stickied"].as_bool().unwrap_or(false),
|
2020-12-30 16:01:02 +13:00
|
|
|
},
|
2021-01-07 18:27:24 +13:00
|
|
|
media,
|
2021-01-06 17:01:21 +13:00
|
|
|
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
|
2021-01-07 18:27:24 +13:00
|
|
|
}
|
2020-10-26 09:25:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// COMMENTS
|
2020-12-20 16:54:46 +13:00
|
|
|
#[async_recursion]
|
2021-01-07 18:27:24 +13:00
|
|
|
async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
|
2020-12-20 16:54:46 +13:00
|
|
|
// Separate the comment JSON into a Vector of comments
|
2020-12-06 17:54:43 +13:00
|
|
|
let comment_data = json["data"]["children"].as_array().unwrap();
|
2020-10-26 09:25:59 +13:00
|
|
|
|
|
|
|
let mut comments: Vec<Comment> = Vec::new();
|
2020-10-26 16:57:19 +13:00
|
|
|
|
2020-12-20 16:54:46 +13:00
|
|
|
// For each comment, retrieve the values to build a Comment object
|
2020-12-23 15:29:43 +13:00
|
|
|
for comment in comment_data {
|
2020-10-26 09:25:59 +13:00
|
|
|
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
|
2020-12-20 16:54:46 +13:00
|
|
|
if unix_time == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:52:57 +13:00
|
|
|
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
2021-01-03 08:09:26 +13:00
|
|
|
let body = rewrite_url(&val(comment, "body_html"));
|
2020-10-26 13:52:57 +13:00
|
|
|
|
2020-12-20 16:54:46 +13:00
|
|
|
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
|
2021-01-07 18:27:24 +13:00
|
|
|
parse_comments(&comment["data"]["replies"]).await
|
2020-12-20 16:54:46 +13:00
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
2020-10-26 15:05:09 +13:00
|
|
|
|
2020-10-26 09:25:59 +13:00
|
|
|
comments.push(Comment {
|
2021-01-02 09:33:57 +13:00
|
|
|
id: val(comment, "id"),
|
|
|
|
body,
|
|
|
|
author: val(comment, "author"),
|
2020-12-08 07:32:46 +13:00
|
|
|
score: format_num(score),
|
2021-01-06 17:01:21 +13:00
|
|
|
time: OffsetDateTime::from_unix_timestamp(unix_time).format("%b %d %Y %H:%M UTC"),
|
2021-01-02 09:33:57 +13:00
|
|
|
replies,
|
2020-12-21 08:29:23 +13:00
|
|
|
flair: Flair(
|
2021-01-02 09:33:57 +13:00
|
|
|
val(comment, "author_flair_text"),
|
|
|
|
val(comment, "author_flair_background_color"),
|
|
|
|
val(comment, "author_flair_text_color"),
|
2020-12-21 08:29:23 +13:00
|
|
|
),
|
2020-10-26 09:25:59 +13:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-07 18:27:24 +13:00
|
|
|
comments
|
2020-10-26 16:57:19 +13:00
|
|
|
}
|