2018-06-04 21:57:03 +02:00
|
|
|
use rocket::{
|
|
|
|
request::Form,
|
2018-06-21 16:00:25 +02:00
|
|
|
response::Redirect
|
2018-06-04 21:57:03 +02:00
|
|
|
};
|
2018-06-21 12:28:42 +02:00
|
|
|
use serde_json;
|
2018-05-10 11:44:57 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::broadcast;
|
|
|
|
use plume_models::{
|
2018-06-19 21:16:18 +02:00
|
|
|
blogs::Blog,
|
2018-05-19 09:39:59 +02:00
|
|
|
comments::*,
|
2018-06-23 18:36:11 +02:00
|
|
|
db_conn::DbConn,
|
2018-06-21 12:28:42 +02:00
|
|
|
instance::Instance,
|
2018-05-19 09:39:59 +02:00
|
|
|
posts::Post,
|
|
|
|
users::User
|
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use inbox::Inbox;
|
2018-05-10 11:44:57 +02:00
|
|
|
|
2018-05-10 16:26:12 +02:00
|
|
|
#[derive(FromForm)]
|
2018-06-21 16:00:25 +02:00
|
|
|
pub struct CommentQuery {
|
|
|
|
pub responding_to: Option<i32>
|
2018-05-10 16:26:12 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 11:44:57 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct NewCommentForm {
|
2018-05-10 16:26:12 +02:00
|
|
|
pub content: String
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
// See: https://github.com/SergioBenitez/Rocket/pull/454
|
|
|
|
#[post("/~/<blog_name>/<slug>/comment", data = "<data>")]
|
|
|
|
fn create(blog_name: String, slug: String, data: Form<NewCommentForm>, user: User, conn: DbConn) -> Redirect {
|
|
|
|
create_response(blog_name, slug, None, data, user, conn)
|
|
|
|
}
|
|
|
|
|
2018-06-19 21:16:18 +02:00
|
|
|
#[post("/~/<blog_name>/<slug>/comment?<query>", data = "<data>")]
|
2018-06-21 12:28:42 +02:00
|
|
|
fn create_response(blog_name: String, slug: String, query: Option<CommentQuery>, data: Form<NewCommentForm>, user: User, conn: DbConn) -> Redirect {
|
2018-06-19 21:16:18 +02:00
|
|
|
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-05-19 00:04:30 +02:00
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
let (new_comment, id) = NewComment::build()
|
|
|
|
.content(form.content.clone())
|
|
|
|
.in_response_to_id(query.and_then(|q| q.responding_to))
|
|
|
|
.post(post)
|
|
|
|
.author(user.clone())
|
|
|
|
.create(&*conn);
|
|
|
|
|
|
|
|
let instance = Instance::get_local(&*conn).unwrap();
|
2018-06-21 18:00:37 +02:00
|
|
|
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)");
|
2018-06-21 17:31:42 +02:00
|
|
|
broadcast(&user, new_comment, user.get_followers(&*conn));
|
2018-05-10 17:36:32 +02:00
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
Redirect::to(format!("/~/{}/{}/#comment-{}", blog_name, slug, id))
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|