2023-08-01 13:53:36 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::{
|
2023-06-06 16:27:22 +00:00
|
|
|
build_response::{build_comment_response, send_local_notifs},
|
2022-05-03 17:44:13 +00:00
|
|
|
comment::{CommentResponse, RemoveComment},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-08-01 13:53:36 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
|
2022-05-03 17:44:13 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
comment::{Comment, CommentUpdateForm},
|
2023-08-08 15:19:55 +00:00
|
|
|
comment_report::CommentReport,
|
2022-05-03 17:44:13 +00:00
|
|
|
moderator::{ModRemoveComment, ModRemoveCommentForm},
|
|
|
|
post::Post,
|
|
|
|
},
|
2023-08-08 15:19:55 +00:00
|
|
|
traits::{Crud, Reportable},
|
2022-05-03 17:44:13 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_views::structs::CommentView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn remove_comment(
|
|
|
|
data: Json<RemoveComment>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<CommentResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
let comment_id = data.comment_id;
|
|
|
|
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
// Verify that only a mod or admin can remove
|
|
|
|
is_mod_or_admin(
|
|
|
|
&mut context.pool(),
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
// Do the remove
|
|
|
|
let removed = data.removed;
|
|
|
|
let updated_comment = Comment::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
comment_id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&CommentUpdateForm {
|
|
|
|
removed: Some(removed),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-08-01 13:53:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-08 15:19:55 +00:00
|
|
|
CommentReport::resolve_all_for_object(&mut context.pool(), comment_id, local_user_view.person.id)
|
|
|
|
.await?;
|
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
// Mod tables
|
|
|
|
let form = ModRemoveCommentForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
comment_id: data.comment_id,
|
|
|
|
removed: Some(removed),
|
|
|
|
reason: data.reason.clone(),
|
|
|
|
};
|
|
|
|
ModRemoveComment::create(&mut context.pool(), &form).await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
let post_id = updated_comment.post_id;
|
|
|
|
let post = Post::read(&mut context.pool(), post_id).await?;
|
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
vec![],
|
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person.clone(),
|
|
|
|
&post,
|
|
|
|
false,
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let updated_comment_id = updated_comment.id;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::RemoveComment(
|
|
|
|
updated_comment,
|
|
|
|
local_user_view.person.clone(),
|
|
|
|
orig_comment.community,
|
|
|
|
data.reason.clone(),
|
|
|
|
),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
Ok(Json(
|
2023-06-06 16:27:22 +00:00
|
|
|
build_comment_response(
|
2023-08-01 13:53:36 +00:00
|
|
|
&context,
|
|
|
|
updated_comment_id,
|
2023-06-06 16:27:22 +00:00
|
|
|
Some(local_user_view),
|
|
|
|
recipient_ids,
|
|
|
|
)
|
2023-08-01 13:53:36 +00:00
|
|
|
.await?,
|
|
|
|
))
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|