]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/block/block_user.rs
Remove update and read site config. Fixes #2306 (#2329)
[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::{blocking, 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,
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), 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,
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), request_counter)
168       .await?;
169     let blocked_person = self
170       .object
171       .dereference(context, local_instance(context), request_counter)
172       .await?;
173     let target = self
174       .target
175       .dereference(context, local_instance(context), request_counter)
176       .await?;
177     match target {
178       SiteOrCommunity::Site(_site) => {
179         let blocked_person = blocking(context.pool(), move |conn| {
180           Person::ban_person(conn, blocked_person.id, true, expires)
181         })
182         .await??;
183         if self.remove_data.unwrap_or(false) {
184           remove_user_data(
185             blocked_person.id,
186             context.pool(),
187             context.settings(),
188             context.client(),
189           )
190           .await?;
191         }
192
193         // write mod log
194         let form = ModBanForm {
195           mod_person_id: mod_person.id,
196           other_person_id: blocked_person.id,
197           reason: self.summary,
198           banned: Some(true),
199           expires,
200         };
201         blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
202       }
203       SiteOrCommunity::Community(community) => {
204         let community_user_ban_form = CommunityPersonBanForm {
205           community_id: community.id,
206           person_id: blocked_person.id,
207           expires: Some(expires),
208         };
209         blocking(context.pool(), move |conn| {
210           CommunityPersonBan::ban(conn, &community_user_ban_form)
211         })
212         .await??;
213
214         // Also unsubscribe them from the community, if they are subscribed
215         let community_follower_form = CommunityFollowerForm {
216           community_id: community.id,
217           person_id: blocked_person.id,
218           pending: false,
219         };
220         blocking(context.pool(), move |conn: &'_ _| {
221           CommunityFollower::unfollow(conn, &community_follower_form)
222         })
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         blocking(context.pool(), move |conn| {
240           ModBanFromCommunity::create(conn, &form)
241         })
242         .await??;
243       }
244     }
245
246     Ok(())
247   }
248 }
249
250 #[async_trait::async_trait(?Send)]
251 impl GetCommunity for BlockUser {
252   #[tracing::instrument(skip_all)]
253   async fn get_community(
254     &self,
255     context: &LemmyContext,
256     request_counter: &mut i32,
257   ) -> Result<ApubCommunity, LemmyError> {
258     let target = self
259       .target
260       .dereference(context, local_instance(context), request_counter)
261       .await?;
262     match target {
263       SiteOrCommunity::Community(c) => Ok(c),
264       SiteOrCommunity::Site(_) => Err(anyhow!("Calling get_community() on site activity").into()),
265     }
266   }
267 }