]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Rewrite remaining federation actions, get rid of PerformCrud trait (#3794)
[lemmy.git] / crates / api_crud / src / community / delete.rs
1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4   build_response::build_community_response,
5   community::{CommunityResponse, DeleteCommunity},
6   context::LemmyContext,
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{is_top_mod, local_user_view_from_jwt},
9 };
10 use lemmy_db_schema::{
11   source::community::{Community, CommunityUpdateForm},
12   traits::Crud,
13 };
14 use lemmy_db_views_actor::structs::CommunityModeratorView;
15 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
16
17 #[tracing::instrument(skip(context))]
18 pub async fn delete_community(
19   data: Json<DeleteCommunity>,
20   context: Data<LemmyContext>,
21 ) -> Result<Json<CommunityResponse>, LemmyError> {
22   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
23
24   // Fetch the community mods
25   let community_id = data.community_id;
26   let community_mods =
27     CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
28
29   // Make sure deleter is the top mod
30   is_top_mod(&local_user_view, &community_mods)?;
31
32   // Do the delete
33   let community_id = data.community_id;
34   let deleted = data.deleted;
35   let community = Community::update(
36     &mut context.pool(),
37     community_id,
38     &CommunityUpdateForm::builder()
39       .deleted(Some(deleted))
40       .build(),
41   )
42   .await
43   .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
44
45   ActivityChannel::submit_activity(
46     SendActivityData::DeleteCommunity(local_user_view.person.clone(), community, data.deleted),
47     &context,
48   )
49   .await?;
50
51   build_community_response(&context, local_user_view, community_id).await
52 }