2021-08-17 18:04:58 +00:00
|
|
|
use crate::PerformCrud;
|
2021-03-25 19:19:40 +00:00
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin};
|
2022-02-14 15:14:24 +00:00
|
|
|
use lemmy_apub::activities::deletion::{send_apub_delete_in_community, DeletableObjects};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::Community,
|
|
|
|
moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2021-08-17 18:04:58 +00:00
|
|
|
use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
|
2021-12-06 14:54:47 +00:00
|
|
|
use lemmy_utils::{utils::naive_from_unix, ConnectionId, LemmyError};
|
2021-08-17 18:04:58 +00:00
|
|
|
use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
|
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;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).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;
|
2021-04-08 11:29:08 +00:00
|
|
|
let community_mods = blocking(context.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2021-04-08 11:29:08 +00:00
|
|
|
|
|
|
|
// Make sure deleter is the top mod
|
|
|
|
if local_user_view.person.id != community_mods[0].moderator.id {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("no_community_edit_allowed"));
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do the delete
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let deleted = data.deleted;
|
2021-04-16 13:10:43 +00:00
|
|
|
let updated_community = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
Community::update_deleted(conn, community_id, deleted)
|
|
|
|
})
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_community"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2022-02-14 15:14:24 +00:00
|
|
|
let res = send_community_ws_message(
|
2021-08-17 18:04:58 +00:00
|
|
|
data.community_id,
|
|
|
|
UserOperationCrud::DeleteCommunity,
|
|
|
|
websocket_id,
|
|
|
|
Some(local_user_view.person.id),
|
|
|
|
context,
|
|
|
|
)
|
2022-02-14 15:14:24 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Send apub messages
|
|
|
|
let deletable = DeletableObjects::Community(Box::new(updated_community.clone().into()));
|
|
|
|
send_apub_delete_in_community(
|
|
|
|
local_user_view.person,
|
|
|
|
updated_community,
|
|
|
|
deletable,
|
|
|
|
None,
|
|
|
|
deleted,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(res)
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for RemoveCommunity {
|
|
|
|
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: &RemoveCommunity = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Verify its an admin (only an admin can remove a community)
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
|
|
|
// Do the remove
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let removed = data.removed;
|
2021-04-16 13:10:43 +00:00
|
|
|
let updated_community = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
Community::update_removed(conn, community_id, removed)
|
|
|
|
})
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_community"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Mod tables
|
2021-03-25 19:30:15 +00:00
|
|
|
let expires = data.expires.map(naive_from_unix);
|
2021-03-25 19:19:40 +00:00
|
|
|
let form = ModRemoveCommunityForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
community_id: data.community_id,
|
|
|
|
removed: Some(removed),
|
|
|
|
reason: data.reason.to_owned(),
|
|
|
|
expires,
|
|
|
|
};
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
ModRemoveCommunity::create(conn, &form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2022-02-14 15:14:24 +00:00
|
|
|
let res = send_community_ws_message(
|
2021-08-17 18:04:58 +00:00
|
|
|
data.community_id,
|
|
|
|
UserOperationCrud::RemoveCommunity,
|
|
|
|
websocket_id,
|
|
|
|
Some(local_user_view.person.id),
|
|
|
|
context,
|
|
|
|
)
|
2022-02-14 15:14:24 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Apub messages
|
|
|
|
let deletable = DeletableObjects::Community(Box::new(updated_community.clone().into()));
|
|
|
|
send_apub_delete_in_community(
|
|
|
|
local_user_view.person,
|
|
|
|
updated_community,
|
|
|
|
deletable,
|
|
|
|
data.reason.clone().or_else(|| Some("".to_string())),
|
|
|
|
removed,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(res)
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|