]> Untitled Git - lemmy.git/blob - crates/api_crud/src/post/remove.rs
Make functions work with both connection and pool (#3420)
[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   build_response::build_post_response,
5   context::LemmyContext,
6   post::{PostResponse, RemovePost},
7   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     moderator::{ModRemovePost, ModRemovePostForm},
12     post::{Post, PostUpdateForm},
13   },
14   traits::Crud,
15 };
16 use lemmy_utils::error::LemmyError;
17
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for RemovePost {
20   type Response = PostResponse;
21
22   #[tracing::instrument(skip(context))]
23   async fn perform(&self, context: &Data<LemmyContext>) -> Result<PostResponse, LemmyError> {
24     let data: &RemovePost = self;
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     Post::update(
49       &mut context.pool(),
50       post_id,
51       &PostUpdateForm::builder().removed(Some(removed)).build(),
52     )
53     .await?;
54
55     // Mod tables
56     let form = ModRemovePostForm {
57       mod_person_id: local_user_view.person.id,
58       post_id: data.post_id,
59       removed: Some(removed),
60       reason: data.reason.clone(),
61     };
62     ModRemovePost::create(&mut context.pool(), &form).await?;
63
64     build_post_response(
65       context,
66       orig_post.community_id,
67       local_user_view.person.id,
68       post_id,
69     )
70     .await
71   }
72 }