1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 post::{DeletePost, PostResponse},
6 utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt},
7 websocket::UserOperationCrud,
10 source::post::{Post, PostUpdateForm},
13 use lemmy_utils::{error::LemmyError, ConnectionId};
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for DeletePost {
17 type Response = PostResponse;
19 #[tracing::instrument(skip(context, websocket_id))]
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PostResponse, LemmyError> {
25 let data: &DeletePost = self;
26 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
28 let post_id = data.post_id;
29 let orig_post = Post::read(context.pool(), post_id).await?;
31 // Dont delete it if its already been deleted.
32 if orig_post.deleted == data.deleted {
33 return Err(LemmyError::from_message("couldnt_update_post"));
37 local_user_view.person.id,
38 orig_post.community_id,
42 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
44 // Verify that only the creator can delete
45 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
46 return Err(LemmyError::from_message("no_post_edit_allowed"));
50 let post_id = data.post_id;
51 let deleted = data.deleted;
55 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
60 .send_post_ws_message(
61 &UserOperationCrud::DeletePost,
64 Some(local_user_view.person.id),