1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{DeletePost, PostResponse},
8 check_community_deleted_or_removed,
9 get_local_user_view_from_jwt,
12 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
13 use lemmy_db_schema::{
16 post::{Post, PostUpdateForm},
20 use lemmy_utils::{error::LemmyError, ConnectionId};
21 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
23 #[async_trait::async_trait(?Send)]
24 impl PerformCrud for DeletePost {
25 type Response = PostResponse;
27 #[tracing::instrument(skip(context, websocket_id))]
30 context: &Data<LemmyContext>,
31 websocket_id: Option<ConnectionId>,
32 ) -> Result<PostResponse, LemmyError> {
33 let data: &DeletePost = self;
35 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37 let post_id = data.post_id;
38 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
40 // Dont delete it if its already been deleted.
41 if orig_post.deleted == data.deleted {
42 return Err(LemmyError::from_message("couldnt_update_post"));
46 local_user_view.person.id,
47 orig_post.community_id,
51 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
53 // Verify that only the creator can delete
54 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
55 return Err(LemmyError::from_message("no_post_edit_allowed"));
59 let post_id = data.post_id;
60 let deleted = data.deleted;
61 let updated_post = blocking(context.pool(), move |conn| {
65 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
70 let res = send_post_ws_message(
72 UserOperationCrud::DeletePost,
74 Some(local_user_view.person.id),
80 let community = blocking(context.pool(), move |conn| {
81 Community::read(conn, orig_post.community_id)
84 let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
85 send_apub_delete_in_community(
86 local_user_view.person,