]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/remove.rs
Speedup CI (#3852)
[lemmy.git] / crates / api_crud / src / post / remove.rs
1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4   build_response::build_post_response,
5   context::LemmyContext,
6   post::{PostResponse, RemovePost},
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
9 };
10 use lemmy_db_schema::{
11   source::{
12     moderator::{ModRemovePost, ModRemovePostForm},
13     post::{Post, PostUpdateForm},
14   },
15   traits::Crud,
16 };
17 use lemmy_utils::error::LemmyError;
18
19 #[tracing::instrument(skip(context))]
20 pub async fn remove_post(
21   data: Json<RemovePost>,
22   context: Data<LemmyContext>,
23 ) -> Result<Json<PostResponse>, LemmyError> {
24   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
25
26   let post_id = data.post_id;
27   let orig_post = Post::read(&mut context.pool(), post_id).await?;
28
29   check_community_ban(
30     local_user_view.person.id,
31     orig_post.community_id,
32     &mut context.pool(),
33   )
34   .await?;
35
36   // Verify that only the mods can remove
37   is_mod_or_admin(
38     &mut context.pool(),
39     local_user_view.person.id,
40     orig_post.community_id,
41   )
42   .await?;
43
44   // Update the post
45   let post_id = data.post_id;
46   let removed = data.removed;
47   let post = Post::update(
48     &mut context.pool(),
49     post_id,
50     &PostUpdateForm {
51       removed: Some(removed),
52       ..Default::default()
53     },
54   )
55   .await?;
56
57   // Mod tables
58   let form = ModRemovePostForm {
59     mod_person_id: local_user_view.person.id,
60     post_id: data.post_id,
61     removed: Some(removed),
62     reason: data.reason.clone(),
63   };
64   ModRemovePost::create(&mut context.pool(), &form).await?;
65
66   let person_id = local_user_view.person.id;
67   ActivityChannel::submit_activity(
68     SendActivityData::RemovePost(post, local_user_view.person, data.0),
69     &context,
70   )
71   .await?;
72
73   build_post_response(&context, orig_post.community_id, person_id, post_id).await
74 }