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;
25 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
27 // Fetch the community mods
28 let community_id = data.community_id;
29 let community_mods = blocking(context.pool(), move |conn| {
30 CommunityModeratorView::for_community(conn, community_id)
34 // Make sure deleter is the top mod
35 if local_user_view.person.id != community_mods[0].moderator.id {
36 return Err(ApiError::err_plain("no_community_edit_allowed").into());
40 let community_id = data.community_id;
41 let deleted = data.deleted;
42 let updated_community = blocking(context.pool(), move |conn| {
43 Community::update_deleted(conn, community_id, deleted)
46 .map_err(|e| ApiError::err("couldnt_update_community", e))?;
50 &local_user_view.person,
52 updated_community.actor_id.clone().into(),
58 send_community_ws_message(
60 UserOperationCrud::DeleteCommunity,
62 Some(local_user_view.person.id),
69 #[async_trait::async_trait(?Send)]
70 impl PerformCrud for RemoveCommunity {
71 type Response = CommunityResponse;
75 context: &Data<LemmyContext>,
76 websocket_id: Option<ConnectionId>,
77 ) -> Result<CommunityResponse, LemmyError> {
78 let data: &RemoveCommunity = self;
80 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
82 // Verify its an admin (only an admin can remove a community)
83 is_admin(&local_user_view)?;
86 let community_id = data.community_id;
87 let removed = data.removed;
88 let updated_community = blocking(context.pool(), move |conn| {
89 Community::update_removed(conn, community_id, removed)
92 .map_err(|e| ApiError::err("couldnt_update_community", e))?;
95 let expires = data.expires.map(naive_from_unix);
96 let form = ModRemoveCommunityForm {
97 mod_person_id: local_user_view.person.id,
98 community_id: data.community_id,
99 removed: Some(removed),
100 reason: data.reason.to_owned(),
103 blocking(context.pool(), move |conn| {
104 ModRemoveCommunity::create(conn, &form)
110 &local_user_view.person,
112 updated_community.actor_id.clone().into(),
113 data.reason.clone().unwrap_or_else(|| "".to_string()),
119 send_community_ws_message(
121 UserOperationCrud::RemoveCommunity,
123 Some(local_user_view.person.id),