2022-02-07 19:23:12 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{
|
2022-06-08 15:45:39 +00:00
|
|
|
block::{generate_cc, SiteOrCommunity},
|
2022-12-01 20:52:49 +00:00
|
|
|
community::send_activity_in_community,
|
2022-02-07 19:23:12 +00:00
|
|
|
generate_activity_id,
|
|
|
|
send_lemmy_activity,
|
|
|
|
verify_is_public,
|
|
|
|
verify_mod_action,
|
|
|
|
verify_person_in_community,
|
|
|
|
},
|
|
|
|
activity_lists::AnnouncableActivities,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2022-12-01 20:52:49 +00:00
|
|
|
objects::{instance::remote_instance_inboxes, person::ApubPerson},
|
2022-02-07 19:23:12 +00:00
|
|
|
protocol::activities::block::block_user::BlockUser,
|
2022-06-02 14:33:41 +00:00
|
|
|
ActorType,
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
|
|
|
core::object_id::ObjectId,
|
|
|
|
data::Data,
|
2022-06-08 15:45:39 +00:00
|
|
|
traits::{ActivityHandler, Actor},
|
2022-06-02 14:33:41 +00:00
|
|
|
utils::verify_domains_match,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
use activitystreams_kinds::{activity::BlockType, public};
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use chrono::NaiveDateTime;
|
2022-11-26 02:04:46 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-11-26 02:04:46 +00:00
|
|
|
utils::{remove_user_data, remove_user_data_in_community},
|
|
|
|
};
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::{
|
|
|
|
CommunityFollower,
|
|
|
|
CommunityFollowerForm,
|
|
|
|
CommunityPersonBan,
|
|
|
|
CommunityPersonBanForm,
|
|
|
|
},
|
2022-04-07 20:44:28 +00:00
|
|
|
moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
|
2022-10-27 09:24:07 +00:00
|
|
|
person::{Person, PersonUpdateForm},
|
2022-02-07 19:23:12 +00:00
|
|
|
},
|
|
|
|
traits::{Bannable, Crud, Followable},
|
|
|
|
};
|
2022-06-22 20:24:54 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, utils::convert_datetime};
|
2022-06-02 14:33:41 +00:00
|
|
|
use url::Url;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
impl BlockUser {
|
|
|
|
pub(in crate::activities::block) async fn new(
|
|
|
|
target: &SiteOrCommunity,
|
|
|
|
user: &ApubPerson,
|
|
|
|
mod_: &ApubPerson,
|
|
|
|
remove_data: Option<bool>,
|
|
|
|
reason: Option<String>,
|
|
|
|
expires: Option<NaiveDateTime>,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<BlockUser, LemmyError> {
|
2022-12-01 20:52:49 +00:00
|
|
|
let audience = if let SiteOrCommunity::Community(c) = target {
|
|
|
|
Some(ObjectId::new(c.actor_id()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(BlockUser {
|
|
|
|
actor: ObjectId::new(mod_.actor_id()),
|
|
|
|
to: vec![public()],
|
|
|
|
object: ObjectId::new(user.actor_id()),
|
|
|
|
cc: generate_cc(target, context.pool()).await?,
|
|
|
|
target: target.id(),
|
|
|
|
kind: BlockType::Block,
|
|
|
|
remove_data,
|
|
|
|
summary: reason,
|
|
|
|
id: generate_activity_id(
|
|
|
|
BlockType::Block,
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?,
|
2022-12-01 20:52:49 +00:00
|
|
|
audience,
|
2022-02-07 19:23:12 +00:00
|
|
|
expires: expires.map(convert_datetime),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
pub async fn send(
|
|
|
|
target: &SiteOrCommunity,
|
|
|
|
user: &ApubPerson,
|
|
|
|
mod_: &ApubPerson,
|
|
|
|
remove_data: bool,
|
|
|
|
reason: Option<String>,
|
|
|
|
expires: Option<NaiveDateTime>,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let block = BlockUser::new(
|
|
|
|
target,
|
|
|
|
user,
|
|
|
|
mod_,
|
|
|
|
Some(remove_data),
|
|
|
|
reason,
|
|
|
|
expires,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
match target {
|
|
|
|
SiteOrCommunity::Site(_) => {
|
2022-06-08 15:45:39 +00:00
|
|
|
let inboxes = remote_instance_inboxes(context.pool()).await?;
|
|
|
|
send_lemmy_activity(context, block, mod_, inboxes, false).await
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
SiteOrCommunity::Community(c) => {
|
|
|
|
let activity = AnnouncableActivities::BlockUser(block);
|
2022-06-08 15:45:39 +00:00
|
|
|
let inboxes = vec![user.shared_inbox_or_inbox()];
|
2022-11-23 23:40:47 +00:00
|
|
|
send_activity_in_community(activity, mod_, c, inboxes, true, context).await
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ActivityHandler for BlockUser {
|
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn verify(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
verify_is_public(&self.to, &self.cc)?;
|
|
|
|
match self
|
|
|
|
.target
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await?
|
|
|
|
{
|
|
|
|
SiteOrCommunity::Site(site) => {
|
|
|
|
let domain = self.object.inner().domain().expect("url needs domain");
|
2022-06-22 20:24:54 +00:00
|
|
|
if context.settings().hostname == domain {
|
2022-02-07 19:23:12 +00:00
|
|
|
return Err(
|
|
|
|
anyhow!("Site bans from remote instance can't affect user's home instance").into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// site ban can only target a user who is on the same instance as the actor (admin)
|
|
|
|
verify_domains_match(&site.actor_id(), self.actor.inner())?;
|
|
|
|
verify_domains_match(&site.actor_id(), self.object.inner())?;
|
|
|
|
}
|
|
|
|
SiteOrCommunity::Community(community) => {
|
|
|
|
verify_person_in_community(&self.actor, &community, context, request_counter).await?;
|
2022-04-04 14:46:49 +00:00
|
|
|
verify_mod_action(
|
|
|
|
&self.actor,
|
|
|
|
self.object.inner(),
|
2022-10-10 15:20:36 +00:00
|
|
|
community.id,
|
2022-04-04 14:46:49 +00:00
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn receive(
|
|
|
|
self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let expires = self.expires.map(|u| u.naive_local());
|
|
|
|
let mod_person = self
|
|
|
|
.actor
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await?;
|
|
|
|
let blocked_person = self
|
|
|
|
.object
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await?;
|
|
|
|
let target = self
|
|
|
|
.target
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await?;
|
|
|
|
match target {
|
|
|
|
SiteOrCommunity::Site(_site) => {
|
2022-11-09 10:05:00 +00:00
|
|
|
let blocked_person = Person::update(
|
|
|
|
context.pool(),
|
|
|
|
blocked_person.id,
|
|
|
|
&PersonUpdateForm::builder()
|
|
|
|
.banned(Some(true))
|
|
|
|
.ban_expires(Some(expires))
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
if self.remove_data.unwrap_or(false) {
|
2022-06-13 19:15:04 +00:00
|
|
|
remove_user_data(
|
|
|
|
blocked_person.id,
|
|
|
|
context.pool(),
|
2022-06-22 20:24:54 +00:00
|
|
|
context.settings(),
|
2022-06-13 19:15:04 +00:00
|
|
|
context.client(),
|
|
|
|
)
|
|
|
|
.await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write mod log
|
|
|
|
let form = ModBanForm {
|
|
|
|
mod_person_id: mod_person.id,
|
|
|
|
other_person_id: blocked_person.id,
|
|
|
|
reason: self.summary,
|
|
|
|
banned: Some(true),
|
|
|
|
expires,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
ModBan::create(context.pool(), &form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
SiteOrCommunity::Community(community) => {
|
|
|
|
let community_user_ban_form = CommunityPersonBanForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: blocked_person.id,
|
|
|
|
expires: Some(expires),
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityPersonBan::ban(context.pool(), &community_user_ban_form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
// Also unsubscribe them from the community, if they are subscribed
|
|
|
|
let community_follower_form = CommunityFollowerForm {
|
|
|
|
community_id: community.id,
|
|
|
|
person_id: blocked_person.id,
|
|
|
|
pending: false,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
CommunityFollower::unfollow(context.pool(), &community_follower_form)
|
|
|
|
.await
|
|
|
|
.ok();
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
if self.remove_data.unwrap_or(false) {
|
|
|
|
remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write to mod log
|
2022-04-07 20:44:28 +00:00
|
|
|
let form = ModBanFromCommunityForm {
|
2022-02-07 19:23:12 +00:00
|
|
|
mod_person_id: mod_person.id,
|
|
|
|
other_person_id: blocked_person.id,
|
2022-04-07 20:44:28 +00:00
|
|
|
community_id: community.id,
|
2022-02-07 19:23:12 +00:00
|
|
|
reason: self.summary,
|
|
|
|
banned: Some(true),
|
|
|
|
expires,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
ModBanFromCommunity::create(context.pool(), &form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|