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};
8 moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
12 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
13 use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeleteCommunity {
18 type Response = CommunityResponse;
22 context: &Data<LemmyContext>,
23 websocket_id: Option<ConnectionId>,
24 ) -> Result<CommunityResponse, LemmyError> {
25 let data: &DeleteCommunity = self;
27 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29 // Fetch the community mods
30 let community_id = data.community_id;
31 let community_mods = blocking(context.pool(), move |conn| {
32 CommunityModeratorView::for_community(conn, community_id)
36 // Make sure deleter is the top mod
37 if local_user_view.person.id != community_mods[0].moderator.id {
38 return Err(ApiError::err_plain("no_community_edit_allowed").into());
42 let community_id = data.community_id;
43 let deleted = data.deleted;
44 let updated_community = blocking(context.pool(), move |conn| {
45 Community::update_deleted(conn, community_id, deleted)
48 .map_err(|e| ApiError::err("couldnt_update_community", e))?;
52 &local_user_view.person.clone().into(),
53 &updated_community.clone().into(),
54 updated_community.actor_id.clone().into(),
60 send_community_ws_message(
62 UserOperationCrud::DeleteCommunity,
64 Some(local_user_view.person.id),
71 #[async_trait::async_trait(?Send)]
72 impl PerformCrud for RemoveCommunity {
73 type Response = CommunityResponse;
77 context: &Data<LemmyContext>,
78 websocket_id: Option<ConnectionId>,
79 ) -> Result<CommunityResponse, LemmyError> {
80 let data: &RemoveCommunity = self;
82 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
84 // Verify its an admin (only an admin can remove a community)
85 is_admin(&local_user_view)?;
88 let community_id = data.community_id;
89 let removed = data.removed;
90 let updated_community = blocking(context.pool(), move |conn| {
91 Community::update_removed(conn, community_id, removed)
94 .map_err(|e| ApiError::err("couldnt_update_community", e))?;
97 let expires = data.expires.map(naive_from_unix);
98 let form = ModRemoveCommunityForm {
99 mod_person_id: local_user_view.person.id,
100 community_id: data.community_id,
101 removed: Some(removed),
102 reason: data.reason.to_owned(),
105 blocking(context.pool(), move |conn| {
106 ModRemoveCommunity::create(conn, &form)
112 &local_user_view.person.clone().into(),
113 &updated_community.clone().into(),
114 updated_community.actor_id.clone().into(),
115 data.reason.clone().unwrap_or_else(|| "".to_string()),
121 send_community_ws_message(
123 UserOperationCrud::RemoveCommunity,
125 Some(local_user_view.person.id),