2021-03-25 19:19:40 +00:00
|
|
|
use crate::{community::send_community_websocket, PerformCrud};
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin};
|
|
|
|
use lemmy_apub::CommunityType;
|
2021-07-30 18:44:15 +00:00
|
|
|
use lemmy_db_queries::{source::community::Community_, Crud, DeleteableOrRemoveable};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
community::*,
|
|
|
|
moderator::{ModRemoveCommunity, ModRemoveCommunityForm},
|
|
|
|
};
|
2021-04-08 11:29:08 +00:00
|
|
|
use lemmy_db_views_actor::{
|
|
|
|
community_moderator_view::CommunityModeratorView,
|
|
|
|
community_view::CommunityView,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_utils::{utils::naive_from_unix, ApiError, ConnectionId, LemmyError};
|
2021-03-25 19:30:15 +00:00
|
|
|
use lemmy_websocket::{LemmyContext, UserOperationCrud};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for DeleteCommunity {
|
|
|
|
type Response = CommunityResponse;
|
|
|
|
|
|
|
|
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-03-25 19:19:40 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
|
|
|
|
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-03-25 19:19:40 +00:00
|
|
|
return Err(ApiError::err("no_community_edit_allowed").into());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-16 13:10:43 +00:00
|
|
|
.map_err(|_| ApiError::err("couldnt_update_community"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Send apub messages
|
|
|
|
if deleted {
|
2021-04-13 11:48:30 +00:00
|
|
|
updated_community
|
2021-07-30 18:44:15 +00:00
|
|
|
.blank_out_deleted_or_removed_info()
|
2021-04-13 11:48:30 +00:00
|
|
|
.send_delete(local_user_view.person.to_owned(), context)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
} else {
|
2021-04-13 11:48:30 +00:00
|
|
|
updated_community
|
|
|
|
.send_undo_delete(local_user_view.person.to_owned(), context)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let person_id = local_user_view.person.id;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut community_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityView::read(conn, community_id, Some(person_id))
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
// Blank out deleted or removed info
|
|
|
|
if deleted {
|
|
|
|
community_view.community = community_view.community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let res = CommunityResponse { community_view };
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
send_community_websocket(
|
|
|
|
&res,
|
|
|
|
context,
|
|
|
|
websocket_id,
|
|
|
|
UserOperationCrud::DeleteCommunity,
|
|
|
|
);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for RemoveCommunity {
|
|
|
|
type Response = CommunityResponse;
|
|
|
|
|
|
|
|
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-03-25 19:19:40 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
|
|
|
|
|
|
|
// 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-04-16 13:10:43 +00:00
|
|
|
.map_err(|_| ApiError::err("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??;
|
|
|
|
|
|
|
|
// Apub messages
|
|
|
|
if removed {
|
2021-07-30 18:44:15 +00:00
|
|
|
updated_community
|
|
|
|
.blank_out_deleted_or_removed_info()
|
|
|
|
.send_remove(context)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
} else {
|
|
|
|
updated_community.send_undo_remove(context).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let person_id = local_user_view.person.id;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut community_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityView::read(conn, community_id, Some(person_id))
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-07-30 18:44:15 +00:00
|
|
|
// Blank out deleted or removed info
|
|
|
|
if removed {
|
|
|
|
community_view.community = community_view.community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let res = CommunityResponse { community_view };
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
send_community_websocket(
|
|
|
|
&res,
|
|
|
|
context,
|
|
|
|
websocket_id,
|
|
|
|
UserOperationCrud::RemoveCommunity,
|
|
|
|
);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|