2018-09-09 15:08:53 +00:00
|
|
|
use activitypub::object::Note;
|
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-12-06 17:54:16 +00:00
|
|
|
use rocket_i18n::I18n;
|
2018-06-29 12:56:00 +00:00
|
|
|
use validator::Validate;
|
2018-12-06 17:54:16 +00:00
|
|
|
use template_utils::Ructe;
|
2018-05-10 09:44:57 +00:00
|
|
|
|
2018-12-23 10:13:36 +00:00
|
|
|
use plume_common::{utils, activity_pub::{broadcast, ApRequest,
|
|
|
|
ActivityStream, inbox::Deletable}};
|
2018-06-23 16:36:11 +00:00
|
|
|
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-12-23 10:12:15 +00:00
|
|
|
instance::Instance,
|
2018-09-09 15:08:53 +00:00
|
|
|
mentions::Mention,
|
2018-05-19 07:39:59 +00:00
|
|
|
posts::Post,
|
2018-09-09 15:08:53 +00:00
|
|
|
safe_string::SafeString,
|
2018-12-06 17:54:16 +00:00
|
|
|
tags::Tag,
|
2018-05-19 07:39:59 +00:00
|
|
|
users::User
|
|
|
|
};
|
2018-12-02 16:37:51 +00:00
|
|
|
use Worker;
|
2018-12-29 08:36:07 +00:00
|
|
|
use routes::errors::ErrorPage;
|
2018-05-10 09:44:57 +00:00
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
#[derive(Default, FromForm, Debug, Validate, Serialize)]
|
|
|
|
pub struct NewCommentForm {
|
2018-06-26 22:19:18 +00:00
|
|
|
pub responding_to: Option<i32>,
|
2018-07-07 20:51:48 +00:00
|
|
|
#[validate(length(min = "1", message = "Your comment can't be empty"))]
|
2018-11-07 14:57:31 +00:00
|
|
|
pub content: String,
|
|
|
|
pub warning: String,
|
2018-05-10 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:54:16 +00:00
|
|
|
#[post("/~/<blog_name>/<slug>/comment", data = "<form>")]
|
|
|
|
pub fn create(blog_name: String, slug: String, form: LenientForm<NewCommentForm>, user: User, conn: DbConn, worker: Worker, intl: I18n)
|
2018-12-29 08:36:07 +00:00
|
|
|
-> Result<Redirect, Ructe> {
|
|
|
|
let blog = Blog::find_by_fqn(&*conn, &blog_name).expect("comments::create: blog error");
|
|
|
|
let post = Post::find_by_slug(&*conn, &slug, blog.id).expect("comments::create: post error");
|
2018-07-06 09:51:19 +00:00
|
|
|
form.validate()
|
|
|
|
.map(|_| {
|
2018-12-29 08:36:07 +00:00
|
|
|
let (html, mentions, _hashtags) = utils::md_to_html(
|
|
|
|
form.content.as_ref(),
|
|
|
|
&Instance::get_local(&conn).expect("comments::create: local instance error").public_domain
|
|
|
|
);
|
2018-09-09 15:08:53 +00:00
|
|
|
let comm = Comment::insert(&*conn, NewComment {
|
|
|
|
content: SafeString::new(html.as_ref()),
|
2018-11-26 09:21:52 +00:00
|
|
|
in_response_to_id: form.responding_to,
|
2018-09-09 15:08:53 +00:00
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id,
|
|
|
|
ap_url: None,
|
2018-11-26 09:21:52 +00:00
|
|
|
sensitive: !form.warning.is_empty(),
|
2018-12-24 10:23:04 +00:00
|
|
|
spoiler_text: form.warning.clone(),
|
|
|
|
public_visibility: true
|
2018-12-29 08:36:07 +00:00
|
|
|
}).expect("comments::create: insert error").update_ap_url(&*conn).expect("comments::create: update ap url error");
|
|
|
|
let new_comment = comm.create_activity(&*conn).expect("comments::create: activity error");
|
2018-05-18 22:04:30 +00:00
|
|
|
|
2018-09-09 15:08:53 +00:00
|
|
|
// save mentions
|
|
|
|
for ment in mentions {
|
2018-12-29 08:36:07 +00:00
|
|
|
Mention::from_activity(
|
|
|
|
&*conn,
|
|
|
|
&Mention::build_activity(&*conn, &ment).expect("comments::create: build mention error"),
|
|
|
|
post.id,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
).expect("comments::create: mention save error");
|
2018-09-09 15:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// federate
|
2018-12-29 08:36:07 +00:00
|
|
|
let dest = User::one_by_instance(&*conn).expect("comments::create: dest error");
|
2018-07-26 15:51:41 +00:00
|
|
|
let user_clone = user.clone();
|
2018-12-02 16:37:51 +00:00
|
|
|
worker.execute(move || broadcast(&user_clone, new_comment, dest));
|
2018-06-21 10:28:42 +00:00
|
|
|
|
2018-12-13 21:20:19 +00:00
|
|
|
Redirect::to(uri!(super::posts::details: blog = blog_name, slug = slug, responding_to = _))
|
2018-07-06 09:51:19 +00:00
|
|
|
})
|
|
|
|
.map_err(|errors| {
|
|
|
|
// TODO: de-duplicate this code
|
2018-12-29 08:36:07 +00:00
|
|
|
let comments = CommentTree::from_post(&*conn, &post, Some(&user)).expect("comments::create: comments error");
|
2018-05-10 15:36:32 +00:00
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
let previous = form.responding_to.and_then(|r| Comment::get(&*conn, r).ok());
|
2018-12-06 17:54:16 +00:00
|
|
|
|
2018-12-29 08:36:07 +00:00
|
|
|
render!(posts::details(
|
2018-12-06 17:54:16 +00:00
|
|
|
&(&*conn, &intl.catalog, Some(user.clone())),
|
|
|
|
post.clone(),
|
|
|
|
blog,
|
|
|
|
&*form,
|
|
|
|
errors,
|
2018-12-29 08:36:07 +00:00
|
|
|
Tag::for_post(&*conn, post.id).expect("comments::create: tags error"),
|
2018-12-24 10:23:04 +00:00
|
|
|
comments,
|
2018-12-06 17:54:16 +00:00
|
|
|
previous,
|
2018-12-29 08:36:07 +00:00
|
|
|
post.count_likes(&*conn).expect("comments::create: count likes error"),
|
|
|
|
post.count_reshares(&*conn).expect("comments::create: count reshares error"),
|
|
|
|
user.has_liked(&*conn, &post).expect("comments::create: liked error"),
|
|
|
|
user.has_reshared(&*conn, &post).expect("comments::create: reshared error"),
|
|
|
|
user.is_following(&*conn, post.get_authors(&*conn).expect("comments::create: authors error")[0].id)
|
|
|
|
.expect("comments::create: following error"),
|
|
|
|
post.get_authors(&*conn).expect("comments::create: authors error")[0].clone()
|
|
|
|
))
|
2018-09-09 10:53:22 +00:00
|
|
|
})
|
2018-05-10 09:44:57 +00:00
|
|
|
}
|
2018-09-09 15:08:53 +00:00
|
|
|
|
2018-12-23 10:13:36 +00:00
|
|
|
#[post("/~/<blog>/<slug>/comment/<id>/delete")]
|
2018-12-29 08:36:07 +00:00
|
|
|
pub fn delete(blog: String, slug: String, id: i32, user: User, conn: DbConn, worker: Worker) -> Result<Redirect, ErrorPage> {
|
|
|
|
if let Ok(comment) = Comment::get(&*conn, id) {
|
2018-12-23 10:13:36 +00:00
|
|
|
if comment.author_id == user.id {
|
2018-12-29 08:36:07 +00:00
|
|
|
let dest = User::one_by_instance(&*conn)?;
|
|
|
|
let delete_activity = comment.delete(&*conn)?;
|
2018-12-23 10:13:36 +00:00
|
|
|
worker.execute(move || broadcast(&user, delete_activity, dest));
|
|
|
|
}
|
|
|
|
}
|
2018-12-29 08:36:07 +00:00
|
|
|
Ok(Redirect::to(uri!(super::posts::details: blog = blog, slug = slug, responding_to = _)))
|
2018-12-23 10:13:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 15:08:53 +00:00
|
|
|
#[get("/~/<_blog>/<_slug>/comment/<id>")]
|
2018-12-06 17:54:16 +00:00
|
|
|
pub fn activity_pub(_blog: String, _slug: String, id: i32, _ap: ApRequest, conn: DbConn) -> Option<ActivityStream<Note>> {
|
2018-12-29 08:36:07 +00:00
|
|
|
Comment::get(&*conn, id)
|
|
|
|
.and_then(|c| c.to_activity(&*conn))
|
|
|
|
.ok()
|
|
|
|
.map(ActivityStream::new)
|
2018-09-09 15:08:53 +00:00
|
|
|
}
|