2022-04-13 18:12:25 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
community::{BanFromCommunity, BanFromCommunityResponse},
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_local_user_view_from_jwt, is_mod_or_admin, remove_user_data_in_community},
|
2023-04-13 10:53:55 +00:00
|
|
|
websocket::{
|
|
|
|
handlers::messages::SendCommunityRoomMessage,
|
|
|
|
serialize_websocket_message,
|
|
|
|
UserOperation,
|
|
|
|
},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::{
|
|
|
|
CommunityFollower,
|
|
|
|
CommunityFollowerForm,
|
|
|
|
CommunityPersonBan,
|
|
|
|
CommunityPersonBanForm,
|
|
|
|
},
|
|
|
|
moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
|
|
|
|
},
|
|
|
|
traits::{Bannable, Crud, Followable},
|
|
|
|
};
|
2023-03-01 17:19:46 +00:00
|
|
|
use lemmy_db_views_actor::structs::PersonView;
|
2023-04-15 14:45:11 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
error::LemmyError,
|
|
|
|
utils::{time::naive_from_unix, validation::is_valid_body_field},
|
|
|
|
ConnectionId,
|
|
|
|
};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for BanFromCommunity {
|
|
|
|
type Response = BanFromCommunityResponse;
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<BanFromCommunityResponse, LemmyError> {
|
|
|
|
let data: &BanFromCommunity = self;
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
|
|
|
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let banned_person_id = data.person_id;
|
|
|
|
let remove_data = data.remove_data.unwrap_or(false);
|
|
|
|
let expires = data.expires.map(naive_from_unix);
|
|
|
|
|
|
|
|
// Verify that only mods or admins can ban
|
|
|
|
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
|
2023-04-15 14:45:11 +00:00
|
|
|
is_valid_body_field(&data.reason)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
let community_user_ban_form = CommunityPersonBanForm {
|
|
|
|
community_id: data.community_id,
|
|
|
|
person_id: data.person_id,
|
|
|
|
expires: Some(expires),
|
|
|
|
};
|
|
|
|
|
|
|
|
if data.ban {
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityPersonBan::ban(context.pool(), &community_user_ban_form)
|
|
|
|
.await
|
2022-04-13 18:12:25 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
|
|
|
|
|
|
|
|
// Also unsubscribe them from the community, if they are subscribed
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: data.community_id,
|
|
|
|
person_id: banned_person_id,
|
|
|
|
pending: false,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
|
|
|
|
CommunityFollower::unfollow(context.pool(), &community_follower_form)
|
|
|
|
.await
|
|
|
|
.ok();
|
2022-04-13 18:12:25 +00:00
|
|
|
} else {
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityPersonBan::unban(context.pool(), &community_user_ban_form)
|
|
|
|
.await
|
2022-04-13 18:12:25 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove/Restore their data if that's desired
|
|
|
|
if remove_data {
|
|
|
|
remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModBanFromCommunityForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
other_person_id: data.person_id,
|
|
|
|
community_id: data.community_id,
|
2022-11-19 04:33:54 +00:00
|
|
|
reason: data.reason.clone(),
|
2022-04-13 18:12:25 +00:00
|
|
|
banned: Some(data.ban),
|
|
|
|
expires,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
|
|
|
|
ModBanFromCommunity::create(context.pool(), &form).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
let person_id = data.person_id;
|
2023-03-01 17:19:46 +00:00
|
|
|
let person_view = PersonView::read(context.pool(), person_id).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
let res = BanFromCommunityResponse {
|
|
|
|
person_view,
|
|
|
|
banned: data.ban,
|
|
|
|
};
|
|
|
|
|
2023-04-13 10:53:55 +00:00
|
|
|
// A custom ban message
|
|
|
|
let message = serialize_websocket_message(&UserOperation::BanFromCommunity, &res)?;
|
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
|
|
|
community_id,
|
|
|
|
message,
|
|
|
|
websocket_id,
|
|
|
|
});
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|