]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Rework error handling (fixes #1714) (#2135)
[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_in_community, DeletableObjects};
5 use lemmy_db_schema::{
6   source::{
7     community::Community,
8     moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
9   },
10   traits::Crud,
11 };
12 use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
13 use lemmy_utils::{utils::naive_from_unix, ConnectionId, LemmyError};
14 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
15
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for DeleteCommunity {
18   type Response = CommunityResponse;
19
20   #[tracing::instrument(skip(context, websocket_id))]
21   async fn perform(
22     &self,
23     context: &Data<LemmyContext>,
24     websocket_id: Option<ConnectionId>,
25   ) -> Result<CommunityResponse, LemmyError> {
26     let data: &DeleteCommunity = self;
27     let local_user_view =
28       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
29
30     // Fetch the community mods
31     let community_id = data.community_id;
32     let community_mods = blocking(context.pool(), move |conn| {
33       CommunityModeratorView::for_community(conn, community_id)
34     })
35     .await??;
36
37     // Make sure deleter is the top mod
38     if local_user_view.person.id != community_mods[0].moderator.id {
39       return Err(LemmyError::from_message("no_community_edit_allowed"));
40     }
41
42     // Do the delete
43     let community_id = data.community_id;
44     let deleted = data.deleted;
45     let updated_community = blocking(context.pool(), move |conn| {
46       Community::update_deleted(conn, community_id, deleted)
47     })
48     .await?
49     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
50
51     let res = send_community_ws_message(
52       data.community_id,
53       UserOperationCrud::DeleteCommunity,
54       websocket_id,
55       Some(local_user_view.person.id),
56       context,
57     )
58     .await?;
59
60     // Send apub messages
61     let deletable = DeletableObjects::Community(Box::new(updated_community.clone().into()));
62     send_apub_delete_in_community(
63       local_user_view.person,
64       updated_community,
65       deletable,
66       None,
67       deleted,
68       context,
69     )
70     .await?;
71
72     Ok(res)
73   }
74 }
75
76 #[async_trait::async_trait(?Send)]
77 impl PerformCrud for RemoveCommunity {
78   type Response = CommunityResponse;
79
80   #[tracing::instrument(skip(context, websocket_id))]
81   async fn perform(
82     &self,
83     context: &Data<LemmyContext>,
84     websocket_id: Option<ConnectionId>,
85   ) -> Result<CommunityResponse, LemmyError> {
86     let data: &RemoveCommunity = self;
87     let local_user_view =
88       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
89
90     // Verify its an admin (only an admin can remove a community)
91     is_admin(&local_user_view)?;
92
93     // Do the remove
94     let community_id = data.community_id;
95     let removed = data.removed;
96     let updated_community = blocking(context.pool(), move |conn| {
97       Community::update_removed(conn, community_id, removed)
98     })
99     .await?
100     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
101
102     // Mod tables
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(),
109       expires,
110     };
111     blocking(context.pool(), move |conn| {
112       ModRemoveCommunity::create(conn, &form)
113     })
114     .await??;
115
116     let res = send_community_ws_message(
117       data.community_id,
118       UserOperationCrud::RemoveCommunity,
119       websocket_id,
120       Some(local_user_view.person.id),
121       context,
122     )
123     .await?;
124
125     // Apub messages
126     let deletable = DeletableObjects::Community(Box::new(updated_community.clone().into()));
127     send_apub_delete_in_community(
128       local_user_view.person,
129       updated_community,
130       deletable,
131       data.reason.clone().or_else(|| Some("".to_string())),
132       removed,
133       context,
134     )
135     .await?;
136     Ok(res)
137   }
138 }