From a3d73cb2c4088984a578d84fae0850bdaf60aa48 Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 10 May 2018 10:44:57 +0100 Subject: [PATCH] Create and display comments --- src/main.rs | 5 ++++- src/models/comments.rs | 11 +++++++++++ src/routes/comments.rs | 38 ++++++++++++++++++++++++++++++++++++ src/routes/mod.rs | 1 + src/routes/posts.rs | 11 ++++++++++- templates/comments/new.tera | 20 +++++++++++++++++++ templates/posts/details.tera | 23 +++++++++++++++------- 7 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 src/routes/comments.rs create mode 100644 templates/comments/new.tera diff --git a/src/main.rs b/src/main.rs index 737a3fb..610f633 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,7 +89,10 @@ fn main() { routes::posts::activity_details, routes::posts::new, routes::posts::new_auth, - routes::posts::create + routes::posts::create, + + routes::comments::new, + routes::comments::create ]) .manage(init_pool()) .attach(Template::fairing()) diff --git a/src/models/comments.rs b/src/models/comments.rs index 89c100e..f80a5a7 100644 --- a/src/models/comments.rs +++ b/src/models/comments.rs @@ -1,6 +1,7 @@ use chrono; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; +use models::users::User; use schema::comments; #[derive(Queryable, Identifiable, Serialize)] @@ -43,4 +44,14 @@ impl Comment { .expect("Error loading comment by id") .into_iter().nth(0) } + + pub fn for_post(conn: &PgConnection, post_id: i32) -> Vec { + comments::table.filter(comments::post_id.eq(post_id)) + .load::(conn) + .expect("Error loading comment by post id") + } + + pub fn get_author(&self, conn: &PgConnection) -> User { + User::get(conn, self.author_id).unwrap() + } } diff --git a/src/routes/comments.rs b/src/routes/comments.rs new file mode 100644 index 0000000..fab683c --- /dev/null +++ b/src/routes/comments.rs @@ -0,0 +1,38 @@ +use rocket::request::Form; +use rocket::response::Redirect; +use rocket_contrib::Template; + +use db_conn::DbConn; +use models::comments::*; +use models::posts::Post; +use models::users::User; + +#[get("/~/<_blog>//comment")] +fn new(_blog: String, slug: String, _user: User, conn: DbConn) -> Template { + let post = Post::find_by_slug(&*conn, slug).unwrap(); + Template::render("comments/new", json!({ + "post": post + })) +} + +#[derive(FromForm)] +struct NewCommentForm { + pub content: String, + pub respond_to: Option +} + +#[post("/~/<_blog>//comment", data = "")] +fn create(_blog: String, slug: String, data: Form, user: User, conn: DbConn) -> Redirect { + let post = Post::find_by_slug(&*conn, slug).unwrap(); + let form = data.get(); + Comment::insert(&*conn, NewComment { + content: form.content.clone(), + in_response_to_id: form.respond_to, + post_id: post.id, + author_id: user.id, + ap_url: None, + sensitive: false, + spoiler_text: "".to_string() + }); + Redirect::to("") +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 8d7aae6..7a225a9 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,4 +1,5 @@ pub mod blogs; +pub mod comments; pub mod instance; pub mod posts; pub mod session; diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 3b0e4db..f2a928c 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -2,6 +2,7 @@ use heck::KebabCase; use rocket::request::Form; use rocket::response::Redirect; use rocket_contrib::Template; +use serde_json; use std::collections::HashMap; use activity_pub::{context, activity_pub, ActivityPub}; @@ -10,6 +11,7 @@ use activity_pub::object::Object; use activity_pub::outbox::broadcast; use db_conn::DbConn; use models::blogs::*; +use models::comments::Comment; use models::post_authors::*; use models::posts::*; use models::users::User; @@ -19,9 +21,16 @@ use utils; fn details(blog: String, slug: String, conn: DbConn) -> Template { let blog = Blog::find_by_actor_id(&*conn, blog).unwrap(); let post = Post::find_by_slug(&*conn, slug).unwrap(); + let comments = Comment::for_post(&*conn, post.id); Template::render("posts/details", json!({ "post": post, - "blog": blog + "blog": blog, + "comments": comments.into_iter().map(|c| { + json!({ + "content": c.content, + "author": c.get_author(&*conn) + }) + }).collect::>() })) } diff --git a/templates/comments/new.tera b/templates/comments/new.tera new file mode 100644 index 0000000..0536a43 --- /dev/null +++ b/templates/comments/new.tera @@ -0,0 +1,20 @@ +{% extends "base" %} + +{% block title %} +Comment "{{ post.title }}" +{% endblock title %} + +{% block content %} +

Comment "{{ post.title }}"

+
+ + + + {% if responding_to %} + + {% endif %} + + +
+{% endblock content %} + diff --git a/templates/posts/details.tera b/templates/posts/details.tera index 1714bfe..71cc171 100644 --- a/templates/posts/details.tera +++ b/templates/posts/details.tera @@ -5,11 +5,20 @@ {% endblock title %} {% block content %} -

{{ post.title }}

-

Published in {{ blog.title }}

-
-

- {{ post.content | safe }} -

-

License: {{ post.license }}

+

{{ post.title }}

+

Published in {{ blog.title }}

+
+

+ {{ post.content | safe }} +

+

License: {{ post.license }}

+
+

Comments

+ {% for comment in comments %} +
+ {{ comment.author.display_name }} +
{{ comment.content | safe }}
+
+ {% endfor %} + Comment {% endblock content %}