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