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::community_view::CommunityView;
11 use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::{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 // Verify its the creator (only a creator can delete the community)
27 let community_id = data.community_id;
28 let read_community = blocking(context.pool(), move |conn| {
29 Community::read(conn, community_id)
32 if read_community.creator_id != local_user_view.person.id {
33 return Err(ApiError::err("no_community_edit_allowed").into());
37 let community_id = data.community_id;
38 let deleted = data.deleted;
39 let updated_community = match blocking(context.pool(), move |conn| {
40 Community::update_deleted(conn, community_id, deleted)
44 Ok(community) => community,
45 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
50 updated_community.send_delete(context).await?;
52 updated_community.send_undo_delete(context).await?;
55 let community_id = data.community_id;
56 let person_id = local_user_view.person.id;
57 let community_view = blocking(context.pool(), move |conn| {
58 CommunityView::read(conn, community_id, Some(person_id))
62 let res = CommunityResponse { community_view };
64 send_community_websocket(
68 UserOperationCrud::DeleteCommunity,
75 #[async_trait::async_trait(?Send)]
76 impl PerformCrud for RemoveCommunity {
77 type Response = CommunityResponse;
81 context: &Data<LemmyContext>,
82 websocket_id: Option<ConnectionId>,
83 ) -> Result<CommunityResponse, LemmyError> {
84 let data: &RemoveCommunity = &self;
85 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
87 // Verify its an admin (only an admin can remove a community)
88 is_admin(&local_user_view)?;
91 let community_id = data.community_id;
92 let removed = data.removed;
93 let updated_community = match blocking(context.pool(), move |conn| {
94 Community::update_removed(conn, community_id, removed)
98 Ok(community) => community,
99 Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
103 let expires = data.expires.map(naive_from_unix);
104 let form = ModRemoveCommunityForm {
105 mod_person_id: local_user_view.person.id,
106 community_id: data.community_id,
107 removed: Some(removed),
108 reason: data.reason.to_owned(),
111 blocking(context.pool(), move |conn| {
112 ModRemoveCommunity::create(conn, &form)
118 updated_community.send_remove(context).await?;
120 updated_community.send_undo_remove(context).await?;
123 let community_id = data.community_id;
124 let person_id = local_user_view.person.id;
125 let community_view = blocking(context.pool(), move |conn| {
126 CommunityView::read(conn, community_id, Some(person_id))
130 let res = CommunityResponse { community_view };
132 send_community_websocket(
136 UserOperationCrud::RemoveCommunity,