1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 get_local_user_view_from_jwt,
11 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
12 use lemmy_db_schema::{
15 moderator::{ModRemovePost, ModRemovePostForm},
20 use lemmy_utils::{ConnectionId, LemmyError};
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| {
62 Post::update_deleted(conn, post_id, deleted)
66 let res = send_post_ws_message(
68 UserOperationCrud::DeletePost,
70 Some(local_user_view.person.id),
76 let community = blocking(context.pool(), move |conn| {
77 Community::read(conn, orig_post.community_id)
80 let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
81 send_apub_delete_in_community(
82 local_user_view.person,
94 #[async_trait::async_trait(?Send)]
95 impl PerformCrud for RemovePost {
96 type Response = PostResponse;
98 #[tracing::instrument(skip(context, websocket_id))]
101 context: &Data<LemmyContext>,
102 websocket_id: Option<ConnectionId>,
103 ) -> Result<PostResponse, LemmyError> {
104 let data: &RemovePost = self;
105 let local_user_view =
106 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
108 let post_id = data.post_id;
109 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
112 local_user_view.person.id,
113 orig_post.community_id,
118 // Verify that only the mods can remove
121 local_user_view.person.id,
122 orig_post.community_id,
127 let post_id = data.post_id;
128 let removed = data.removed;
129 let updated_post = blocking(context.pool(), move |conn| {
130 Post::update_removed(conn, post_id, removed)
135 let form = ModRemovePostForm {
136 mod_person_id: local_user_view.person.id,
137 post_id: data.post_id,
138 removed: Some(removed),
139 reason: data.reason.to_owned(),
141 blocking(context.pool(), move |conn| {
142 ModRemovePost::create(conn, &form)
146 let res = send_post_ws_message(
148 UserOperationCrud::RemovePost,
150 Some(local_user_view.person.id),
156 let community = blocking(context.pool(), move |conn| {
157 Community::read(conn, orig_post.community_id)
160 let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
161 send_apub_delete_in_community(
162 local_user_view.person,
165 data.reason.clone().or_else(|| Some("".to_string())),