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::{
14 source::{community::Community, post::Post},
17 use lemmy_utils::{ConnectionId, LemmyError};
18 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for DeletePost {
22 type Response = PostResponse;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<PostResponse, LemmyError> {
30 let data: &DeletePost = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 let post_id = data.post_id;
35 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
37 // Dont delete it if its already been deleted.
38 if orig_post.deleted == data.deleted {
39 return Err(LemmyError::from_message("couldnt_update_post"));
43 local_user_view.person.id,
44 orig_post.community_id,
48 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
50 // Verify that only the creator can delete
51 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
52 return Err(LemmyError::from_message("no_post_edit_allowed"));
56 let post_id = data.post_id;
57 let deleted = data.deleted;
58 let updated_post = blocking(context.pool(), move |conn| {
59 Post::update_deleted(conn, post_id, deleted)
63 let res = send_post_ws_message(
65 UserOperationCrud::DeletePost,
67 Some(local_user_view.person.id),
73 let community = blocking(context.pool(), move |conn| {
74 Community::read(conn, orig_post.community_id)
77 let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
78 send_apub_delete_in_community(
79 local_user_view.person,