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