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