]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/block/block_user.rs
540847072fcf942b1e65e5d587a009b018d4d088
[lemmy.git] / crates / apub / src / activities / block / block_user.rs
1 use crate::{
2   activities::{
3     block::{generate_cc, SiteOrCommunity},
4     community::{announce::GetCommunity, send_activity_in_community},
5     generate_activity_id,
6     send_lemmy_activity,
7     verify_is_public,
8     verify_mod_action,
9     verify_person_in_community,
10   },
11   activity_lists::AnnouncableActivities,
12   local_instance,
13   objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson},
14   protocol::activities::block::block_user::BlockUser,
15   ActorType,
16 };
17 use activitypub_federation::{
18   core::object_id::ObjectId,
19   data::Data,
20   traits::{ActivityHandler, Actor},
21   utils::verify_domains_match,
22 };
23 use activitystreams_kinds::{activity::BlockType, public};
24 use anyhow::anyhow;
25 use chrono::NaiveDateTime;
26 use lemmy_api_common::utils::{remove_user_data, remove_user_data_in_community};
27 use lemmy_db_schema::{
28   source::{
29     community::{
30       CommunityFollower,
31       CommunityFollowerForm,
32       CommunityPersonBan,
33       CommunityPersonBanForm,
34     },
35     moderator::{ModBan, ModBanForm, ModBanFromCommunity, ModBanFromCommunityForm},
36     person::{Person, PersonUpdateForm},
37   },
38   traits::{Bannable, Crud, Followable},
39 };
40 use lemmy_utils::{error::LemmyError, utils::convert_datetime};
41 use lemmy_websocket::LemmyContext;
42 use url::Url;
43
44 impl BlockUser {
45   pub(in crate::activities::block) async fn new(
46     target: &SiteOrCommunity,
47     user: &ApubPerson,
48     mod_: &ApubPerson,
49     remove_data: Option<bool>,
50     reason: Option<String>,
51     expires: Option<NaiveDateTime>,
52     context: &LemmyContext,
53   ) -> Result<BlockUser, LemmyError> {
54     Ok(BlockUser {
55       actor: ObjectId::new(mod_.actor_id()),
56       to: vec![public()],
57       object: ObjectId::new(user.actor_id()),
58       cc: generate_cc(target, context.pool()).await?,
59       target: target.id(),
60       kind: BlockType::Block,
61       remove_data,
62       summary: reason,
63       id: generate_activity_id(
64         BlockType::Block,
65         &context.settings().get_protocol_and_hostname(),
66       )?,
67       expires: expires.map(convert_datetime),
68       unparsed: Default::default(),
69     })
70   }
71
72   #[tracing::instrument(skip_all)]
73   pub async fn send(
74     target: &SiteOrCommunity,
75     user: &ApubPerson,
76     mod_: &ApubPerson,
77     remove_data: bool,
78     reason: Option<String>,
79     expires: Option<NaiveDateTime>,
80     context: &LemmyContext,
81   ) -> Result<(), LemmyError> {
82     let block = BlockUser::new(
83       target,
84       user,
85       mod_,
86       Some(remove_data),
87       reason,
88       expires,
89       context,
90     )
91     .await?;
92
93     match target {
94       SiteOrCommunity::Site(_) => {
95         let inboxes = remote_instance_inboxes(context.pool()).await?;
96         send_lemmy_activity(context, block, mod_, inboxes, false).await
97       }
98       SiteOrCommunity::Community(c) => {
99         let activity = AnnouncableActivities::BlockUser(block);
100         let inboxes = vec![user.shared_inbox_or_inbox()];
101         send_activity_in_community(activity, mod_, c, inboxes, context).await
102       }
103     }
104   }
105 }
106
107 #[async_trait::async_trait(?Send)]
108 impl ActivityHandler for BlockUser {
109   type DataType = LemmyContext;
110   type Error = LemmyError;
111
112   fn id(&self) -> &Url {
113     &self.id
114   }
115
116   fn actor(&self) -> &Url {
117     self.actor.inner()
118   }
119
120   #[tracing::instrument(skip_all)]
121   async fn verify(
122     &self,
123     context: &Data<LemmyContext>,
124     request_counter: &mut i32,
125   ) -> Result<(), LemmyError> {
126     verify_is_public(&self.to, &self.cc)?;
127     match self
128       .target
129       .dereference(context, local_instance(context).await, request_counter)
130       .await?
131     {
132       SiteOrCommunity::Site(site) => {
133         let domain = self.object.inner().domain().expect("url needs domain");
134         if context.settings().hostname == domain {
135           return Err(
136             anyhow!("Site bans from remote instance can't affect user's home instance").into(),
137           );
138         }
139         // site ban can only target a user who is on the same instance as the actor (admin)
140         verify_domains_match(&site.actor_id(), self.actor.inner())?;
141         verify_domains_match(&site.actor_id(), self.object.inner())?;
142       }
143       SiteOrCommunity::Community(community) => {
144         verify_person_in_community(&self.actor, &community, context, request_counter).await?;
145         verify_mod_action(
146           &self.actor,
147           self.object.inner(),
148           community.id,
149           context,
150           request_counter,
151         )
152         .await?;
153       }
154     }
155     Ok(())
156   }
157
158   #[tracing::instrument(skip_all)]
159   async fn receive(
160     self,
161     context: &Data<LemmyContext>,
162     request_counter: &mut i32,
163   ) -> Result<(), LemmyError> {
164     let expires = self.expires.map(|u| u.naive_local());
165     let mod_person = self
166       .actor
167       .dereference(context, local_instance(context).await, request_counter)
168       .await?;
169     let blocked_person = self
170       .object
171       .dereference(context, local_instance(context).await, request_counter)
172       .await?;
173     let target = self
174       .target
175       .dereference(context, local_instance(context).await, request_counter)
176       .await?;
177     match target {
178       SiteOrCommunity::Site(_site) => {
179         let blocked_person = Person::update(
180           context.pool(),
181           blocked_person.id,
182           &PersonUpdateForm::builder()
183             .banned(Some(true))
184             .ban_expires(Some(expires))
185             .build(),
186         )
187         .await?;
188         if self.remove_data.unwrap_or(false) {
189           remove_user_data(
190             blocked_person.id,
191             context.pool(),
192             context.settings(),
193             context.client(),
194           )
195           .await?;
196         }
197
198         // write mod log
199         let form = ModBanForm {
200           mod_person_id: mod_person.id,
201           other_person_id: blocked_person.id,
202           reason: self.summary,
203           banned: Some(true),
204           expires,
205         };
206         ModBan::create(context.pool(), &form).await?;
207       }
208       SiteOrCommunity::Community(community) => {
209         let community_user_ban_form = CommunityPersonBanForm {
210           community_id: community.id,
211           person_id: blocked_person.id,
212           expires: Some(expires),
213         };
214         CommunityPersonBan::ban(context.pool(), &community_user_ban_form).await?;
215
216         // Also unsubscribe them from the community, if they are subscribed
217         let community_follower_form = CommunityFollowerForm {
218           community_id: community.id,
219           person_id: blocked_person.id,
220           pending: false,
221         };
222         CommunityFollower::unfollow(context.pool(), &community_follower_form)
223           .await
224           .ok();
225
226         if self.remove_data.unwrap_or(false) {
227           remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
228         }
229
230         // write to mod log
231         let form = ModBanFromCommunityForm {
232           mod_person_id: mod_person.id,
233           other_person_id: blocked_person.id,
234           community_id: community.id,
235           reason: self.summary,
236           banned: Some(true),
237           expires,
238         };
239         ModBanFromCommunity::create(context.pool(), &form).await?;
240       }
241     }
242
243     Ok(())
244   }
245 }
246
247 #[async_trait::async_trait(?Send)]
248 impl GetCommunity for BlockUser {
249   #[tracing::instrument(skip_all)]
250   async fn get_community(
251     &self,
252     context: &LemmyContext,
253     request_counter: &mut i32,
254   ) -> Result<ApubCommunity, LemmyError> {
255     let target = self
256       .target
257       .dereference(context, local_instance(context).await, request_counter)
258       .await?;
259     match target {
260       SiteOrCommunity::Community(c) => Ok(c),
261       SiteOrCommunity::Site(_) => Err(anyhow!("Calling get_community() on site activity").into()),
262     }
263   }
264 }