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},
7 send_activity::{ActivityChannel, SendActivityData},
8 utils::{is_admin, local_user_view_from_jwt},
10 use lemmy_db_schema::{
12 community::{Community, CommunityUpdateForm},
13 moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
18 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
19 utils::time::naive_from_unix,
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?;
29 // Verify its an admin (only an admin can remove a community)
30 is_admin(&local_user_view)?;
33 let community_id = data.community_id;
34 let removed = data.removed;
35 let community = Community::update(
38 &CommunityUpdateForm {
39 removed: Some(removed),
44 .with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
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(),
55 ModRemoveCommunity::create(&mut context.pool(), &form).await?;
57 ActivityChannel::submit_activity(
58 SendActivityData::RemoveCommunity(
59 local_user_view.person.clone(),
68 build_community_response(&context, local_user_view, community_id).await