]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/deletion/mod.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / apub / src / activities / deletion / mod.rs
1 use crate::{
2   activities::{
3     community::{announce::GetCommunity, send_activity_in_community},
4     send_lemmy_activity,
5     verify_is_public,
6     verify_mod_action,
7     verify_person,
8     verify_person_in_community,
9   },
10   activity_lists::AnnouncableActivities,
11   local_instance,
12   objects::{
13     comment::ApubComment,
14     community::ApubCommunity,
15     person::ApubPerson,
16     post::ApubPost,
17     private_message::ApubPrivateMessage,
18   },
19   protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
20   ActorType,
21 };
22 use activitypub_federation::{
23   core::object_id::ObjectId,
24   traits::{Actor, ApubObject},
25   utils::verify_domains_match,
26 };
27 use activitystreams_kinds::public;
28 use lemmy_api_common::utils::blocking;
29 use lemmy_db_schema::{
30   source::{
31     comment::{Comment, CommentUpdateForm},
32     community::{Community, CommunityUpdateForm},
33     person::Person,
34     post::{Post, PostUpdateForm},
35     private_message::{PrivateMessage, PrivateMessageUpdateForm},
36   },
37   traits::Crud,
38 };
39 use lemmy_utils::error::LemmyError;
40 use lemmy_websocket::{
41   send::{
42     send_comment_ws_message_simple,
43     send_community_ws_message,
44     send_pm_ws_message,
45     send_post_ws_message,
46   },
47   LemmyContext,
48   UserOperationCrud,
49 };
50 use std::ops::Deref;
51 use url::Url;
52
53 pub mod delete;
54 pub mod delete_user;
55 pub mod undo_delete;
56
57 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
58 /// action was done by a normal user.
59 #[tracing::instrument(skip_all)]
60 pub async fn send_apub_delete_in_community(
61   actor: Person,
62   community: Community,
63   object: DeletableObjects,
64   reason: Option<String>,
65   deleted: bool,
66   context: &LemmyContext,
67 ) -> Result<(), LemmyError> {
68   let actor = ApubPerson::from(actor);
69   let activity = if deleted {
70     let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
71     AnnouncableActivities::Delete(delete)
72   } else {
73     let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
74     AnnouncableActivities::UndoDelete(undo)
75   };
76   send_activity_in_community(activity, &actor, &community.into(), vec![], context).await
77 }
78
79 #[tracing::instrument(skip_all)]
80 pub async fn send_apub_delete_private_message(
81   actor: &ApubPerson,
82   pm: PrivateMessage,
83   deleted: bool,
84   context: &LemmyContext,
85 ) -> Result<(), LemmyError> {
86   let recipient_id = pm.recipient_id;
87   let recipient: ApubPerson =
88     blocking(context.pool(), move |conn| Person::read(conn, recipient_id))
89       .await??
90       .into();
91
92   let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
93   let inbox = vec![recipient.shared_inbox_or_inbox()];
94   if deleted {
95     let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
96     send_lemmy_activity(context, delete, actor, inbox, true).await?;
97   } else {
98     let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
99     send_lemmy_activity(context, undo, actor, inbox, true).await?;
100   };
101   Ok(())
102 }
103
104 pub enum DeletableObjects {
105   Community(Box<ApubCommunity>),
106   Comment(Box<ApubComment>),
107   Post(Box<ApubPost>),
108   PrivateMessage(Box<ApubPrivateMessage>),
109 }
110
111 impl DeletableObjects {
112   #[tracing::instrument(skip_all)]
113   pub(crate) async fn read_from_db(
114     ap_id: &Url,
115     context: &LemmyContext,
116   ) -> Result<DeletableObjects, LemmyError> {
117     if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
118       return Ok(DeletableObjects::Community(Box::new(c)));
119     }
120     if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
121       return Ok(DeletableObjects::Post(Box::new(p)));
122     }
123     if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
124       return Ok(DeletableObjects::Comment(Box::new(c)));
125     }
126     if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
127       return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
128     }
129     Err(diesel::NotFound.into())
130   }
131
132   pub(crate) fn id(&self) -> Url {
133     match self {
134       DeletableObjects::Community(c) => c.actor_id(),
135       DeletableObjects::Comment(c) => c.ap_id.clone().into(),
136       DeletableObjects::Post(p) => p.ap_id.clone().into(),
137       DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
138     }
139   }
140 }
141
142 #[tracing::instrument(skip_all)]
143 pub(in crate::activities) async fn verify_delete_activity(
144   activity: &Delete,
145   is_mod_action: bool,
146   context: &LemmyContext,
147   request_counter: &mut i32,
148 ) -> Result<(), LemmyError> {
149   let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
150   match object {
151     DeletableObjects::Community(community) => {
152       verify_is_public(&activity.to, &[])?;
153       if community.local {
154         // can only do this check for local community, in remote case it would try to fetch the
155         // deleted community (which fails)
156         verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
157       }
158       // community deletion is always a mod (or admin) action
159       verify_mod_action(
160         &activity.actor,
161         activity.object.id(),
162         community.id,
163         context,
164         request_counter,
165       )
166       .await?;
167     }
168     DeletableObjects::Post(p) => {
169       verify_is_public(&activity.to, &[])?;
170       verify_delete_post_or_comment(
171         &activity.actor,
172         &p.ap_id.clone().into(),
173         &activity.get_community(context, request_counter).await?,
174         is_mod_action,
175         context,
176         request_counter,
177       )
178       .await?;
179     }
180     DeletableObjects::Comment(c) => {
181       verify_is_public(&activity.to, &[])?;
182       verify_delete_post_or_comment(
183         &activity.actor,
184         &c.ap_id.clone().into(),
185         &activity.get_community(context, request_counter).await?,
186         is_mod_action,
187         context,
188         request_counter,
189       )
190       .await?;
191     }
192     DeletableObjects::PrivateMessage(_) => {
193       verify_person(&activity.actor, context, request_counter).await?;
194       verify_domains_match(activity.actor.inner(), activity.object.id())?;
195     }
196   }
197   Ok(())
198 }
199
200 #[tracing::instrument(skip_all)]
201 async fn verify_delete_post_or_comment(
202   actor: &ObjectId<ApubPerson>,
203   object_id: &Url,
204   community: &ApubCommunity,
205   is_mod_action: bool,
206   context: &LemmyContext,
207   request_counter: &mut i32,
208 ) -> Result<(), LemmyError> {
209   verify_person_in_community(actor, community, context, request_counter).await?;
210   if is_mod_action {
211     verify_mod_action(actor, object_id, community.id, context, request_counter).await?;
212   } else {
213     // domain of post ap_id and post.creator ap_id are identical, so we just check the former
214     verify_domains_match(actor.inner(), object_id)?;
215   }
216   Ok(())
217 }
218
219 /// Write deletion or restoring of an object to the database, and send websocket message.
220 #[tracing::instrument(skip_all)]
221 async fn receive_delete_action(
222   object: &Url,
223   actor: &ObjectId<ApubPerson>,
224   deleted: bool,
225   context: &LemmyContext,
226   request_counter: &mut i32,
227 ) -> Result<(), LemmyError> {
228   match DeletableObjects::read_from_db(object, context).await? {
229     DeletableObjects::Community(community) => {
230       if community.local {
231         let mod_: Person = actor
232           .dereference(context, local_instance(context), request_counter)
233           .await?
234           .deref()
235           .clone();
236         let object = DeletableObjects::Community(community.clone());
237         let c: Community = community.deref().deref().clone();
238         send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
239       }
240
241       let community = blocking(context.pool(), move |conn| {
242         Community::update(
243           conn,
244           community.id,
245           &CommunityUpdateForm::builder()
246             .deleted(Some(deleted))
247             .build(),
248         )
249       })
250       .await??;
251       send_community_ws_message(
252         community.id,
253         UserOperationCrud::DeleteCommunity,
254         None,
255         None,
256         context,
257       )
258       .await?;
259     }
260     DeletableObjects::Post(post) => {
261       if deleted != post.deleted {
262         let deleted_post = blocking(context.pool(), move |conn| {
263           Post::update(
264             conn,
265             post.id,
266             &PostUpdateForm::builder().deleted(Some(deleted)).build(),
267           )
268         })
269         .await??;
270         send_post_ws_message(
271           deleted_post.id,
272           UserOperationCrud::DeletePost,
273           None,
274           None,
275           context,
276         )
277         .await?;
278       }
279     }
280     DeletableObjects::Comment(comment) => {
281       if deleted != comment.deleted {
282         let deleted_comment = blocking(context.pool(), move |conn| {
283           Comment::update(
284             conn,
285             comment.id,
286             &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
287           )
288         })
289         .await??;
290         send_comment_ws_message_simple(
291           deleted_comment.id,
292           UserOperationCrud::DeleteComment,
293           context,
294         )
295         .await?;
296       }
297     }
298     DeletableObjects::PrivateMessage(pm) => {
299       let deleted_private_message = blocking(context.pool(), move |conn| {
300         PrivateMessage::update(
301           conn,
302           pm.id,
303           &PrivateMessageUpdateForm::builder()
304             .deleted(Some(deleted))
305             .build(),
306         )
307       })
308       .await??;
309
310       send_pm_ws_message(
311         deleted_private_message.id,
312         UserOperationCrud::DeletePrivateMessage,
313         None,
314         context,
315       )
316       .await?;
317     }
318   }
319   Ok(())
320 }