]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Don't drop error context when adding a message to errors (#1958)
[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, 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(LemmyError::from)
50     .map_err(|e| e.with_message("couldnt_update_community"))?;
51
52     // Send apub messages
53     send_apub_delete(
54       &local_user_view.person.clone().into(),
55       &updated_community.clone().into(),
56       DeletableObjects::Community(Box::new(updated_community.into())),
57       deleted,
58       context,
59     )
60     .await?;
61
62     send_community_ws_message(
63       data.community_id,
64       UserOperationCrud::DeleteCommunity,
65       websocket_id,
66       Some(local_user_view.person.id),
67       context,
68     )
69     .await
70   }
71 }
72
73 #[async_trait::async_trait(?Send)]
74 impl PerformCrud for RemoveCommunity {
75   type Response = CommunityResponse;
76
77   #[tracing::instrument(skip(context, websocket_id))]
78   async fn perform(
79     &self,
80     context: &Data<LemmyContext>,
81     websocket_id: Option<ConnectionId>,
82   ) -> Result<CommunityResponse, LemmyError> {
83     let data: &RemoveCommunity = self;
84     let local_user_view =
85       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
86
87     // Verify its an admin (only an admin can remove a community)
88     is_admin(&local_user_view)?;
89
90     // Do the remove
91     let community_id = data.community_id;
92     let removed = data.removed;
93     let updated_community = blocking(context.pool(), move |conn| {
94       Community::update_removed(conn, community_id, removed)
95     })
96     .await?
97     .map_err(LemmyError::from)
98     .map_err(|e| e.with_message("couldnt_update_community"))?;
99
100     // Mod tables
101     let expires = data.expires.map(naive_from_unix);
102     let form = ModRemoveCommunityForm {
103       mod_person_id: local_user_view.person.id,
104       community_id: data.community_id,
105       removed: Some(removed),
106       reason: data.reason.to_owned(),
107       expires,
108     };
109     blocking(context.pool(), move |conn| {
110       ModRemoveCommunity::create(conn, &form)
111     })
112     .await??;
113
114     // Apub messages
115     send_apub_remove(
116       &local_user_view.person.clone().into(),
117       &updated_community.clone().into(),
118       DeletableObjects::Community(Box::new(updated_community.into())),
119       data.reason.clone().unwrap_or_else(|| "".to_string()),
120       removed,
121       context,
122     )
123     .await?;
124
125     send_community_ws_message(
126       data.community_id,
127       UserOperationCrud::RemoveCommunity,
128       websocket_id,
129       Some(local_user_view.person.id),
130       context,
131     )
132     .await
133   }
134 }