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