1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::build_post_response,
6 post::{DeletePost, PostResponse},
7 utils::{check_community_ban, check_community_deleted_or_removed, local_user_view_from_jwt},
10 source::post::{Post, PostUpdateForm},
13 use lemmy_utils::error::LemmyError;
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for DeletePost {
17 type Response = PostResponse;
19 #[tracing::instrument(skip(context))]
20 async fn perform(&self, context: &Data<LemmyContext>) -> Result<PostResponse, LemmyError> {
21 let data: &DeletePost = self;
22 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
24 let post_id = data.post_id;
25 let orig_post = Post::read(context.pool(), post_id).await?;
27 // Dont delete it if its already been deleted.
28 if orig_post.deleted == data.deleted {
29 return Err(LemmyError::from_message("couldnt_update_post"));
33 local_user_view.person.id,
34 orig_post.community_id,
38 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
40 // Verify that only the creator can delete
41 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
42 return Err(LemmyError::from_message("no_post_edit_allowed"));
46 let post_id = data.post_id;
47 let deleted = data.deleted;
51 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
57 orig_post.community_id,
58 local_user_view.person.id,