1 use crate::{community::send_community_websocket, 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::CommunityType;
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::{
11 community_moderator_view::CommunityModeratorView,
12 community_view::CommunityView,
14 use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
15 use lemmy_websocket::{LemmyContext, UserOperationCrud};
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for DeleteCommunity {
19 type Response = CommunityResponse;
23 context: &Data<LemmyContext>,
24 websocket_id: Option<ConnectionId>,
25 ) -> Result<CommunityResponse, LemmyError> {
26 let data: &DeleteCommunity = &self;
27 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).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("no_community_edit_allowed").into());
42 let community_id = data.community_id;
43 let deleted = data.deleted;
44 let updated_community = match blocking(context.pool(), move |conn| {
45 Community::update_deleted(conn, community_id, deleted)
49 Ok(community) => community,
50 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
56 .send_delete(local_user_view.person.to_owned(), context)
60 .send_undo_delete(local_user_view.person.to_owned(), context)
64 let community_id = data.community_id;
65 let person_id = local_user_view.person.id;
66 let community_view = blocking(context.pool(), move |conn| {
67 CommunityView::read(conn, community_id, Some(person_id))
71 let res = CommunityResponse { community_view };
73 send_community_websocket(
77 UserOperationCrud::DeleteCommunity,
84 #[async_trait::async_trait(?Send)]
85 impl PerformCrud for RemoveCommunity {
86 type Response = CommunityResponse;
90 context: &Data<LemmyContext>,
91 websocket_id: Option<ConnectionId>,
92 ) -> Result<CommunityResponse, LemmyError> {
93 let data: &RemoveCommunity = &self;
94 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
96 // Verify its an admin (only an admin can remove a community)
97 is_admin(&local_user_view)?;
100 let community_id = data.community_id;
101 let removed = data.removed;
102 let updated_community = match blocking(context.pool(), move |conn| {
103 Community::update_removed(conn, community_id, removed)
107 Ok(community) => community,
108 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
112 let expires = data.expires.map(naive_from_unix);
113 let form = ModRemoveCommunityForm {
114 mod_person_id: local_user_view.person.id,
115 community_id: data.community_id,
116 removed: Some(removed),
117 reason: data.reason.to_owned(),
120 blocking(context.pool(), move |conn| {
121 ModRemoveCommunity::create(conn, &form)
127 updated_community.send_remove(context).await?;
129 updated_community.send_undo_remove(context).await?;
132 let community_id = data.community_id;
133 let person_id = local_user_view.person.id;
134 let community_view = blocking(context.pool(), move |conn| {
135 CommunityView::read(conn, community_id, Some(person_id))
139 let res = CommunityResponse { community_view };
141 send_community_websocket(
145 UserOperationCrud::RemoveCommunity,