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},
7 utils::{is_admin, local_user_view_from_jwt},
11 community::{Community, CommunityUpdateForm},
12 moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
16 use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix};
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for RemoveCommunity {
20 type Response = CommunityResponse;
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?;
27 // Verify its an admin (only an admin can remove a community)
28 is_admin(&local_user_view)?;
31 let community_id = data.community_id;
32 let removed = data.removed;
36 &CommunityUpdateForm::builder()
37 .removed(Some(removed))
41 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
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(),
52 ModRemoveCommunity::create(context.pool(), &form).await?;
54 build_community_response(context, local_user_view, community_id).await