]> Untitled Git - lemmy.git/blob - crates/api_crud/src/community/delete.rs
Adding a vector indexing check to prevent panics. Fixes #2753 (#2754)
[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::{get_local_user_view_from_jwt, is_top_mod},
7   websocket::{send::send_community_ws_message, 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 =
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 =
33       CommunityModeratorView::for_community(context.pool(), community_id).await?;
34
35     // Make sure deleter is the top mod
36     is_top_mod(&local_user_view, &community_mods)?;
37
38     // Do the delete
39     let community_id = data.community_id;
40     let deleted = data.deleted;
41     Community::update(
42       context.pool(),
43       community_id,
44       &CommunityUpdateForm::builder()
45         .deleted(Some(deleted))
46         .build(),
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     Ok(res)
61   }
62 }