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:
parent
fee2cb1b56
commit
4a40e16277
12
src/post.rs
12
src/post.rs
@ -47,7 +47,7 @@ pub async fn item(req: Request<()>) -> tide::Result {
|
|||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
// Parse the JSON into Post and Comment structs
|
// Parse the JSON into Post and Comment structs
|
||||||
let post = parse_post(&res[0]).await;
|
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
|
// Use the Post and Comment structs to generate a website to show users
|
||||||
template(PostTemplate {
|
template(PostTemplate {
|
||||||
@ -133,7 +133,7 @@ async fn parse_post(json: &serde_json::Value) -> Post {
|
|||||||
|
|
||||||
// COMMENTS
|
// COMMENTS
|
||||||
#[async_recursion]
|
#[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
|
// Separate the comment JSON into a Vector of comments
|
||||||
let comment_data = match json["data"]["children"].as_array() {
|
let comment_data = match json["data"]["children"].as_array() {
|
||||||
Some(f) => f.to_owned(),
|
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 each comment, retrieve the values to build a Comment object
|
||||||
for comment in comment_data {
|
for comment in comment_data {
|
||||||
let unix_time = comment["data"]["created_utc"].as_f64().unwrap_or_default();
|
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 (rel_time, created) = time(unix_time);
|
||||||
|
|
||||||
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
let score = comment["data"]["score"].as_i64().unwrap_or(0);
|
||||||
let body = rewrite_urls(&val(&comment, "body_html"));
|
let body = rewrite_urls(&val(&comment, "body_html"));
|
||||||
|
|
||||||
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
|
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 {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
comments.push(Comment {
|
comments.push(Comment {
|
||||||
id: val(&comment, "id"),
|
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,
|
body,
|
||||||
author: Author {
|
author: Author {
|
||||||
name: val(&comment, "author"),
|
name: val(&comment, "author"),
|
||||||
|
@ -76,9 +76,14 @@ pub struct Post {
|
|||||||
pub gallery: Vec<GalleryMedia>,
|
pub gallery: Vec<GalleryMedia>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(path = "comment.html", escape = "none")]
|
||||||
// Comment with content, post, score and data/time that it was posted
|
// Comment with content, post, score and data/time that it was posted
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
pub kind: String,
|
||||||
|
pub post_link: String,
|
||||||
|
pub post_author: String,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
pub author: Author,
|
pub author: Author,
|
||||||
pub score: String,
|
pub score: String,
|
||||||
|
24
templates/comment.html
Normal file
24
templates/comment.html
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{% import "utils.html" as utils %}
|
||||||
|
|
||||||
|
{% if kind == "more" %}
|
||||||
|
<a class="deeper_replies" href="{{ post_link }}{{ id }}">→ More replies</a>
|
||||||
|
{% else if kind == "t1" %}
|
||||||
|
<div id="{{ id }}" class="comment">
|
||||||
|
<div class="comment_left">
|
||||||
|
<p class="comment_score">{{ score }}</p>
|
||||||
|
<div class="line"></div>
|
||||||
|
</div>
|
||||||
|
<details class="comment_right" open>
|
||||||
|
<summary class="comment_data">
|
||||||
|
<a class="comment_author {{ author.distinguished }} {% if author.name == post_author %}op{% endif %}" href="/u/{{ author.name }}">u/{{ author.name }}</a>
|
||||||
|
{% if author.flair.flair_parts.len() > 0 %}
|
||||||
|
<small class="author_flair">{% call utils::render_flair(author.flair.flair_parts) %}</small>
|
||||||
|
{% endif %}
|
||||||
|
<span class="created" title="{{ created }}">{{ rel_time }}</span>
|
||||||
|
</summary>
|
||||||
|
<div class="comment_body">{{ body }}</div>
|
||||||
|
<blockquote class="replies">{% for c in replies -%}{{ c.render().unwrap() }}{%- endfor %}
|
||||||
|
</blockquote>
|
||||||
|
</details>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
@ -17,29 +17,6 @@
|
|||||||
{% call utils::sub_list(post.community.as_str()) %}
|
{% call utils::sub_list(post.community.as_str()) %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
<!-- OPEN COMMENT MACRO -->
|
|
||||||
{% macro comment(item) -%}
|
|
||||||
<div id="{{ item.id }}" class="comment">
|
|
||||||
<div class="comment_left">
|
|
||||||
<p class="comment_score">{{ item.score }}</p>
|
|
||||||
<div class="line"></div>
|
|
||||||
</div>
|
|
||||||
<details class="comment_right" open>
|
|
||||||
<summary class="comment_data">
|
|
||||||
<a class="comment_author {{ item.author.distinguished }} {% if item.author.name == post.author.name %}op{% endif %}" href="/u/{{ item.author.name }}">u/{{ item.author.name }}</a>
|
|
||||||
{% if item.author.flair.flair_parts.len() > 0 %}
|
|
||||||
<small class="author_flair">{% call utils::render_flair(item.author.flair.flair_parts) %}</small>
|
|
||||||
{% endif %}
|
|
||||||
<span class="created" title="{{ post.created }}">{{ item.rel_time }}</span>
|
|
||||||
</summary>
|
|
||||||
<div class="comment_body">{{ item.body }}</div>
|
|
||||||
{%- endmacro %}
|
|
||||||
|
|
||||||
<!-- CLOSE COMMENT MACRO -->
|
|
||||||
{% macro close() %}
|
|
||||||
</details></div>
|
|
||||||
{% endmacro %}
|
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div id="column_one">
|
<div id="column_one">
|
||||||
|
|
||||||
@ -124,25 +101,7 @@
|
|||||||
<!-- COMMENTS -->
|
<!-- COMMENTS -->
|
||||||
{% for c in comments -%}
|
{% for c in comments -%}
|
||||||
<div class="thread">
|
<div class="thread">
|
||||||
<!-- EACH COMMENT -->
|
{{ c.render().unwrap() }}
|
||||||
{% call comment(c) %}
|
|
||||||
<blockquote class="replies">{% for reply1 in c.replies %}{% call comment(reply1) %}
|
|
||||||
<!-- FIRST-LEVEL REPLIES -->
|
|
||||||
<blockquote class="replies">{% for reply2 in reply1.replies %}{% call comment(reply2) %}
|
|
||||||
<!-- SECOND-LEVEL REPLIES -->
|
|
||||||
<blockquote class="replies">{% for reply3 in reply2.replies %}{% call comment(reply3) %}
|
|
||||||
<!-- THIRD-LEVEL REPLIES -->
|
|
||||||
{% if reply3.replies.len() > 0 %}
|
|
||||||
<!-- LINK TO CONTINUE REPLIES -->
|
|
||||||
<a class="deeper_replies" href="{{ post.permalink }}{{ reply3.id }}">→ More replies</a>
|
|
||||||
{% endif %}
|
|
||||||
{% call close() %}
|
|
||||||
{% endfor %}
|
|
||||||
</blockquote>{% call close() %}
|
|
||||||
{% endfor %}
|
|
||||||
</blockquote>{% call close() %}
|
|
||||||
{% endfor %}
|
|
||||||
</blockquote>{% call close() %}
|
|
||||||
</div>
|
</div>
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user