1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{DeletePost, PostResponse},
5 utils::{check_community_ban, check_community_deleted_or_removed, get_local_user_view_from_jwt},
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
11 post::{Post, PostUpdateForm},
15 use lemmy_utils::{error::LemmyError, ConnectionId};
16 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for DeletePost {
20 type Response = PostResponse;
22 #[tracing::instrument(skip(context, websocket_id))]
25 context: &Data<LemmyContext>,
26 websocket_id: Option<ConnectionId>,
27 ) -> Result<PostResponse, LemmyError> {
28 let data: &DeletePost = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32 let post_id = data.post_id;
33 let orig_post = Post::read(context.pool(), post_id).await?;
35 // Dont delete it if its already been deleted.
36 if orig_post.deleted == data.deleted {
37 return Err(LemmyError::from_message("couldnt_update_post"));
41 local_user_view.person.id,
42 orig_post.community_id,
46 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
48 // Verify that only the creator can delete
49 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
50 return Err(LemmyError::from_message("no_post_edit_allowed"));
54 let post_id = data.post_id;
55 let deleted = data.deleted;
56 let updated_post = Post::update(
59 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
63 let res = send_post_ws_message(
65 UserOperationCrud::DeletePost,
67 Some(local_user_view.person.id),
73 let community = Community::read(context.pool(), orig_post.community_id).await?;
74 let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
75 send_apub_delete_in_community(
76 local_user_view.person,