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