2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-05-03 17:44:13 +00:00
|
|
|
comment::{CommentResponse, DeleteComment},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{check_community_ban, get_local_user_view_from_jwt},
|
2023-04-13 10:53:55 +00:00
|
|
|
websocket::UserOperationCrud,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{
|
|
|
|
comment::{Comment, CommentUpdateForm},
|
|
|
|
post::Post,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::CommentView;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, ConnectionId};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for DeleteComment {
|
|
|
|
type Response = CommentResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<CommentResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &DeleteComment = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let comment_id = data.comment_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-11-03 17:47:24 +00:00
|
|
|
// Dont delete it if its already been deleted.
|
|
|
|
if orig_comment.comment.deleted == data.deleted {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("couldnt_update_comment"));
|
2021-11-03 17:47:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Verify that only the creator can delete
|
|
|
|
if local_user_view.person.id != orig_comment.creator.id {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("no_comment_edit_allowed"));
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the delete
|
|
|
|
let deleted = data.deleted;
|
2022-11-09 10:05:00 +00:00
|
|
|
let updated_comment = Comment::update(
|
|
|
|
context.pool(),
|
|
|
|
comment_id,
|
|
|
|
&CommentUpdateForm::builder().deleted(Some(deleted)).build(),
|
|
|
|
)
|
|
|
|
.await
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-11-16 00:58:15 +00:00
|
|
|
let post_id = updated_comment.post_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let post = Post::read(context.pool(), post_id).await?;
|
2023-04-13 10:53:55 +00:00
|
|
|
let recipient_ids = context
|
|
|
|
.send_local_notifs(
|
|
|
|
vec![],
|
|
|
|
&updated_comment,
|
|
|
|
&local_user_view.person,
|
|
|
|
&post,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-11-16 00:58:15 +00:00
|
|
|
|
2023-04-13 10:53:55 +00:00
|
|
|
let res = context
|
|
|
|
.send_comment_ws_message(
|
|
|
|
&UserOperationCrud::DeleteComment,
|
|
|
|
data.comment_id,
|
|
|
|
websocket_id,
|
|
|
|
None,
|
|
|
|
Some(local_user_view.person.id),
|
|
|
|
recipient_ids,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-14 15:14:24 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|