2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::{
|
2023-06-06 16:27:22 +00:00
|
|
|
build_response::build_community_response,
|
2022-05-03 17:44:13 +00:00
|
|
|
community::{CommunityResponse, DeleteCommunity},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{is_top_mod, local_user_view_from_jwt},
|
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;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn delete_community(
|
|
|
|
data: Json<DeleteCommunity>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<CommunityResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Fetch the community mods
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let community_mods =
|
|
|
|
CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Make sure deleter is the top mod
|
|
|
|
is_top_mod(&local_user_view, &community_mods)?;
|
2021-04-08 11:29:08 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Do the delete
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let deleted = data.deleted;
|
|
|
|
let community = Community::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
community_id,
|
|
|
|
&CommunityUpdateForm::builder()
|
|
|
|
.deleted(Some(deleted))
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateCommunity)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::DeleteCommunity(local_user_view.person.clone(), community, data.deleted),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
build_community_response(&context, local_user_view, community_id).await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|