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, send_apub_remove};
12 use lemmy_db_schema::{
15 moderator::{ModRemovePost, ModRemovePostForm},
20 use lemmy_utils::{ApiError, 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;
29 context: &Data<LemmyContext>,
30 websocket_id: Option<ConnectionId>,
31 ) -> Result<PostResponse, LemmyError> {
32 let data: &DeletePost = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36 let post_id = data.post_id;
37 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
40 local_user_view.person.id,
41 orig_post.community_id,
45 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
47 // Verify that only the creator can delete
48 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
49 return Err(ApiError::err_plain("no_post_edit_allowed").into());
53 let post_id = data.post_id;
54 let deleted = data.deleted;
55 let updated_post = blocking(context.pool(), move |conn| {
56 Post::update_deleted(conn, post_id, deleted)
61 let community = blocking(context.pool(), move |conn| {
62 Community::read(conn, orig_post.community_id)
66 &local_user_view.person,
68 updated_post.ap_id.into(),
76 UserOperationCrud::DeletePost,
78 Some(local_user_view.person.id),
85 #[async_trait::async_trait(?Send)]
86 impl PerformCrud for RemovePost {
87 type Response = PostResponse;
91 context: &Data<LemmyContext>,
92 websocket_id: Option<ConnectionId>,
93 ) -> Result<PostResponse, LemmyError> {
94 let data: &RemovePost = self;
96 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
98 let post_id = data.post_id;
99 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
102 local_user_view.person.id,
103 orig_post.community_id,
108 // Verify that only the mods can remove
111 local_user_view.person.id,
112 orig_post.community_id,
117 let post_id = data.post_id;
118 let removed = data.removed;
119 let updated_post = blocking(context.pool(), move |conn| {
120 Post::update_removed(conn, post_id, removed)
125 let form = ModRemovePostForm {
126 mod_person_id: local_user_view.person.id,
127 post_id: data.post_id,
128 removed: Some(removed),
129 reason: data.reason.to_owned(),
131 blocking(context.pool(), move |conn| {
132 ModRemovePost::create(conn, &form)
137 let community = blocking(context.pool(), move |conn| {
138 Community::read(conn, orig_post.community_id)
142 &local_user_view.person,
144 updated_post.ap_id.into(),
145 data.reason.clone().unwrap_or_else(|| "".to_string()),
151 send_post_ws_message(
153 UserOperationCrud::RemovePost,
155 Some(local_user_view.person.id),