Move local check into function.

content_removal_remote_users
Dessalines 2024-02-29 09:29:14 -05:00
parent 052860e62a
commit 469eaa2d5e
2 changed files with 71 additions and 70 deletions

View File

@ -174,74 +174,77 @@ pub(crate) async fn ban_nonlocal_user_from_local_communities(
expires: &Option<i64>, expires: &Option<i64>,
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
) -> LemmyResult<()> { ) -> LemmyResult<()> {
let ids = Person::list_local_community_ids(&mut context.pool(), target.id).await?; // Only run this code for federated users
if !target.local {
let ids = Person::list_local_community_ids(&mut context.pool(), target.id).await?;
for community_id in ids { for community_id in ids {
let expires_dt = check_expire_time(*expires)?; let expires_dt = check_expire_time(*expires)?;
// Ban / unban them from our local communities // Ban / unban them from our local communities
let community_user_ban_form = CommunityPersonBanForm { let community_user_ban_form = CommunityPersonBanForm {
community_id,
person_id: target.id,
expires: Some(expires_dt),
};
if ban {
// Ignore all errors for these
CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form)
.await
.with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)
.ok();
// Also unsubscribe them from the community, if they are subscribed
let community_follower_form = CommunityFollowerForm {
community_id, community_id,
person_id: target.id, person_id: target.id,
pending: false, expires: Some(expires_dt),
}; };
CommunityFollower::unfollow(&mut context.pool(), &community_follower_form) if ban {
.await // Ignore all errors for these
.ok(); CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form)
} else { .await
CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form) .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)
.await .ok();
.with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)
.ok();
}
// Mod tables // Also unsubscribe them from the community, if they are subscribed
let form = ModBanFromCommunityForm { let community_follower_form = CommunityFollowerForm {
mod_person_id: local_user_view.person.id, community_id,
other_person_id: target.id, person_id: target.id,
community_id, pending: false,
reason: reason.clone(), };
banned: Some(ban),
expires: expires_dt,
};
ModBanFromCommunity::create(&mut context.pool(), &form).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)
.ok();
}
// Federate the ban from community // Mod tables
let ban_from_community = BanFromCommunity { let form = ModBanFromCommunityForm {
community_id, mod_person_id: local_user_view.person.id,
person_id: target.id, other_person_id: target.id,
ban,
reason: reason.clone(),
remove_data: *remove_data,
expires: *expires,
};
ActivityChannel::submit_activity(
SendActivityData::BanFromCommunity {
moderator: local_user_view.person.clone(),
community_id, community_id,
target: target.clone(), reason: reason.clone(),
data: ban_from_community, banned: Some(ban),
}, expires: expires_dt,
context, };
)
.await?; ModBanFromCommunity::create(&mut context.pool(), &form).await?;
// Federate the ban from community
let ban_from_community = BanFromCommunity {
community_id,
person_id: target.id,
ban,
reason: reason.clone(),
remove_data: *remove_data,
expires: *expires,
};
ActivityChannel::submit_activity(
SendActivityData::BanFromCommunity {
moderator: local_user_view.person.clone(),
community_id,
target: target.clone(),
data: ban_from_community,
},
context,
)
.await?;
}
} }
Ok(()) Ok(())

View File

@ -72,18 +72,16 @@ pub async fn ban_from_site(
let person_view = PersonView::read(&mut context.pool(), person.id).await?; let person_view = PersonView::read(&mut context.pool(), person.id).await?;
if !person.local { ban_nonlocal_user_from_local_communities(
ban_nonlocal_user_from_local_communities( &local_user_view,
&local_user_view, &person,
&person, data.ban,
data.ban, &data.reason,
&data.reason, &data.remove_data,
&data.remove_data, &data.expires,
&data.expires, &context,
&context, )
) .await?;
.await?;
}
ActivityChannel::submit_activity( ActivityChannel::submit_activity(
SendActivityData::BanFromSite { SendActivityData::BanFromSite {