2022-05-03 17:44:13 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
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-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},
|
2022-05-03 17:44:13 +00:00
|
|
|
moderator::{ModRemoveComment, ModRemoveCommentForm},
|
|
|
|
post::Post,
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
|
|
|
use lemmy_db_views::structs::CommentView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2023-07-28 14:39:38 +00:00
|
|
|
use std::ops::Deref;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for RemoveComment {
|
|
|
|
type Response = CommentResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
|
2022-05-03 17:44:13 +00:00
|
|
|
let data: &RemoveComment = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
|
|
|
let comment_id = data.comment_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-05-03 17:44:13 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Verify that only a mod or admin can remove
|
|
|
|
is_mod_or_admin(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-05-03 17:44:13 +00:00
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Do the remove
|
|
|
|
let removed = data.removed;
|
2022-11-09 10:05:00 +00:00
|
|
|
let updated_comment = Comment::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
comment_id,
|
|
|
|
&CommentUpdateForm::builder().removed(Some(removed)).build(),
|
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModRemoveCommentForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
comment_id: data.comment_id,
|
|
|
|
removed: Some(removed),
|
2022-11-19 04:33:54 +00:00
|
|
|
reason: data.reason.clone(),
|
2022-05-03 17:44:13 +00:00
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
ModRemoveComment::create(&mut context.pool(), &form).await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
|
|
|
let post_id = updated_comment.post_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let post = Post::read(&mut context.pool(), post_id).await?;
|
2023-06-06 16:27:22 +00:00
|
|
|
let recipient_ids = send_local_notifs(
|
|
|
|
vec![],
|
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person.clone(),
|
|
|
|
&post,
|
|
|
|
false,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-05-03 17:44:13 +00:00
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
build_comment_response(
|
2023-07-28 14:39:38 +00:00
|
|
|
context.deref(),
|
2023-06-06 16:27:22 +00:00
|
|
|
updated_comment.id,
|
|
|
|
Some(local_user_view),
|
|
|
|
None,
|
|
|
|
recipient_ids,
|
|
|
|
)
|
|
|
|
.await
|
2022-05-03 17:44:13 +00:00
|
|
|
}
|
|
|
|
}
|