]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/deletion/mod.rs
Reduce stack memory usage in apub code
[lemmy.git] / crates / apub / src / activities / deletion / mod.rs
1 use crate::{
2   activities::{verify_mod_action, verify_person_in_community},
3   objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
4   protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
5 };
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{
8   object_id::ObjectId,
9   traits::{ActorType, ApubObject},
10   verify::verify_domains_match,
11 };
12 use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
13 use lemmy_utils::LemmyError;
14 use lemmy_websocket::{
15   send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
16   LemmyContext,
17   UserOperationCrud,
18 };
19 use url::Url;
20
21 pub mod delete;
22 pub mod undo_delete;
23
24 pub async fn send_apub_delete(
25   actor: &ApubPerson,
26   community: &ApubCommunity,
27   object_id: Url,
28   deleted: bool,
29   context: &LemmyContext,
30 ) -> Result<(), LemmyError> {
31   if deleted {
32     Delete::send(actor, community, object_id, None, context).await
33   } else {
34     UndoDelete::send(actor, community, object_id, None, context).await
35   }
36 }
37
38 // TODO: remove reason is actually optional in lemmy. we set an empty string in that case, but its
39 //       ugly
40 pub async fn send_apub_remove(
41   actor: &ApubPerson,
42   community: &ApubCommunity,
43   object_id: Url,
44   reason: String,
45   removed: bool,
46   context: &LemmyContext,
47 ) -> Result<(), LemmyError> {
48   if removed {
49     Delete::send(actor, community, object_id, Some(reason), context).await
50   } else {
51     UndoDelete::send(actor, community, object_id, Some(reason), context).await
52   }
53 }
54
55 pub enum DeletableObjects {
56   Community(ApubCommunity),
57   Comment(ApubComment),
58   Post(ApubPost),
59 }
60
61 impl DeletableObjects {
62   pub(crate) async fn read_from_db(
63     ap_id: &Url,
64     context: &LemmyContext,
65   ) -> Result<DeletableObjects, LemmyError> {
66     if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
67       return Ok(DeletableObjects::Community(c));
68     }
69     if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
70       return Ok(DeletableObjects::Post(p));
71     }
72     if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
73       return Ok(DeletableObjects::Comment(c));
74     }
75     Err(diesel::NotFound.into())
76   }
77 }
78
79 pub(in crate::activities) async fn verify_delete_activity(
80   object: &Url,
81   actor: &ObjectId<ApubPerson>,
82   community: &ApubCommunity,
83   is_mod_action: bool,
84   context: &LemmyContext,
85   request_counter: &mut i32,
86 ) -> Result<(), LemmyError> {
87   let object = DeletableObjects::read_from_db(object, context).await?;
88   match object {
89     DeletableObjects::Community(community) => {
90       if community.local {
91         // can only do this check for local community, in remote case it would try to fetch the
92         // deleted community (which fails)
93         verify_person_in_community(actor, &community, context, request_counter).await?;
94       }
95       // community deletion is always a mod (or admin) action
96       verify_mod_action(actor, &community, context, request_counter).await?;
97     }
98     DeletableObjects::Post(p) => {
99       verify_delete_activity_post_or_comment(
100         actor,
101         &p.ap_id.clone().into(),
102         community,
103         is_mod_action,
104         context,
105         request_counter,
106       )
107       .await?;
108     }
109     DeletableObjects::Comment(c) => {
110       verify_delete_activity_post_or_comment(
111         actor,
112         &c.ap_id.clone().into(),
113         community,
114         is_mod_action,
115         context,
116         request_counter,
117       )
118       .await?;
119     }
120   }
121   Ok(())
122 }
123
124 async fn verify_delete_activity_post_or_comment(
125   actor: &ObjectId<ApubPerson>,
126   object_id: &Url,
127   community: &ApubCommunity,
128   is_mod_action: bool,
129   context: &LemmyContext,
130   request_counter: &mut i32,
131 ) -> Result<(), LemmyError> {
132   verify_person_in_community(actor, community, context, request_counter).await?;
133   if is_mod_action {
134     verify_mod_action(actor, community, context, request_counter).await?;
135   } else {
136     // domain of post ap_id and post.creator ap_id are identical, so we just check the former
137     verify_domains_match(actor.inner(), object_id)?;
138   }
139   Ok(())
140 }
141
142 /// Write deletion or restoring of an object to the database, and send websocket message.
143 /// TODO: we should do something similar for receive_remove_action(), but its much more complicated
144 ///       because of the mod log
145 async fn receive_delete_action(
146   object: &Url,
147   actor: &ObjectId<ApubPerson>,
148   deleted: bool,
149   context: &LemmyContext,
150   request_counter: &mut i32,
151 ) -> Result<(), LemmyError> {
152   match DeletableObjects::read_from_db(object, context).await? {
153     DeletableObjects::Community(community) => {
154       if community.local {
155         let mod_ = actor.dereference(context, request_counter).await?;
156         let object = community.actor_id();
157         send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
158       }
159
160       let community = blocking(context.pool(), move |conn| {
161         Community::update_deleted(conn, community.id, deleted)
162       })
163       .await??;
164       send_community_ws_message(
165         community.id,
166         UserOperationCrud::DeleteCommunity,
167         None,
168         None,
169         context,
170       )
171       .await?;
172     }
173     DeletableObjects::Post(post) => {
174       if deleted != post.deleted {
175         let deleted_post = blocking(context.pool(), move |conn| {
176           Post::update_deleted(conn, post.id, deleted)
177         })
178         .await??;
179         send_post_ws_message(
180           deleted_post.id,
181           UserOperationCrud::DeletePost,
182           None,
183           None,
184           context,
185         )
186         .await?;
187       }
188     }
189     DeletableObjects::Comment(comment) => {
190       if deleted != comment.deleted {
191         let deleted_comment = blocking(context.pool(), move |conn| {
192           Comment::update_deleted(conn, comment.id, deleted)
193         })
194         .await??;
195         send_comment_ws_message_simple(
196           deleted_comment.id,
197           UserOperationCrud::DeleteComment,
198           context,
199         )
200         .await?;
201       }
202     }
203   }
204   Ok(())
205 }