3 block::{generate_cc, SiteOrCommunity},
4 community::send_activity_in_community,
9 activity_lists::AnnouncableActivities,
11 objects::{instance::remote_instance_inboxes, person::ApubPerson},
12 protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
15 use activitypub_federation::{
16 core::object_id::ObjectId,
18 traits::{ActivityHandler, Actor},
19 utils::verify_domains_match,
21 use activitystreams_kinds::{activity::UndoType, public};
22 use lemmy_api_common::LemmyContext;
23 use lemmy_db_schema::{
25 community::{CommunityPersonBan, CommunityPersonBanForm},
26 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
27 person::{Person, PersonUpdateForm},
29 traits::{Bannable, Crud},
31 use lemmy_utils::error::LemmyError;
35 #[tracing::instrument(skip_all)]
37 target: &SiteOrCommunity,
40 reason: Option<String>,
41 context: &LemmyContext,
42 ) -> Result<(), LemmyError> {
43 let block = BlockUser::new(target, user, mod_, None, reason, None, context).await?;
44 let audience = if let SiteOrCommunity::Community(c) = target {
45 Some(ObjectId::new(c.actor_id()))
50 let id = generate_activity_id(
52 &context.settings().get_protocol_and_hostname(),
54 let undo = UndoBlockUser {
55 actor: ObjectId::new(mod_.actor_id()),
58 cc: generate_cc(target, context.pool()).await?,
64 let mut inboxes = vec![user.shared_inbox_or_inbox()];
66 SiteOrCommunity::Site(_) => {
67 inboxes.append(&mut remote_instance_inboxes(context.pool()).await?);
68 send_lemmy_activity(context, undo, mod_, inboxes, false).await
70 SiteOrCommunity::Community(c) => {
71 let activity = AnnouncableActivities::UndoBlockUser(undo);
72 send_activity_in_community(activity, mod_, c, inboxes, true, context).await
78 #[async_trait::async_trait(?Send)]
79 impl ActivityHandler for UndoBlockUser {
80 type DataType = LemmyContext;
81 type Error = LemmyError;
83 fn id(&self) -> &Url {
87 fn actor(&self) -> &Url {
91 #[tracing::instrument(skip_all)]
94 context: &Data<LemmyContext>,
95 request_counter: &mut i32,
96 ) -> Result<(), LemmyError> {
97 verify_is_public(&self.to, &self.cc)?;
98 verify_domains_match(self.actor.inner(), self.object.actor.inner())?;
99 self.object.verify(context, request_counter).await?;
103 #[tracing::instrument(skip_all)]
106 context: &Data<LemmyContext>,
107 request_counter: &mut i32,
108 ) -> Result<(), LemmyError> {
109 let instance = local_instance(context).await;
110 let expires = self.object.expires.map(|u| u.naive_local());
111 let mod_person = self
113 .dereference(context, instance, request_counter)
115 let blocked_person = self
118 .dereference(context, instance, request_counter)
123 .dereference(context, instance, request_counter)
126 SiteOrCommunity::Site(_site) => {
127 let blocked_person = Person::update(
130 &PersonUpdateForm::builder()
132 .ban_expires(Some(expires))
138 let form = ModBanForm {
139 mod_person_id: mod_person.id,
140 other_person_id: blocked_person.id,
141 reason: self.object.summary,
145 ModBan::create(context.pool(), &form).await?;
147 SiteOrCommunity::Community(community) => {
148 let community_user_ban_form = CommunityPersonBanForm {
149 community_id: community.id,
150 person_id: blocked_person.id,
153 CommunityPersonBan::unban(context.pool(), &community_user_ban_form).await?;
156 let form = ModBanFromCommunityForm {
157 mod_person_id: mod_person.id,
158 other_person_id: blocked_person.id,
159 community_id: community.id,
160 reason: self.object.summary,
164 ModBanFromCommunity::create(context.pool(), &form).await?;