2023-08-01 13:53:36 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2021-03-25 19:19:40 +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, DeleteComment},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-08-01 13:53:36 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-10-13 13:48:18 +00:00
|
|
|
utils::check_community_user_action,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2024-04-08 14:26:24 +00:00
|
|
|
source::comment::{Comment, CommentUpdateForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::{CommentView, LocalUserView};
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn delete_comment(
|
|
|
|
data: Json<DeleteComment>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Json<CommentResponse>> {
|
2023-08-01 13:53:36 +00:00
|
|
|
let comment_id = data.comment_id;
|
2024-04-16 12:48:15 +00:00
|
|
|
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None)
|
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::CouldntFindComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
// Dont delete it if its already been deleted.
|
|
|
|
if orig_comment.comment.deleted == data.deleted {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::CouldntUpdateComment)?
|
2023-08-01 13:53:36 +00:00
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-10-13 13:48:18 +00:00
|
|
|
check_community_user_action(
|
|
|
|
&local_user_view.person,
|
2023-08-01 13:53:36 +00:00
|
|
|
orig_comment.community.id,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2021-11-03 17:47:24 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
// Verify that only the creator can delete
|
|
|
|
if local_user_view.person.id != orig_comment.creator.id {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::NoCommentEditAllowed)?
|
2023-08-01 13:53:36 +00:00
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
// Do the delete
|
|
|
|
let deleted = data.deleted;
|
|
|
|
let updated_comment = Comment::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
comment_id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&CommentUpdateForm {
|
|
|
|
deleted: Some(deleted),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-08-01 13:53:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2024-04-08 14:26:24 +00:00
|
|
|
let recipient_ids =
|
|
|
|
send_local_notifs(vec![], comment_id, &local_user_view.person, false, &context).await?;
|
2023-08-01 13:53:36 +00:00
|
|
|
let updated_comment_id = updated_comment.id;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-01 13:53:36 +00:00
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::DeleteComment(
|
|
|
|
updated_comment,
|
|
|
|
local_user_view.person.clone(),
|
|
|
|
orig_comment.community,
|
|
|
|
),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-14 15:14:24 +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?,
|
|
|
|
))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|