Fix comment structuring (#113)

* Start recursive comments

* Update comment.html

* Fix move error

Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com>
This commit is contained in:
robrobinbin 2021-02-10 19:48:51 +01:00 committed by GitHub
parent fee2cb1b56
commit 4a40e16277
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 48 deletions

View file

@ -47,7 +47,7 @@ pub async fn item(req: Request<()>) -> tide::Result {
Ok(res) => {
// Parse the JSON into Post and Comment structs
let post = parse_post(&res[0]).await;
let comments = parse_comments(&res[1]).await;
let comments = parse_comments(&res[1], &post.permalink, &post.author.name).await;
// Use the Post and Comment structs to generate a website to show users
template(PostTemplate {
@ -133,7 +133,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
// COMMENTS
#[async_recursion]
async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str) -> Vec<Comment> {
// Separate the comment JSON into a Vector of comments
let comment_data = match json["data"]["children"].as_array() {
Some(f) => f.to_owned(),
@ -145,22 +145,22 @@ async fn parse_comments(json: &serde_json::Value) -> Vec<Comment> {
// For each comment, retrieve the values to build a Comment object
for comment in comment_data {
let unix_time = comment["data"]["created_utc"].as_f64().unwrap_or_default();
if unix_time == 0.0 {
continue;
}
let (rel_time, created) = time(unix_time);
let score = comment["data"]["score"].as_i64().unwrap_or(0);
let body = rewrite_urls(&val(&comment, "body_html"));
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
parse_comments(&comment["data"]["replies"]).await
parse_comments(&comment["data"]["replies"], post_link, post_author).await
} else {
Vec::new()
};
comments.push(Comment {
id: val(&comment, "id"),
kind: comment["kind"].as_str().unwrap_or_default().to_string(),
post_link: post_link.to_string(),
post_author: post_author.to_string(),
body,
author: Author {
name: val(&comment, "author"),