]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/remove.rs
Remove chatserver (#2919)
[lemmy.git] / crates / api_crud / src / community / remove.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   build_response::build_community_response,
5   community::{CommunityResponse, RemoveCommunity},
6   context::LemmyContext,
7   utils::{is_admin, local_user_view_from_jwt},
8 };
9 use lemmy_db_schema::{
10   source::{
11     community::{Community, CommunityUpdateForm},
12     moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
13   },
14   traits::Crud,
15 };
16 use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix};
17
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for RemoveCommunity {
20   type Response = CommunityResponse;
21
22   #[tracing::instrument(skip(context))]
23   async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
24     let data: &RemoveCommunity = self;
25     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
26
27     // Verify its an admin (only an admin can remove a community)
28     is_admin(&local_user_view)?;
29
30     // Do the remove
31     let community_id = data.community_id;
32     let removed = data.removed;
33     Community::update(
34       context.pool(),
35       community_id,
36       &CommunityUpdateForm::builder()
37         .removed(Some(removed))
38         .build(),
39     )
40     .await
41     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
42
43     // Mod tables
44     let expires = data.expires.map(naive_from_unix);
45     let form = ModRemoveCommunityForm {
46       mod_person_id: local_user_view.person.id,
47       community_id: data.community_id,
48       removed: Some(removed),
49       reason: data.reason.clone(),
50       expires,
51     };
52     ModRemoveCommunity::create(context.pool(), &form).await?;
53
54     build_community_response(context, local_user_view, community_id).await
55   }
56 }