]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Dont return error in case optional auth is invalid (#2879)
[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   context::LemmyContext,
6   utils::{is_top_mod, local_user_view_from_jwt},
7   websocket::UserOperationCrud,
8 };
9 use lemmy_db_schema::{
10   source::community::{Community, CommunityUpdateForm},
11   traits::Crud,
12 };
13 use lemmy_db_views_actor::structs::CommunityModeratorView;
14 use lemmy_utils::{error::LemmyError, ConnectionId};
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 = local_user_view_from_jwt(&data.auth, context).await?;
28
29     // Fetch the community mods
30     let community_id = data.community_id;
31     let community_mods =
32       CommunityModeratorView::for_community(context.pool(), community_id).await?;
33
34     // Make sure deleter is the top mod
35     is_top_mod(&local_user_view, &community_mods)?;
36
37     // Do the delete
38     let community_id = data.community_id;
39     let deleted = data.deleted;
40     Community::update(
41       context.pool(),
42       community_id,
43       &CommunityUpdateForm::builder()
44         .deleted(Some(deleted))
45         .build(),
46     )
47     .await
48     .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
49
50     let res = context
51       .send_community_ws_message(
52         &UserOperationCrud::DeleteCommunity,
53         data.community_id,
54         websocket_id,
55         Some(local_user_view.person.id),
56       )
57       .await?;
58
59     Ok(res)
60   }
61 }