Plume/src/routes/comments.rs

70 lines
2.7 KiB
Rust
Raw Normal View History

2018-06-04 21:57:03 +02:00
use rocket::{
2018-06-24 18:58:57 +02:00
request::LenientForm,
response::Redirect
2018-06-04 21:57:03 +02:00
};
2018-07-06 11:51:19 +02:00
use rocket_contrib::Template;
use serde_json;
2018-06-29 14:56:00 +02:00
use validator::Validate;
2018-05-10 11:44:57 +02:00
use plume_common::activity_pub::broadcast;
use plume_models::{
blogs::Blog,
2018-05-19 09:39:59 +02:00
comments::*,
db_conn::DbConn,
instance::Instance,
2018-05-19 09:39:59 +02:00
posts::Post,
users::User
};
use inbox::Inbox;
2018-05-10 11:44:57 +02:00
2018-06-29 14:56:00 +02:00
#[derive(FromForm, Debug, Validate)]
2018-05-10 11:44:57 +02:00
struct NewCommentForm {
pub responding_to: Option<i32>,
#[validate(length(min = "1", message = "Your comment can't be empty"))]
pub content: String
2018-05-10 11:44:57 +02:00
}
#[post("/~/<blog_name>/<slug>/comment", data = "<data>")]
2018-07-06 11:51:19 +02:00
fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, user: User, conn: DbConn) -> Result<Redirect, Template> {
let blog = Blog::find_by_fqn(&*conn, blog_name.clone()).unwrap();
let post = Post::find_by_slug(&*conn, slug.clone(), blog.id).unwrap();
2018-05-10 11:44:57 +02:00
let form = data.get();
2018-07-06 11:51:19 +02:00
form.validate()
.map(|_| {
let (new_comment, id) = NewComment::build()
.content(form.content.clone())
.in_response_to_id(form.responding_to.clone())
.post(post.clone())
.author(user.clone())
.create(&*conn);
2018-05-19 00:04:30 +02:00
2018-07-06 11:51:19 +02:00
let instance = Instance::get_local(&*conn).unwrap();
instance.received(&*conn, serde_json::to_value(new_comment.clone()).expect("JSON serialization error"))
.expect("We are not compatible with ourselve: local broadcast failed (new comment)");
broadcast(&user, new_comment, user.get_followers(&*conn));
2018-07-06 11:51:19 +02:00
Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id))
})
.map_err(|errors| {
// TODO: de-duplicate this code
let comments = Comment::list_by_post(&*conn, post.id);
let comms = comments.clone();
2018-05-10 17:36:32 +02:00
2018-07-06 11:51:19 +02:00
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, &comms)).collect::<Vec<serde_json::Value>>(),
2018-07-06 11:51:19 +02:00
"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, &vec![])),
2018-07-06 11:51:19 +02:00
"user_fqn": user.get_fqn(&*conn),
"errors": errors
}))
})
2018-05-10 11:44:57 +02:00
}