mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-02-10 16:13:11 +00:00
* add private visibility * filter private communities in post_view.rs * also filter in comment_view * community follower state * remove unused method * sql fmt * add CommunityFollower.approved_by * implement api endpoints * api changes * only admins can create private community for now * add local api tests * fix api tests * follow remote private community * use authorized fetch for content in private community * federate community visibility * dont mark content in private community as public * expose ApprovalRequired in api * also check content fetchable for outbox/featured * address private community content to followers * implement reject activity * fix tests * add files * remove local api tests * dont use delay * is_new_instance * single query for is_new_instance * return subscribed type for pending follow * working * need to catch errors in waitUntil * clippy * fix query * lint for unused async * diesel.toml comment * add comment * avoid db reads * rename approved_by to approver_id * add helper * form init * list pending follows should return items for all communities * clippy * ci * fix down migration * fix api tests * references * rename * run git diff * ci * fix schema check * fix joins * ci * ci * skip_serializing_none * fix test --------- Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
129 lines
3.5 KiB
Rust
129 lines
3.5 KiB
Rust
use activitypub_federation::config::Data;
|
|
use actix_web::web::Json;
|
|
use lemmy_api_common::{
|
|
community::{BanFromCommunity, BanFromCommunityResponse},
|
|
context::LemmyContext,
|
|
send_activity::{ActivityChannel, SendActivityData},
|
|
utils::{
|
|
check_community_mod_action,
|
|
check_expire_time,
|
|
remove_or_restore_user_data_in_community,
|
|
},
|
|
};
|
|
use lemmy_db_schema::{
|
|
source::{
|
|
community::{
|
|
Community,
|
|
CommunityFollower,
|
|
CommunityFollowerForm,
|
|
CommunityPersonBan,
|
|
CommunityPersonBanForm,
|
|
},
|
|
local_user::LocalUser,
|
|
moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
|
|
},
|
|
traits::{Bannable, Crud, Followable},
|
|
};
|
|
use lemmy_db_views::structs::LocalUserView;
|
|
use lemmy_db_views_actor::structs::PersonView;
|
|
use lemmy_utils::{
|
|
error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
|
|
utils::validation::is_valid_body_field,
|
|
};
|
|
|
|
#[tracing::instrument(skip(context))]
|
|
pub async fn ban_from_community(
|
|
data: Json<BanFromCommunity>,
|
|
context: Data<LemmyContext>,
|
|
local_user_view: LocalUserView,
|
|
) -> LemmyResult<Json<BanFromCommunityResponse>> {
|
|
let banned_person_id = data.person_id;
|
|
let expires = check_expire_time(data.expires)?;
|
|
let community = Community::read(&mut context.pool(), data.community_id).await?;
|
|
|
|
// Verify that only mods or admins can ban
|
|
check_community_mod_action(
|
|
&local_user_view.person,
|
|
&community,
|
|
false,
|
|
&mut context.pool(),
|
|
)
|
|
.await?;
|
|
|
|
LocalUser::is_higher_mod_or_admin_check(
|
|
&mut context.pool(),
|
|
data.community_id,
|
|
local_user_view.person.id,
|
|
vec![data.person_id],
|
|
)
|
|
.await?;
|
|
|
|
if let Some(reason) = &data.reason {
|
|
is_valid_body_field(reason, false)?;
|
|
}
|
|
|
|
let community_user_ban_form = CommunityPersonBanForm {
|
|
community_id: data.community_id,
|
|
person_id: data.person_id,
|
|
expires: Some(expires),
|
|
};
|
|
|
|
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::new(data.community_id, banned_person_id);
|
|
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)?;
|
|
}
|
|
|
|
// Remove/Restore their data if that's desired
|
|
if data.remove_or_restore_data.unwrap_or(false) {
|
|
let remove_data = data.ban;
|
|
remove_or_restore_user_data_in_community(
|
|
data.community_id,
|
|
local_user_view.person.id,
|
|
banned_person_id,
|
|
remove_data,
|
|
&data.reason,
|
|
&mut 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,
|
|
reason: data.reason.clone(),
|
|
banned: Some(data.ban),
|
|
expires,
|
|
};
|
|
|
|
ModBanFromCommunity::create(&mut context.pool(), &form).await?;
|
|
|
|
let person_view = PersonView::read(&mut context.pool(), data.person_id).await?;
|
|
|
|
ActivityChannel::submit_activity(
|
|
SendActivityData::BanFromCommunity {
|
|
moderator: local_user_view.person,
|
|
community_id: data.community_id,
|
|
target: person_view.person.clone(),
|
|
data: data.0.clone(),
|
|
},
|
|
&context,
|
|
)?;
|
|
|
|
Ok(Json(BanFromCommunityResponse {
|
|
person_view,
|
|
banned: data.ban,
|
|
}))
|
|
}
|