3 block::{generate_cc, SiteOrCommunity},
4 community::send_activity_in_community,
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
13 objects::{instance::remote_instance_inboxes, person::ApubPerson},
14 protocol::activities::block::block_user::BlockUser,
17 use activitypub_federation::{
18 core::object_id::ObjectId,
20 traits::{ActivityHandler, Actor},
21 utils::verify_domains_match,
23 use activitystreams_kinds::{activity::BlockType, public};
25 use chrono::NaiveDateTime;
26 use lemmy_api_common::utils::{remove_user_data, remove_user_data_in_community};
27 use lemmy_db_schema::{
31 CommunityFollowerForm,
33 CommunityPersonBanForm,
35 moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
36 person::{Person, PersonUpdateForm},
38 traits::{Bannable, Crud, Followable},
40 use lemmy_utils::{error::LemmyError, utils::convert_datetime};
41 use lemmy_websocket::LemmyContext;
45 pub(in crate::activities::block) async fn new(
46 target: &SiteOrCommunity,
49 remove_data: Option<bool>,
50 reason: Option<String>,
51 expires: Option<NaiveDateTime>,
52 context: &LemmyContext,
53 ) -> Result<BlockUser, LemmyError> {
54 let audience = if let SiteOrCommunity::Community(c) = target {
55 Some(ObjectId::new(c.actor_id()))
60 actor: ObjectId::new(mod_.actor_id()),
62 object: ObjectId::new(user.actor_id()),
63 cc: generate_cc(target, context.pool()).await?,
65 kind: BlockType::Block,
68 id: generate_activity_id(
70 &context.settings().get_protocol_and_hostname(),
73 expires: expires.map(convert_datetime),
77 #[tracing::instrument(skip_all)]
79 target: &SiteOrCommunity,
83 reason: Option<String>,
84 expires: Option<NaiveDateTime>,
85 context: &LemmyContext,
86 ) -> Result<(), LemmyError> {
87 let block = BlockUser::new(
99 SiteOrCommunity::Site(_) => {
100 let inboxes = remote_instance_inboxes(context.pool()).await?;
101 send_lemmy_activity(context, block, mod_, inboxes, false).await
103 SiteOrCommunity::Community(c) => {
104 let activity = AnnouncableActivities::BlockUser(block);
105 let inboxes = vec![user.shared_inbox_or_inbox()];
106 send_activity_in_community(activity, mod_, c, inboxes, true, context).await
112 #[async_trait::async_trait(?Send)]
113 impl ActivityHandler for BlockUser {
114 type DataType = LemmyContext;
115 type Error = LemmyError;
117 fn id(&self) -> &Url {
121 fn actor(&self) -> &Url {
125 #[tracing::instrument(skip_all)]
128 context: &Data<LemmyContext>,
129 request_counter: &mut i32,
130 ) -> Result<(), LemmyError> {
131 verify_is_public(&self.to, &self.cc)?;
134 .dereference(context, local_instance(context).await, request_counter)
137 SiteOrCommunity::Site(site) => {
138 let domain = self.object.inner().domain().expect("url needs domain");
139 if context.settings().hostname == domain {
141 anyhow!("Site bans from remote instance can't affect user's home instance").into(),
144 // site ban can only target a user who is on the same instance as the actor (admin)
145 verify_domains_match(&site.actor_id(), self.actor.inner())?;
146 verify_domains_match(&site.actor_id(), self.object.inner())?;
148 SiteOrCommunity::Community(community) => {
149 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
163 #[tracing::instrument(skip_all)]
166 context: &Data<LemmyContext>,
167 request_counter: &mut i32,
168 ) -> Result<(), LemmyError> {
169 let expires = self.expires.map(|u| u.naive_local());
170 let mod_person = self
172 .dereference(context, local_instance(context).await, request_counter)
174 let blocked_person = self
176 .dereference(context, local_instance(context).await, request_counter)
180 .dereference(context, local_instance(context).await, request_counter)
183 SiteOrCommunity::Site(_site) => {
184 let blocked_person = Person::update(
187 &PersonUpdateForm::builder()
189 .ban_expires(Some(expires))
193 if self.remove_data.unwrap_or(false) {
204 let form = ModBanForm {
205 mod_person_id: mod_person.id,
206 other_person_id: blocked_person.id,
207 reason: self.summary,
211 ModBan::create(context.pool(), &form).await?;
213 SiteOrCommunity::Community(community) => {
214 let community_user_ban_form = CommunityPersonBanForm {
215 community_id: community.id,
216 person_id: blocked_person.id,
217 expires: Some(expires),
219 CommunityPersonBan::ban(context.pool(), &community_user_ban_form).await?;
221 // Also unsubscribe them from the community, if they are subscribed
222 let community_follower_form = CommunityFollowerForm {
223 community_id: community.id,
224 person_id: blocked_person.id,
227 CommunityFollower::unfollow(context.pool(), &community_follower_form)
231 if self.remove_data.unwrap_or(false) {
232 remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
236 let form = ModBanFromCommunityForm {
237 mod_person_id: mod_person.id,
238 other_person_id: blocked_person.id,
239 community_id: community.id,
240 reason: self.summary,
244 ModBanFromCommunity::create(context.pool(), &form).await?;