2022-11-02 19:18:22 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{
|
|
|
|
block::{generate_cc, SiteOrCommunity},
|
2022-12-01 20:52:49 +00:00
|
|
|
community::send_activity_in_community,
|
2022-11-02 19:18:22 +00:00
|
|
|
generate_activity_id,
|
|
|
|
send_lemmy_activity,
|
|
|
|
verify_is_public,
|
|
|
|
},
|
|
|
|
activity_lists::AnnouncableActivities,
|
2023-07-14 15:17:06 +00:00
|
|
|
insert_received_activity,
|
2023-09-09 16:25:03 +00:00
|
|
|
objects::person::ApubPerson,
|
2022-11-02 19:18:22 +00:00
|
|
|
protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
|
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
kinds::{activity::UndoType, public},
|
|
|
|
protocol::verification::verify_domains_match,
|
2022-06-08 15:45:39 +00:00
|
|
|
traits::{ActivityHandler, Actor},
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
2023-10-11 14:48:19 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activity::ActivitySendTargets,
|
2022-02-07 19:23:12 +00:00
|
|
|
community::{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},
|
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
impl UndoBlockUser {
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
pub async fn send(
|
|
|
|
target: &SiteOrCommunity,
|
|
|
|
user: &ApubPerson,
|
|
|
|
mod_: &ApubPerson,
|
|
|
|
reason: Option<String>,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-02-07 19:23:12 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let block = BlockUser::new(target, user, mod_, None, reason, None, context).await?;
|
2022-12-01 20:52:49 +00:00
|
|
|
let audience = if let SiteOrCommunity::Community(c) = target {
|
2023-03-21 15:03:05 +00:00
|
|
|
Some(c.id().into())
|
2022-12-01 20:52:49 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
let id = generate_activity_id(
|
|
|
|
UndoType::Undo,
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
|
|
|
let undo = UndoBlockUser {
|
2023-03-21 15:03:05 +00:00
|
|
|
actor: mod_.id().into(),
|
2022-02-07 19:23:12 +00:00
|
|
|
to: vec![public()],
|
|
|
|
object: block,
|
2023-07-11 13:09:59 +00:00
|
|
|
cc: generate_cc(target, &mut context.pool()).await?,
|
2022-02-07 19:23:12 +00:00
|
|
|
kind: UndoType::Undo,
|
|
|
|
id: id.clone(),
|
2022-12-01 20:52:49 +00:00
|
|
|
audience,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
|
2023-09-09 16:25:03 +00:00
|
|
|
let mut inboxes = ActivitySendTargets::to_inbox(user.shared_inbox_or_inbox());
|
2022-02-07 19:23:12 +00:00
|
|
|
match target {
|
|
|
|
SiteOrCommunity::Site(_) => {
|
2023-09-09 16:25:03 +00:00
|
|
|
inboxes.set_all_instances();
|
2022-06-08 15:45:39 +00:00
|
|
|
send_lemmy_activity(context, undo, mod_, inboxes, false).await
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
SiteOrCommunity::Community(c) => {
|
|
|
|
let activity = AnnouncableActivities::UndoBlockUser(undo);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-02-07 19:23:12 +00:00
|
|
|
impl ActivityHandler for UndoBlockUser {
|
|
|
|
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)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
|
2023-07-14 15:17:06 +00:00
|
|
|
insert_received_activity(&self.id, context).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
verify_is_public(&self.to, &self.cc)?;
|
|
|
|
verify_domains_match(self.actor.inner(), self.object.actor.inner())?;
|
2023-03-21 15:03:05 +00:00
|
|
|
self.object.verify(context).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
|
2023-08-24 15:27:00 +00:00
|
|
|
let expires = self.object.expires.map(Into::into);
|
2023-03-21 15:03:05 +00:00
|
|
|
let mod_person = self.actor.dereference(context).await?;
|
|
|
|
let blocked_person = self.object.object.dereference(context).await?;
|
|
|
|
match self.object.target.dereference(context).await? {
|
2022-02-07 19:23:12 +00:00
|
|
|
SiteOrCommunity::Site(_site) => {
|
2022-11-09 10:05:00 +00:00
|
|
|
let blocked_person = Person::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
blocked_person.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PersonUpdateForm {
|
|
|
|
banned: Some(false),
|
|
|
|
ban_expires: Some(expires),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
|
|
|
.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,
|
2023-10-11 14:48:19 +00:00
|
|
|
reason: self.object.summary,
|
2022-02-07 19:23:12 +00:00
|
|
|
banned: Some(false),
|
|
|
|
expires,
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
ModBan::create(&mut 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: None,
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
// 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,
|
2023-10-11 14:48:19 +00:00
|
|
|
reason: self.object.summary,
|
2022-02-07 19:23:12 +00:00
|
|
|
banned: Some(false),
|
|
|
|
expires,
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
ModBanFromCommunity::create(&mut context.pool(), &form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|