]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/remove.rs
b79b2a66658cbb3345922b61d0d4976e9609616d
[lemmy.git] / crates / api_crud / src / community / remove.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, RemoveCommunity},
6   context::LemmyContext,
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{is_admin, local_user_view_from_jwt},
9 };
10 use lemmy_db_schema::{
11   source::{
12     community::{Community, CommunityUpdateForm},
13     moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
14   },
15   traits::Crud,
16 };
17 use lemmy_utils::{
18   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
19   utils::time::naive_from_unix,
20 };
21
22 #[tracing::instrument(skip(context))]
23 pub async fn remove_community(
24   data: Json<RemoveCommunity>,
25   context: Data<LemmyContext>,
26 ) -> Result<Json<CommunityResponse>, LemmyError> {
27   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
28
29   // Verify its an admin (only an admin can remove a community)
30   is_admin(&local_user_view)?;
31
32   // Do the remove
33   let community_id = data.community_id;
34   let removed = data.removed;
35   let community = Community::update(
36     &mut context.pool(),
37     community_id,
38     &CommunityUpdateForm::builder()
39       .removed(Some(removed))
40       .build(),
41   )
42   .await
43   .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
44
45   // Mod tables
46   let expires = data.expires.map(naive_from_unix);
47   let form = ModRemoveCommunityForm {
48     mod_person_id: local_user_view.person.id,
49     community_id: data.community_id,
50     removed: Some(removed),
51     reason: data.reason.clone(),
52     expires,
53   };
54   ModRemoveCommunity::create(&mut context.pool(), &form).await?;
55
56   ActivityChannel::submit_activity(
57     SendActivityData::RemoveCommunity(
58       local_user_view.person.clone(),
59       community,
60       data.reason.clone(),
61       data.removed,
62     ),
63     &context,
64   )
65   .await?;
66
67   build_community_response(&context, local_user_view, community_id).await
68 }