2021-08-17 18:04:58 +00:00
|
|
|
use crate::PerformCrud;
|
2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
community::{CommunityResponse, DeleteCommunity},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{is_top_mod, local_user_view_from_jwt},
|
2023-04-13 10:53:55 +00:00
|
|
|
websocket::UserOperationCrud,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::community::{Community, CommunityUpdateForm},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views_actor::structs::CommunityModeratorView;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, ConnectionId};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for DeleteCommunity {
|
|
|
|
type Response = CommunityResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<CommunityResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &DeleteCommunity = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-04-08 11:29:08 +00:00
|
|
|
// Fetch the community mods
|
2021-03-25 19:19:40 +00:00
|
|
|
let community_id = data.community_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let community_mods =
|
|
|
|
CommunityModeratorView::for_community(context.pool(), community_id).await?;
|
2021-04-08 11:29:08 +00:00
|
|
|
|
|
|
|
// Make sure deleter is the top mod
|
2023-02-28 11:34:50 +00:00
|
|
|
is_top_mod(&local_user_view, &community_mods)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Do the delete
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let deleted = data.deleted;
|
2022-11-28 14:29:33 +00:00
|
|
|
Community::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
context.pool(),
|
|
|
|
community_id,
|
|
|
|
&CommunityUpdateForm::builder()
|
|
|
|
.deleted(Some(deleted))
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.await
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-04-13 10:53:55 +00:00
|
|
|
let res = context
|
|
|
|
.send_community_ws_message(
|
|
|
|
&UserOperationCrud::DeleteCommunity,
|
|
|
|
data.community_id,
|
|
|
|
websocket_id,
|
|
|
|
Some(local_user_view.person.id),
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-14 15:14:24 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|