From 4a40e1627763c1076c31e0b98c6dc0a084746d7b Mon Sep 17 00:00:00 2001 From: robrobinbin <8597693+robrobinbin@users.noreply.github.com> Date: Wed, 10 Feb 2021 19:48:51 +0100 Subject: [PATCH] Fix comment structuring (#113) * Start recursive comments * Update comment.html * Fix move error Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com> --- src/post.rs | 12 ++++++------ src/utils.rs | 5 +++++ templates/comment.html | 24 +++++++++++++++++++++++ templates/post.html | 43 +----------------------------------------- 4 files changed, 36 insertions(+), 48 deletions(-) create mode 100644 templates/comment.html diff --git a/src/post.rs b/src/post.rs index b207661..25deb03 100644 --- a/src/post.rs +++ b/src/post.rs @@ -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 { +async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str) -> Vec { // 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 { // 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 = 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"), diff --git a/src/utils.rs b/src/utils.rs index 0b70fa7..f6398c9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -76,9 +76,14 @@ pub struct Post { pub gallery: Vec, } +#[derive(Template)] +#[template(path = "comment.html", escape = "none")] // Comment with content, post, score and data/time that it was posted pub struct Comment { pub id: String, + pub kind: String, + pub post_link: String, + pub post_author: String, pub body: String, pub author: Author, pub score: String, diff --git a/templates/comment.html b/templates/comment.html new file mode 100644 index 0000000..f2bfc05 --- /dev/null +++ b/templates/comment.html @@ -0,0 +1,24 @@ +{% import "utils.html" as utils %} + +{% if kind == "more" %} +→ More replies +{% else if kind == "t1" %} +
+
+

{{ score }}

+
+
+
+ + + {% if author.flair.flair_parts.len() > 0 %} + {% call utils::render_flair(author.flair.flair_parts) %} + {% endif %} + {{ rel_time }} + +
{{ body }}
+
{% for c in replies -%}{{ c.render().unwrap() }}{%- endfor %} +
+
+
+{% endif %} diff --git a/templates/post.html b/templates/post.html index 1dee7d8..7bcced6 100644 --- a/templates/post.html +++ b/templates/post.html @@ -17,29 +17,6 @@ {% call utils::sub_list(post.community.as_str()) %} {% endblock %} - -{% macro comment(item) -%} -
-
-

{{ item.score }}

-
-
-
- - u/{{ item.author.name }} - {% if item.author.flair.flair_parts.len() > 0 %} - {% call utils::render_flair(item.author.flair.flair_parts) %} - {% endif %} - {{ item.rel_time }} - -
{{ item.body }}
-{%- endmacro %} - - -{% macro close() %} -
-{% endmacro %} - {% block content %}
@@ -124,25 +101,7 @@ {% for c in comments -%}
- - {% call comment(c) %} -
{% for reply1 in c.replies %}{% call comment(reply1) %} - -
{% for reply2 in reply1.replies %}{% call comment(reply2) %} - -
{% for reply3 in reply2.replies %}{% call comment(reply3) %} - - {% if reply3.replies.len() > 0 %} - - → More replies - {% endif %} - {% call close() %} - {% endfor %} -
{% call close() %} - {% endfor %} -
{% call close() %} - {% endfor %} -
{% call close() %} + {{ c.render().unwrap() }}
{%- endfor %}