]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Merge branch 'remove_settings_and_secret_singletons_squashed'
[lemmy.git] / crates / api_crud / src / community / delete.rs
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::{
7   community::*,
8   moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
9 };
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};
13
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for DeleteCommunity {
16   type Response = CommunityResponse;
17
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(ApiError::err("no_community_edit_allowed").into());
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(|_| ApiError::err("couldnt_update_community"))?;
47
48     // Send apub messages
49     send_apub_delete(
50       &local_user_view.person,
51       &updated_community,
52       updated_community.actor_id.clone().into(),
53       deleted,
54       context,
55     )
56     .await?;
57
58     send_community_ws_message(
59       data.community_id,
60       UserOperationCrud::DeleteCommunity,
61       websocket_id,
62       Some(local_user_view.person.id),
63       context,
64     )
65     .await
66   }
67 }
68
69 #[async_trait::async_trait(?Send)]
70 impl PerformCrud for RemoveCommunity {
71   type Response = CommunityResponse;
72
73   async fn perform(
74     &self,
75     context: &Data<LemmyContext>,
76     websocket_id: Option<ConnectionId>,
77   ) -> Result<CommunityResponse, LemmyError> {
78     let data: &RemoveCommunity = self;
79     let local_user_view =
80       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
81
82     // Verify its an admin (only an admin can remove a community)
83     is_admin(&local_user_view)?;
84
85     // Do the remove
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)
90     })
91     .await?
92     .map_err(|_| ApiError::err("couldnt_update_community"))?;
93
94     // Mod tables
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(),
101       expires,
102     };
103     blocking(context.pool(), move |conn| {
104       ModRemoveCommunity::create(conn, &form)
105     })
106     .await??;
107
108     // Apub messages
109     send_apub_remove(
110       &local_user_view.person,
111       &updated_community,
112       updated_community.actor_id.clone().into(),
113       data.reason.clone().unwrap_or_else(|| "".to_string()),
114       removed,
115       context,
116     )
117     .await?;
118
119     send_community_ws_message(
120       data.community_id,
121       UserOperationCrud::RemoveCommunity,
122       websocket_id,
123       Some(local_user_view.person.id),
124       context,
125     )
126     .await
127   }
128 }