diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index 260118eb..f1e4b454 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -69,13 +69,17 @@ impl Comment { .len() } - pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value { + pub fn to_json(&self, conn: &PgConnection, others: &Vec) -> serde_json::Value { let mut json = serde_json::to_value(self).unwrap(); json["author"] = self.get_author(conn).to_json(conn); let mentions = Mention::list_for_comment(conn, self.id).into_iter() .map(|m| m.get_mentioned(conn).map(|u| u.get_fqn(conn)).unwrap_or(String::new())) .collect::>(); json["mentions"] = serde_json::to_value(mentions).unwrap(); + json["responses"] = json!(others.into_iter() + .filter(|c| c.in_response_to_id.map(|id| id == self.id).unwrap_or(false)) + .map(|c| c.to_json(conn, others)) + .collect::>()); json } diff --git a/src/routes/comments.rs b/src/routes/comments.rs index d08998ed..f25489ff 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -48,19 +48,20 @@ fn create(blog_name: String, slug: String, data: LenientForm, us .map_err(|errors| { // TODO: de-duplicate this code let comments = Comment::list_by_post(&*conn, post.id); + let comms = comments.clone(); Template::render("posts/details", json!({ "author": post.get_authors(&*conn)[0].to_json(&*conn), "post": post, "blog": blog, - "comments": comments.into_iter().map(|c| c.to_json(&*conn)).collect::>(), + "comments": &comments.into_iter().map(|c| c.to_json(&*conn, &comms)).collect::>(), "n_likes": post.get_likes(&*conn).len(), "has_liked": user.has_liked(&*conn, &post), "n_reshares": post.get_reshares(&*conn).len(), "has_reshared": user.has_reshared(&*conn, &post), "account": user, "date": &post.creation_date.timestamp(), - "previous": form.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn)), + "previous": form.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn, &vec![])), "user_fqn": user.get_fqn(&*conn), "errors": errors })) diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 95d9886b..d7787bf6 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -36,19 +36,24 @@ fn details_response(blog: String, slug: String, conn: DbConn, user: Option may_fail!(user, Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| { may_fail!(user, Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| { let comments = Comment::list_by_post(&*conn, post.id); + let comms = comments.clone(); Template::render("posts/details", json!({ "author": post.get_authors(&*conn)[0].to_json(&*conn), "post": post, "blog": blog, - "comments": comments.into_iter().map(|c| c.to_json(&*conn)).collect::>(), + "comments": &comments.into_iter().filter_map(|c| if c.in_response_to_id.is_none() { + Some(c.to_json(&*conn, &comms)) + } else { + None + }).collect::>(), "n_likes": post.get_likes(&*conn).len(), "has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false), "n_reshares": post.get_reshares(&*conn).len(), "has_reshared": user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false), "account": user, "date": &post.creation_date.timestamp(), - "previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn))), + "previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn, &vec![]))), "user_fqn": user.map(|u| u.get_fqn(&*conn)).unwrap_or(String::new()) })) }) diff --git a/static/main.css b/static/main.css index 3e9c6da1..c946fde9 100644 --- a/static/main.css +++ b/static/main.css @@ -281,16 +281,21 @@ main .article-meta .comments > a.button { margin-bottom: 1em; } main .article-meta .comments .list { display: grid; margin: 0; + padding: 0 20%; + background: #ECECEC; } /* ~ Comment ~ */ .comments .list .comment { - background: #ECECEC; - padding: 2em 20%; + padding: 2em; font-size: 1em; } +.comments .list > .comment { + border: none; +} + .comments .list .comment .author { display: flex; flex-direction: row; diff --git a/templates/macros.html.tera b/templates/macros.html.tera index e6b04564..9315ca10 100644 --- a/templates/macros.html.tera +++ b/templates/macros.html.tera @@ -45,3 +45,16 @@ {% endif %} {% endmacro %} +{% macro comment(comm) %} +
+ + {{ comm.author.display_name | default(value=comm.author.username) }} + @{{ comm.author.username }} + +
{{ comm.content | safe }}
+ {{ "Respond" | _ }} + {% for res in comm.responses %} + {{ self::comment(comm=res) }} + {% endfor %} +
+{% endmacro %} diff --git a/templates/posts/details.html.tera b/templates/posts/details.html.tera index 6952b1ab..cc893adc 100644 --- a/templates/posts/details.html.tera +++ b/templates/posts/details.html.tera @@ -1,4 +1,5 @@ {% extends "base" %} +{% import "macros" as macros %} {% block title %} {{ post.title }} @@ -78,20 +79,7 @@
{% for comment in comments %} - {% if comment.author.display_name %} - {% set comment_author_name = comment.author.display_name %} - {% else %} - {% set comment_author_name = comment.author.username %} - {% endif %} - - + {{ macros::comment(comm=comment) }} {% endfor %}