X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapi%2Fsrc%2Fcommunity%2Fban.rs;h=d04a8a0a9854b6575a16db90c4e5232638e91de1;hb=27be1efb74d6761046999980561a343d06235674;hp=95c2bbc04175a1b043ae12bf34e7e10cd351e479;hpb=be1389420b9dc4516f1e9b3e82637f84919a438e;p=lemmy.git diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index 95c2bbc0..d04a8a0a 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -1,8 +1,9 @@ -use crate::Perform; -use actix_web::web::Data; +use activitypub_federation::config::Data; +use actix_web::web::Json; use lemmy_api_common::{ community::{BanFromCommunity, BanFromCommunityResponse}, context::LemmyContext, + send_activity::{ActivityChannel, SendActivityData}, utils::{ is_mod_or_admin, local_user_view_from_jwt, @@ -28,77 +29,85 @@ use lemmy_utils::{ utils::{time::naive_from_unix, validation::is_valid_body_field}, }; -#[async_trait::async_trait(?Send)] -impl Perform for BanFromCommunity { - type Response = BanFromCommunityResponse; +#[tracing::instrument(skip(context))] +pub async fn ban_from_community( + data: Json, + context: Data, +) -> Result, LemmyError> { + let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?; - #[tracing::instrument(skip(context))] - async fn perform( - &self, - context: &Data, - ) -> Result { - let data: &BanFromCommunity = self; - let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; + let banned_person_id = data.person_id; + let remove_data = data.remove_data.unwrap_or(false); + let expires = data.expires.map(naive_from_unix); - 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( + &mut context.pool(), + local_user_view.person.id, + data.community_id, + ) + .await?; + is_valid_body_field(&data.reason, false)?; - // Verify that only mods or admins can ban - is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?; - is_valid_body_field(&data.reason, false)?; + let community_user_ban_form = CommunityPersonBanForm { + community_id: data.community_id, + person_id: data.person_id, + expires: Some(expires), + }; - let community_user_ban_form = CommunityPersonBanForm { + if data.ban { + CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form) + .await + .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?; + + // Also unsubscribe them from the community, if they are subscribed + let community_follower_form = CommunityFollowerForm { community_id: data.community_id, - person_id: data.person_id, - expires: Some(expires), + person_id: banned_person_id, + pending: false, }; - if data.ban { - CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form) - .await - .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?; + CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) + .await + .ok(); + } else { + CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form) + .await + .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?; + } - // 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, - }; + // Remove/Restore their data if that's desired + if remove_data { + remove_user_data_in_community(data.community_id, banned_person_id, &mut context.pool()).await?; + } - CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) - .await - .ok(); - } else { - CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form) - .await - .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?; - } + // Mod tables + let form = ModBanFromCommunityForm { + mod_person_id: local_user_view.person.id, + other_person_id: data.person_id, + community_id: data.community_id, + reason: sanitize_html_opt(&data.reason), + banned: Some(data.ban), + expires, + }; - // Remove/Restore their data if that's desired - if remove_data { - remove_user_data_in_community(community_id, banned_person_id, &mut context.pool()).await?; - } + ModBanFromCommunity::create(&mut context.pool(), &form).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, - reason: sanitize_html_opt(&data.reason), - banned: Some(data.ban), - expires, - }; - - ModBanFromCommunity::create(&mut context.pool(), &form).await?; + let person_view = PersonView::read(&mut context.pool(), data.person_id).await?; - let person_id = data.person_id; - let person_view = PersonView::read(&mut context.pool(), person_id).await?; + ActivityChannel::submit_activity( + SendActivityData::BanFromCommunity( + local_user_view.person, + data.community_id, + person_view.person.clone(), + data.0.clone(), + ), + &context, + ) + .await?; - Ok(BanFromCommunityResponse { - person_view, - banned: data.ban, - }) - } + Ok(Json(BanFromCommunityResponse { + person_view, + banned: data.ban, + })) }