2018-09-09 17:08:53 +02:00
|
|
|
use activitypub::object::Note;
|
2018-06-04 21:57:03 +02:00
|
|
|
use rocket::{
|
2018-07-26 17:51:41 +02:00
|
|
|
State,
|
2018-06-24 18:58:57 +02:00
|
|
|
request::LenientForm,
|
2018-06-21 16:00:25 +02:00
|
|
|
response::Redirect
|
2018-06-04 21:57:03 +02:00
|
|
|
};
|
2018-07-06 11:51:19 +02:00
|
|
|
use rocket_contrib::Template;
|
2018-06-21 12:28:42 +02:00
|
|
|
use serde_json;
|
2018-06-29 14:56:00 +02:00
|
|
|
use validator::Validate;
|
2018-07-26 17:51:41 +02:00
|
|
|
use workerpool::{Pool, thunk::*};
|
2018-05-10 11:44:57 +02:00
|
|
|
|
2018-09-09 17:08:53 +02:00
|
|
|
use plume_common::{utils, activity_pub::{broadcast, ApRequest, ActivityStream}};
|
2018-06-23 18:36:11 +02:00
|
|
|
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-09-09 17:08:53 +02:00
|
|
|
mentions::Mention,
|
2018-05-19 09:39:59 +02:00
|
|
|
posts::Post,
|
2018-09-09 17:08:53 +02:00
|
|
|
safe_string::SafeString,
|
2018-05-19 09:39:59 +02:00
|
|
|
users::User
|
|
|
|
};
|
2018-05-10 11:44:57 +02:00
|
|
|
|
2018-11-07 15:57:31 +01:00
|
|
|
#[derive(FromForm, Debug, Validate, Serialize)]
|
2018-05-10 11:44:57 +02:00
|
|
|
struct NewCommentForm {
|
2018-06-27 00:19:18 +02:00
|
|
|
pub responding_to: Option<i32>,
|
2018-07-07 22:51:48 +02:00
|
|
|
#[validate(length(min = "1", message = "Your comment can't be empty"))]
|
2018-11-07 15:57:31 +01:00
|
|
|
pub content: String,
|
|
|
|
pub warning: String,
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
#[post("/~/<blog_name>/<slug>/comment", data = "<data>")]
|
2018-10-20 11:04:20 +02:00
|
|
|
fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, user: User, conn: DbConn, worker: State<Pool<ThunkWorker<()>>>)
|
|
|
|
-> Result<Redirect, Option<Template>> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, &blog_name).ok_or(None)?;
|
|
|
|
let post = Post::find_by_slug(&*conn, &slug, blog.id).ok_or(None)?;
|
2018-05-10 11:44:57 +02:00
|
|
|
let form = data.get();
|
2018-07-06 11:51:19 +02:00
|
|
|
form.validate()
|
|
|
|
.map(|_| {
|
2018-10-20 16:38:16 +02:00
|
|
|
let (html, mentions, _hashtags) = utils::md_to_html(form.content.as_ref());
|
2018-09-09 17:08:53 +02:00
|
|
|
let comm = Comment::insert(&*conn, NewComment {
|
|
|
|
content: SafeString::new(html.as_ref()),
|
2018-11-26 10:21:52 +01:00
|
|
|
in_response_to_id: form.responding_to,
|
2018-09-09 17:08:53 +02:00
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id,
|
|
|
|
ap_url: None,
|
2018-11-26 10:21:52 +01:00
|
|
|
sensitive: !form.warning.is_empty(),
|
2018-11-07 15:57:31 +01:00
|
|
|
spoiler_text: form.warning.clone()
|
2018-09-09 17:08:53 +02:00
|
|
|
}).update_ap_url(&*conn);
|
|
|
|
let new_comment = comm.create_activity(&*conn);
|
2018-05-19 00:04:30 +02:00
|
|
|
|
2018-09-09 17:08:53 +02:00
|
|
|
// save mentions
|
|
|
|
for ment in mentions {
|
2018-11-26 10:21:52 +01:00
|
|
|
Mention::from_activity(&*conn, &Mention::build_activity(&*conn, &ment), post.id, true, true);
|
2018-09-09 17:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// federate
|
2018-09-09 13:19:11 +02:00
|
|
|
let dest = User::one_by_instance(&*conn);
|
2018-07-26 17:51:41 +02:00
|
|
|
let user_clone = user.clone();
|
2018-09-09 13:19:11 +02:00
|
|
|
worker.execute(Thunk::of(move || broadcast(&user_clone, new_comment, dest)));
|
2018-06-21 12:28:42 +02:00
|
|
|
|
2018-09-09 13:37:20 +02:00
|
|
|
Redirect::to(uri!(super::posts::details: blog = blog_name, slug = slug))
|
2018-07-06 11:51:19 +02:00
|
|
|
})
|
|
|
|
.map_err(|errors| {
|
|
|
|
// TODO: de-duplicate this code
|
|
|
|
let comments = Comment::list_by_post(&*conn, post.id);
|
2018-07-25 18:18:41 +02:00
|
|
|
let comms = comments.clone();
|
2018-05-10 17:36:32 +02:00
|
|
|
|
2018-10-20 11:04:20 +02:00
|
|
|
Some(Template::render("posts/details", json!({
|
2018-07-06 11:51:19 +02:00
|
|
|
"author": post.get_authors(&*conn)[0].to_json(&*conn),
|
|
|
|
"post": post,
|
|
|
|
"blog": blog,
|
2018-07-25 18:18:41 +02:00
|
|
|
"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),
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.to_json(&*conn),
|
2018-07-06 11:51:19 +02:00
|
|
|
"date": &post.creation_date.timestamp(),
|
2018-11-26 10:21:52 +01:00
|
|
|
"previous": form.responding_to.and_then(|r| Comment::get(&*conn, r)).map(|r| r.to_json(&*conn, &[])),
|
2018-07-06 11:51:19 +02:00
|
|
|
"user_fqn": user.get_fqn(&*conn),
|
2018-11-07 15:57:31 +01:00
|
|
|
"comment_form": form,
|
|
|
|
"comment_errors": errors,
|
2018-10-20 11:04:20 +02:00
|
|
|
})))
|
2018-09-09 12:53:22 +02:00
|
|
|
})
|
2018-05-10 11:44:57 +02:00
|
|
|
}
|
2018-09-09 17:08:53 +02:00
|
|
|
|
|
|
|
#[get("/~/<_blog>/<_slug>/comment/<id>")]
|
|
|
|
fn activity_pub(_blog: String, _slug: String, id: i32, _ap: ApRequest, conn: DbConn) -> Option<ActivityStream<Note>> {
|
2018-11-26 10:21:52 +01:00
|
|
|
Comment::get(&*conn, id).map(|c| ActivityStream::new(c.to_activity(&*conn)))
|
2018-09-09 17:08:53 +02:00
|
|
|
}
|