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},
6 websocket::{send::send_post_ws_message, UserOperationCrud},
9 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
10 use lemmy_db_schema::{
13 post::{Post, PostUpdateForm},
17 use lemmy_utils::{error::LemmyError, ConnectionId};
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for DeletePost {
21 type Response = PostResponse;
23 #[tracing::instrument(skip(context, websocket_id))]
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<PostResponse, LemmyError> {
29 let data: &DeletePost = self;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 let post_id = data.post_id;
34 let orig_post = Post::read(context.pool(), post_id).await?;
36 // Dont delete it if its already been deleted.
37 if orig_post.deleted == data.deleted {
38 return Err(LemmyError::from_message("couldnt_update_post"));
42 local_user_view.person.id,
43 orig_post.community_id,
47 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
49 // Verify that only the creator can delete
50 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
51 return Err(LemmyError::from_message("no_post_edit_allowed"));
55 let post_id = data.post_id;
56 let deleted = data.deleted;
57 let updated_post = Post::update(
60 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
64 let res = send_post_ws_message(
66 UserOperationCrud::DeletePost,
68 Some(local_user_view.person.id),
74 let community = Community::read(context.pool(), orig_post.community_id).await?;
75 let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
76 send_apub_delete_in_community(
77 local_user_view.person,