]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
97641f57ece30defab415baeec8791183a3b6d83
[lemmy.git] / crates / api_crud / src / community / delete.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, DeleteCommunity},
6   context::LemmyContext,
7   utils::{is_top_mod, local_user_view_from_jwt},
8 };
9 use lemmy_db_schema::{
10   source::community::{Community, CommunityUpdateForm},
11   traits::Crud,
12 };
13 use lemmy_db_views_actor::structs::CommunityModeratorView;
14 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
15
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeleteCommunity {
18   type Response = CommunityResponse;
19
20   #[tracing::instrument(skip(context))]
21   async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommunityResponse, LemmyError> {
22     let data: &DeleteCommunity = self;
23     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
24
25     // Fetch the community mods
26     let community_id = data.community_id;
27     let community_mods =
28       CommunityModeratorView::for_community(context.pool(), community_id).await?;
29
30     // Make sure deleter is the top mod
31     is_top_mod(&local_user_view, &community_mods)?;
32
33     // Do the delete
34     let community_id = data.community_id;
35     let deleted = data.deleted;
36     Community::update(
37       context.pool(),
38       community_id,
39       &CommunityUpdateForm::builder()
40         .deleted(Some(deleted))
41         .build(),
42     )
43     .await
44     .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
45
46     build_community_response(context, local_user_view, community_id).await
47   }
48 }