]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Rewrite delete activities (#1699)
[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 = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
25
26     // Fetch the community mods
27     let community_id = data.community_id;
28     let community_mods = blocking(context.pool(), move |conn| {
29       CommunityModeratorView::for_community(conn, community_id)
30     })
31     .await??;
32
33     // Make sure deleter is the top mod
34     if local_user_view.person.id != community_mods[0].moderator.id {
35       return Err(ApiError::err("no_community_edit_allowed").into());
36     }
37
38     // Do the delete
39     let community_id = data.community_id;
40     let deleted = data.deleted;
41     let updated_community = blocking(context.pool(), move |conn| {
42       Community::update_deleted(conn, community_id, deleted)
43     })
44     .await?
45     .map_err(|_| ApiError::err("couldnt_update_community"))?;
46
47     // Send apub messages
48     send_apub_delete(
49       &local_user_view.person,
50       &updated_community,
51       updated_community.actor_id.clone().into(),
52       deleted,
53       context,
54     )
55     .await?;
56
57     send_community_ws_message(
58       data.community_id,
59       UserOperationCrud::DeleteCommunity,
60       websocket_id,
61       Some(local_user_view.person.id),
62       context,
63     )
64     .await
65   }
66 }
67
68 #[async_trait::async_trait(?Send)]
69 impl PerformCrud for RemoveCommunity {
70   type Response = CommunityResponse;
71
72   async fn perform(
73     &self,
74     context: &Data<LemmyContext>,
75     websocket_id: Option<ConnectionId>,
76   ) -> Result<CommunityResponse, LemmyError> {
77     let data: &RemoveCommunity = self;
78     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
79
80     // Verify its an admin (only an admin can remove a community)
81     is_admin(&local_user_view)?;
82
83     // Do the remove
84     let community_id = data.community_id;
85     let removed = data.removed;
86     let updated_community = blocking(context.pool(), move |conn| {
87       Community::update_removed(conn, community_id, removed)
88     })
89     .await?
90     .map_err(|_| ApiError::err("couldnt_update_community"))?;
91
92     // Mod tables
93     let expires = data.expires.map(naive_from_unix);
94     let form = ModRemoveCommunityForm {
95       mod_person_id: local_user_view.person.id,
96       community_id: data.community_id,
97       removed: Some(removed),
98       reason: data.reason.to_owned(),
99       expires,
100     };
101     blocking(context.pool(), move |conn| {
102       ModRemoveCommunity::create(conn, &form)
103     })
104     .await??;
105
106     // Apub messages
107     send_apub_remove(
108       &local_user_view.person,
109       &updated_community,
110       updated_community.actor_id.clone().into(),
111       data.reason.clone().unwrap_or_else(|| "".to_string()),
112       removed,
113       context,
114     )
115     .await?;
116
117     send_community_ws_message(
118       data.community_id,
119       UserOperationCrud::RemoveCommunity,
120       websocket_id,
121       Some(local_user_view.person.id),
122       context,
123     )
124     .await
125   }
126 }