]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/remove.rs
add enable_federated_downvotes site option
[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 {
39       removed: Some(removed),
40       ..Default::default()
41     },
42   )
43   .await
44   .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
45
46   // Mod tables
47   let expires = data.expires.map(naive_from_unix);
48   let form = ModRemoveCommunityForm {
49     mod_person_id: local_user_view.person.id,
50     community_id: data.community_id,
51     removed: Some(removed),
52     reason: data.reason.clone(),
53     expires,
54   };
55   ModRemoveCommunity::create(&mut context.pool(), &form).await?;
56
57   ActivityChannel::submit_activity(
58     SendActivityData::RemoveCommunity(
59       local_user_view.person.clone(),
60       community,
61       data.reason.clone(),
62       data.removed,
63     ),
64     &context,
65   )
66   .await?;
67
68   build_community_response(&context, local_user_view, community_id).await
69 }