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, get_local_user_view_from_jwt},
7 websocket::{send::send_post_ws_message, 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;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 let post_id = data.post_id;
30 let orig_post = Post::read(context.pool(), post_id).await?;
32 // Dont delete it if its already been deleted.
33 if orig_post.deleted == data.deleted {
34 return Err(LemmyError::from_message("couldnt_update_post"));
38 local_user_view.person.id,
39 orig_post.community_id,
43 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
45 // Verify that only the creator can delete
46 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
47 return Err(LemmyError::from_message("no_post_edit_allowed"));
51 let post_id = data.post_id;
52 let deleted = data.deleted;
56 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
60 let res = send_post_ws_message(
62 UserOperationCrud::DeletePost,
64 Some(local_user_view.person.id),