1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 get_local_user_view_from_jwt,
10 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
11 use lemmy_db_queries::{source::post::Post_, Crud};
12 use lemmy_db_schema::source::{community::Community, moderator::*, post::*};
13 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeletePost {
18 type Response = PostResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<PostResponse, LemmyError> {
25 let data: &DeletePost = self;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 let post_id = data.post_id;
30 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
33 local_user_view.person.id,
34 orig_post.community_id,
39 // Verify that only the creator can delete
40 if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
41 return Err(ApiError::err_plain("no_post_edit_allowed").into());
45 let post_id = data.post_id;
46 let deleted = data.deleted;
47 let updated_post = blocking(context.pool(), move |conn| {
48 Post::update_deleted(conn, post_id, deleted)
53 let community = blocking(context.pool(), move |conn| {
54 Community::read(conn, orig_post.community_id)
58 &local_user_view.person,
60 updated_post.ap_id.into(),
68 UserOperationCrud::DeletePost,
70 Some(local_user_view.person.id),
77 #[async_trait::async_trait(?Send)]
78 impl PerformCrud for RemovePost {
79 type Response = PostResponse;
83 context: &Data<LemmyContext>,
84 websocket_id: Option<ConnectionId>,
85 ) -> Result<PostResponse, LemmyError> {
86 let data: &RemovePost = self;
88 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
90 let post_id = data.post_id;
91 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
94 local_user_view.person.id,
95 orig_post.community_id,
100 // Verify that only the mods can remove
103 local_user_view.person.id,
104 orig_post.community_id,
109 let post_id = data.post_id;
110 let removed = data.removed;
111 let updated_post = blocking(context.pool(), move |conn| {
112 Post::update_removed(conn, post_id, removed)
117 let form = ModRemovePostForm {
118 mod_person_id: local_user_view.person.id,
119 post_id: data.post_id,
120 removed: Some(removed),
121 reason: data.reason.to_owned(),
123 blocking(context.pool(), move |conn| {
124 ModRemovePost::create(conn, &form)
129 let community = blocking(context.pool(), move |conn| {
130 Community::read(conn, orig_post.community_id)
134 &local_user_view.person,
136 updated_post.ap_id.into(),
137 data.reason.clone().unwrap_or_else(|| "".to_string()),
143 send_post_ws_message(
145 UserOperationCrud::RemovePost,
147 Some(local_user_view.person.id),