]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/remove.rs
Automatically resolve report when post/comment is removed (#3850)
[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     post_report::PostReport,
15   },
16   traits::{Crud, Reportable},
17 };
18 use lemmy_utils::error::LemmyError;
19
20 #[tracing::instrument(skip(context))]
21 pub async fn remove_post(
22   data: Json<RemovePost>,
23   context: Data<LemmyContext>,
24 ) -> Result<Json<PostResponse>, LemmyError> {
25   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
26
27   let post_id = data.post_id;
28   let orig_post = Post::read(&mut context.pool(), post_id).await?;
29
30   check_community_ban(
31     local_user_view.person.id,
32     orig_post.community_id,
33     &mut context.pool(),
34   )
35   .await?;
36
37   // Verify that only the mods can remove
38   is_mod_or_admin(
39     &mut context.pool(),
40     local_user_view.person.id,
41     orig_post.community_id,
42   )
43   .await?;
44
45   // Update the post
46   let post_id = data.post_id;
47   let removed = data.removed;
48   let post = Post::update(
49     &mut context.pool(),
50     post_id,
51     &PostUpdateForm {
52       removed: Some(removed),
53       ..Default::default()
54     },
55   )
56   .await?;
57
58   PostReport::resolve_all_for_object(&mut context.pool(), post_id, local_user_view.person.id)
59     .await?;
60
61   // Mod tables
62   let form = ModRemovePostForm {
63     mod_person_id: local_user_view.person.id,
64     post_id: data.post_id,
65     removed: Some(removed),
66     reason: data.reason.clone(),
67   };
68   ModRemovePost::create(&mut context.pool(), &form).await?;
69
70   let person_id = local_user_view.person.id;
71   ActivityChannel::submit_activity(
72     SendActivityData::RemovePost(post, local_user_view.person, data.0),
73     &context,
74   )
75   .await?;
76
77   build_post_response(&context, orig_post.community_id, person_id, post_id).await
78 }