]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[lemmy.git] / crates / api_crud / src / community / delete.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   community::{CommunityResponse, DeleteCommunity},
5   utils::{blocking, get_local_user_view_from_jwt},
6 };
7 use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
8 use lemmy_db_schema::source::community::Community;
9 use lemmy_db_views_actor::structs::CommunityModeratorView;
10 use lemmy_utils::{ConnectionId, LemmyError};
11 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
12
13 #[async_trait::async_trait(?Send)]
14 impl PerformCrud for DeleteCommunity {
15   type Response = CommunityResponse;
16
17   #[tracing::instrument(skip(context, websocket_id))]
18   async fn perform(
19     &self,
20     context: &Data<LemmyContext>,
21     websocket_id: Option<ConnectionId>,
22   ) -> Result<CommunityResponse, LemmyError> {
23     let data: &DeleteCommunity = self;
24     let local_user_view =
25       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
26
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)
31     })
32     .await??;
33
34     // Make sure deleter is the top mod
35     if local_user_view.person.id != community_mods[0].moderator.id {
36       return Err(LemmyError::from_message("no_community_edit_allowed"));
37     }
38
39     // Do the delete
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)
44     })
45     .await?
46     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
47
48     let res = send_community_ws_message(
49       data.community_id,
50       UserOperationCrud::DeleteCommunity,
51       websocket_id,
52       Some(local_user_view.person.id),
53       context,
54     )
55     .await?;
56
57     // Send apub messages
58     let deletable = DeletableObjects::Community(Box::new(updated_community.clone().into()));
59     send_apub_delete_in_community(
60       local_user_view.person,
61       updated_community,
62       deletable,
63       None,
64       deleted,
65       context,
66     )
67     .await?;
68
69     Ok(res)
70   }
71 }