]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/remove.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / api_crud / src / post / remove.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   post::{PostResponse, RemovePost},
5   utils::{blocking, check_community_ban, get_local_user_view_from_jwt, is_mod_or_admin},
6 };
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
8 use lemmy_db_schema::{
9   source::{
10     community::Community,
11     moderator::{ModRemovePost, ModRemovePostForm},
12     post::{Post, PostUpdateForm},
13   },
14   traits::Crud,
15 };
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
18
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for RemovePost {
21   type Response = PostResponse;
22
23   #[tracing::instrument(skip(context, websocket_id))]
24   async fn perform(
25     &self,
26     context: &Data<LemmyContext>,
27     websocket_id: Option<ConnectionId>,
28   ) -> Result<PostResponse, LemmyError> {
29     let data: &RemovePost = self;
30     let local_user_view =
31       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32
33     let post_id = data.post_id;
34     let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
35
36     check_community_ban(
37       local_user_view.person.id,
38       orig_post.community_id,
39       context.pool(),
40     )
41     .await?;
42
43     // Verify that only the mods can remove
44     is_mod_or_admin(
45       context.pool(),
46       local_user_view.person.id,
47       orig_post.community_id,
48     )
49     .await?;
50
51     // Update the post
52     let post_id = data.post_id;
53     let removed = data.removed;
54     let updated_post = blocking(context.pool(), move |conn| {
55       Post::update(
56         conn,
57         post_id,
58         &PostUpdateForm::builder().removed(Some(removed)).build(),
59       )
60     })
61     .await??;
62
63     // Mod tables
64     let form = ModRemovePostForm {
65       mod_person_id: local_user_view.person.id,
66       post_id: data.post_id,
67       removed: Some(removed),
68       reason: data.reason.to_owned(),
69     };
70     blocking(context.pool(), move |conn| {
71       ModRemovePost::create(conn, &form)
72     })
73     .await??;
74
75     let res = send_post_ws_message(
76       data.post_id,
77       UserOperationCrud::RemovePost,
78       websocket_id,
79       Some(local_user_view.person.id),
80       context,
81     )
82     .await?;
83
84     // apub updates
85     let community = blocking(context.pool(), move |conn| {
86       Community::read(conn, orig_post.community_id)
87     })
88     .await??;
89     let deletable = DeletableObjects::Post(Box::new(updated_post.into()));
90     send_apub_delete_in_community(
91       local_user_view.person,
92       community,
93       deletable,
94       data.reason.clone().or_else(|| Some("".to_string())),
95       removed,
96       context,
97     )
98     .await?;
99     Ok(res)
100   }
101 }