Plume/src/routes/comments.rs

194 lines
6.5 KiB
Rust
Raw Normal View History

2020-01-21 07:02:03 +01:00
use crate::template_utils::Ructe;
use activitystreams::object::Note;
use rocket::{
request::LenientForm,
response::{Flash, Redirect},
};
2019-03-20 17:56:17 +01:00
use validator::Validate;
2018-05-10 11:44:57 +02:00
use std::time::Duration;
2020-01-21 07:02:03 +01:00
use crate::routes::errors::ErrorPage;
use crate::template_utils::IntoContext;
2019-03-20 17:56:17 +01:00
use plume_common::{
activity_pub::{broadcast07, ActivityStream, ApRequest},
2019-03-20 17:56:17 +01:00
utils,
};
use plume_models::{
2021-01-30 13:44:29 +01:00
blogs::Blog, comments::*, db_conn::DbConn, inbox::inbox, instance::Instance, medias::Media,
mentions::Mention, posts::Post, safe_string::SafeString, tags::Tag, users::User, Error,
PlumeRocket, CONFIG,
2018-05-19 09:39:59 +02:00
};
2018-05-10 11:44:57 +02:00
#[derive(Default, FromForm, Debug, Validate)]
pub struct NewCommentForm {
pub responding_to: Option<i32>,
2022-01-06 20:55:49 +01:00
#[validate(length(min = 1, message = "Your comment can't be empty"))]
pub content: String,
pub warning: String,
2018-05-10 11:44:57 +02:00
}
#[post("/~/<blog_name>/<slug>/comment", data = "<form>")]
2019-03-20 17:56:17 +01:00
pub fn create(
blog_name: String,
slug: String,
form: LenientForm<NewCommentForm>,
user: User,
2021-01-30 13:44:29 +01:00
conn: DbConn,
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
rockets: PlumeRocket,
) -> Result<Flash<Redirect>, Ructe> {
2021-01-30 13:44:29 +01:00
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 11:51:19 +02:00
form.validate()
.map(|_| {
let (html, mentions, _hashtags) = utils::md_to_html(
form.content.as_ref(),
Some(
&Instance::get_local()
.expect("comments::create: local instance error")
.public_domain,
),
true,
Some(Media::get_media_processor(&conn, vec![&user])),
);
2019-03-20 17:56:17 +01:00
let comm = Comment::insert(
2021-01-30 13:44:29 +01:00
&conn,
2019-03-20 17:56:17 +01:00
NewComment {
content: SafeString::new(html.as_ref()),
in_response_to_id: form.responding_to,
post_id: post.id,
author_id: user.id,
ap_url: None,
sensitive: !form.warning.is_empty(),
spoiler_text: form.warning.clone(),
public_visibility: true,
},
)
.expect("comments::create: insert error");
let new_comment = comm
.create_activity07(&conn)
2019-03-20 17:56:17 +01:00
.expect("comments::create: activity error");
2018-05-19 00:04:30 +02:00
// save mentions
for ment in mentions {
Mention::from_activity(
2021-01-30 13:44:29 +01:00
&conn,
&Mention::build_activity(&conn, &ment)
2019-03-20 17:56:17 +01:00
.expect("comments::create: build mention error"),
comm.id,
false,
2019-03-20 17:56:17 +01:00
true,
)
.expect("comments::create: mention save error");
}
2021-01-30 13:44:29 +01:00
comm.notify(&conn).expect("comments::create: notify error");
// federate
2021-01-30 13:44:29 +01:00
let dest = User::one_by_instance(&conn).expect("comments::create: dest error");
2018-07-26 17:51:41 +02:00
let user_clone = user.clone();
2021-01-11 21:27:52 +01:00
rockets.worker.execute(move || {
broadcast07(&user_clone, new_comment, dest, CONFIG.proxy().cloned())
2021-01-11 21:27:52 +01:00
});
Flash::success(
2021-01-15 17:13:45 +01:00
Redirect::to(uri!(
super::posts::details: blog = blog_name,
slug = slug,
responding_to = _
)),
i18n!(&rockets.intl.catalog, "Your comment has been posted."),
2019-03-20 17:56:17 +01:00
)
2018-07-06 11:51:19 +02:00
})
.map_err(|errors| {
// TODO: de-duplicate this code
2021-01-30 13:44:29 +01:00
let comments = CommentTree::from_post(&conn, &post, Some(&user))
2019-03-20 17:56:17 +01:00
.expect("comments::create: comments error");
2018-05-10 17:36:32 +02:00
2021-01-30 13:44:29 +01:00
let previous = form.responding_to.and_then(|r| Comment::get(&conn, r).ok());
render!(posts::details(
2021-01-30 13:44:29 +01:00
&(&conn, &rockets).to_context(),
post.clone(),
blog,
&*form,
errors,
2021-01-30 13:44:29 +01:00
Tag::for_post(&conn, post.id).expect("comments::create: tags error"),
comments,
previous,
2021-01-30 13:44:29 +01:00
post.count_likes(&conn)
2019-03-20 17:56:17 +01:00
.expect("comments::create: count likes error"),
2021-01-30 13:44:29 +01:00
post.count_reshares(&conn)
2019-03-20 17:56:17 +01:00
.expect("comments::create: count reshares error"),
2021-01-30 13:44:29 +01:00
user.has_liked(&conn, &post)
2019-03-20 17:56:17 +01:00
.expect("comments::create: liked error"),
2021-01-30 13:44:29 +01:00
user.has_reshared(&conn, &post)
2019-03-20 17:56:17 +01:00
.expect("comments::create: reshared error"),
user.is_following(
&*conn,
2021-01-30 13:44:29 +01:00
post.get_authors(&conn)
2019-03-20 17:56:17 +01:00
.expect("comments::create: authors error")[0]
.id
)
.expect("comments::create: following error"),
2021-01-30 13:44:29 +01:00
post.get_authors(&conn)
2019-03-20 17:56:17 +01:00
.expect("comments::create: authors error")[0]
.clone()
))
})
2018-05-10 11:44:57 +02:00
}
#[post("/~/<blog>/<slug>/comment/<id>/delete")]
2019-03-20 17:56:17 +01:00
pub fn delete(
blog: String,
slug: String,
id: i32,
user: User,
2021-01-30 13:44:29 +01:00
conn: DbConn,
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
rockets: PlumeRocket,
) -> Result<Flash<Redirect>, ErrorPage> {
2021-01-30 13:44:29 +01:00
if let Ok(comment) = Comment::get(&conn, id) {
if comment.author_id == user.id {
2021-01-30 13:44:29 +01:00
let dest = User::one_by_instance(&conn)?;
let delete_activity = comment.build_delete07(&conn)?;
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
inbox(
2021-01-30 13:44:29 +01:00
&conn,
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
serde_json::to_value(&delete_activity).map_err(Error::from)?,
)?;
let user_c = user.clone();
2021-01-11 21:27:52 +01:00
rockets.worker.execute(move || {
broadcast07(&user_c, delete_activity, dest, CONFIG.proxy().cloned())
2021-01-11 21:27:52 +01:00
});
Big refactoring of the Inbox (#443) * Big refactoring of the Inbox We now have a type that routes an activity through the registered handlers until one of them matches. Each Actor/Activity/Object combination is represented by an implementation of AsObject These combinations are then registered on the Inbox type, which will try to deserialize the incoming activity in the requested types. Advantages: - nicer syntax: the final API is clearer and more idiomatic - more generic: only two traits (`AsActor` and `AsObject`) instead of one for each kind of activity - it is easier to see which activities we handle and which one we don't * Small fixes - Avoid panics - Don't search for AP ID infinitely - Code style issues * Fix tests * Introduce a new trait: FromId It should be implemented for any AP object. It allows to look for an object in database using its AP ID, or to dereference it if it was not present in database Also moves the inbox code to plume-models to test it (and write a basic test for each activity type we handle) * Use if let instead of match * Don't require PlumeRocket::intl for tests * Return early and remove a forgotten dbg! * Add more tests to try to understand where the issues come from * Also add a test for comment federation * Don't check creation_date is the same for blogs * Make user and blog federation more tolerant to errors/missing fields * Make clippy happy * Use the correct Accept header when dereferencing * Fix follow approval with Mastodon * Add spaces to characters that should not be in usernames And validate blog names too * Smarter dereferencing: only do it once for each actor/object * Forgot some files * Cargo fmt * Delete plume_test * Delete plume_tests * Update get_id docs + Remove useless : Sized * Appease cargo fmt * Remove dbg! + Use as_ref instead of clone when possible + Use and_then instead of map when possible * Remove .po~ * send unfollow to local instance * read cover from update activity * Make sure "cc" and "to" are never empty and fix a typo in a constant name * Cargo fmt
2019-04-17 19:31:47 +02:00
rockets
.worker
.execute_after(Duration::from_secs(10 * 60), move || {
user.rotate_keypair(&conn)
.expect("Failed to rotate keypair");
});
}
}
Ok(Flash::success(
2021-01-15 17:13:45 +01:00
Redirect::to(uri!(
super::posts::details: blog = blog,
slug = slug,
responding_to = _
)),
i18n!(&rockets.intl.catalog, "Your comment has been deleted."),
2019-03-20 17:56:17 +01:00
))
}
#[get("/~/<_blog>/<_slug>/comment/<id>")]
2019-03-20 17:56:17 +01:00
pub fn activity_pub(
_blog: String,
_slug: String,
id: i32,
_ap: ApRequest,
2021-01-30 13:44:29 +01:00
conn: DbConn,
2019-03-20 17:56:17 +01:00
) -> Option<ActivityStream<Note>> {
2021-01-30 13:44:29 +01:00
Comment::get(&conn, id)
.and_then(|c| c.to_activity07(&conn))
.ok()
.map(ActivityStream::new)
}