1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin};
4 use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
5 use lemmy_db_queries::{source::community::Community_, Crud};
6 use lemmy_db_schema::source::{
8 moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
10 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
11 use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for DeleteCommunity {
16 type Response = CommunityResponse;
20 context: &Data<LemmyContext>,
21 websocket_id: Option<ConnectionId>,
22 ) -> Result<CommunityResponse, LemmyError> {
23 let data: &DeleteCommunity = self;
24 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
26 // Fetch the community mods
27 let community_id = data.community_id;
28 let community_mods = blocking(context.pool(), move |conn| {
29 CommunityModeratorView::for_community(conn, community_id)
33 // Make sure deleter is the top mod
34 if local_user_view.person.id != community_mods[0].moderator.id {
35 return Err(ApiError::err("no_community_edit_allowed").into());
39 let community_id = data.community_id;
40 let deleted = data.deleted;
41 let updated_community = blocking(context.pool(), move |conn| {
42 Community::update_deleted(conn, community_id, deleted)
45 .map_err(|_| ApiError::err("couldnt_update_community"))?;
49 &local_user_view.person,
51 updated_community.actor_id.clone().into(),
57 send_community_ws_message(
59 UserOperationCrud::DeleteCommunity,
61 Some(local_user_view.person.id),
68 #[async_trait::async_trait(?Send)]
69 impl PerformCrud for RemoveCommunity {
70 type Response = CommunityResponse;
74 context: &Data<LemmyContext>,
75 websocket_id: Option<ConnectionId>,
76 ) -> Result<CommunityResponse, LemmyError> {
77 let data: &RemoveCommunity = self;
78 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
80 // Verify its an admin (only an admin can remove a community)
81 is_admin(&local_user_view)?;
84 let community_id = data.community_id;
85 let removed = data.removed;
86 let updated_community = blocking(context.pool(), move |conn| {
87 Community::update_removed(conn, community_id, removed)
90 .map_err(|_| ApiError::err("couldnt_update_community"))?;
93 let expires = data.expires.map(naive_from_unix);
94 let form = ModRemoveCommunityForm {
95 mod_person_id: local_user_view.person.id,
96 community_id: data.community_id,
97 removed: Some(removed),
98 reason: data.reason.to_owned(),
101 blocking(context.pool(), move |conn| {
102 ModRemoveCommunity::create(conn, &form)
108 &local_user_view.person,
110 updated_community.actor_id.clone().into(),
111 data.reason.clone().unwrap_or_else(|| "".to_string()),
117 send_community_ws_message(
119 UserOperationCrud::RemoveCommunity,
121 Some(local_user_view.person.id),