]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/deletion/delete.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / apub / src / activities / deletion / delete.rs
1 use crate::{
2   activities::{
3     community::announce::AnnouncableActivities,
4     deletion::{
5       receive_delete_action,
6       verify_delete_activity,
7       DeletableObjects,
8       WebsocketMessages,
9     },
10     generate_activity_id,
11     verify_activity,
12   },
13   activity_queue::send_to_community_new,
14   extensions::context::lemmy_context,
15   fetcher::person::get_or_fetch_and_upsert_person,
16   ActorType,
17 };
18 use activitystreams::activity::kind::DeleteType;
19 use anyhow::anyhow;
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
22 use lemmy_db_queries::{
23   source::{comment::Comment_, community::Community_, post::Post_},
24   Crud,
25 };
26 use lemmy_db_schema::source::{
27   comment::Comment,
28   community::Community,
29   moderator::{
30     ModRemoveComment,
31     ModRemoveCommentForm,
32     ModRemoveCommunity,
33     ModRemoveCommunityForm,
34     ModRemovePost,
35     ModRemovePostForm,
36   },
37   person::Person,
38   post::Post,
39 };
40 use lemmy_utils::LemmyError;
41 use lemmy_websocket::{
42   send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
43   LemmyContext,
44   UserOperationCrud,
45 };
46 use url::Url;
47
48 /// This is very confusing, because there are four distinct cases to handle:
49 /// - user deletes their post
50 /// - user deletes their comment
51 /// - remote community mod deletes local community
52 /// - remote community deletes itself (triggered by a mod)
53 ///
54 /// TODO: we should probably change how community deletions work to simplify this. Probably by
55 /// wrapping it in an announce just like other activities, instead of having the community send it.
56 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
57 #[serde(rename_all = "camelCase")]
58 pub struct Delete {
59   pub(in crate::activities::deletion) to: PublicUrl,
60   pub(in crate::activities::deletion) object: Url,
61   pub(in crate::activities::deletion) cc: [Url; 1],
62   #[serde(rename = "type")]
63   pub(in crate::activities::deletion) kind: DeleteType,
64   /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
65   /// deleting their own content.
66   pub(in crate::activities::deletion) summary: Option<String>,
67   #[serde(flatten)]
68   pub(in crate::activities::deletion) common: ActivityCommonFields,
69 }
70
71 #[async_trait::async_trait(?Send)]
72 impl ActivityHandler for Delete {
73   async fn verify(
74     &self,
75     context: &LemmyContext,
76     request_counter: &mut i32,
77   ) -> Result<(), LemmyError> {
78     verify_activity(self.common())?;
79     verify_delete_activity(
80       &self.object,
81       &self.cc[0],
82       &self.common,
83       self.summary.is_some(),
84       context,
85       request_counter,
86     )
87     .await?;
88     Ok(())
89   }
90
91   async fn receive(
92     self,
93     context: &LemmyContext,
94     request_counter: &mut i32,
95   ) -> Result<(), LemmyError> {
96     if let Some(reason) = self.summary {
97       // We set reason to empty string if it doesn't exist, to distinguish between delete and
98       // remove. Here we change it back to option, so we don't write it to db.
99       let reason = if reason.is_empty() {
100         None
101       } else {
102         Some(reason)
103       };
104       receive_remove_action(
105         &self.common.actor,
106         &self.object,
107         reason,
108         context,
109         request_counter,
110       )
111       .await
112     } else {
113       receive_delete_action(
114         &self.object,
115         &self.common.actor,
116         WebsocketMessages {
117           community: UserOperationCrud::DeleteCommunity,
118           post: UserOperationCrud::DeletePost,
119           comment: UserOperationCrud::DeleteComment,
120         },
121         true,
122         context,
123         request_counter,
124       )
125       .await
126     }
127   }
128
129   fn common(&self) -> &ActivityCommonFields {
130     &self.common
131   }
132 }
133
134 impl Delete {
135   pub(in crate::activities::deletion) async fn send(
136     actor: &Person,
137     community: &Community,
138     object_id: Url,
139     summary: Option<String>,
140     context: &LemmyContext,
141   ) -> Result<(), LemmyError> {
142     let id = generate_activity_id(DeleteType::Delete)?;
143     let delete = Delete {
144       to: PublicUrl::Public,
145       object: object_id,
146       cc: [community.actor_id()],
147       kind: DeleteType::Delete,
148       summary,
149       common: ActivityCommonFields {
150         context: lemmy_context(),
151         id: id.clone(),
152         actor: actor.actor_id(),
153         unparsed: Default::default(),
154       },
155     };
156
157     let activity = AnnouncableActivities::Delete(delete);
158     send_to_community_new(activity, &id, actor, community, vec![], context).await
159   }
160 }
161
162 pub(in crate::activities) async fn receive_remove_action(
163   actor: &Url,
164   object: &Url,
165   reason: Option<String>,
166   context: &LemmyContext,
167   request_counter: &mut i32,
168 ) -> Result<(), LemmyError> {
169   let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
170   use UserOperationCrud::*;
171   match DeletableObjects::read_from_db(object, context).await? {
172     DeletableObjects::Community(community) => {
173       if community.local {
174         return Err(anyhow!("Only local admin can remove community").into());
175       }
176       let form = ModRemoveCommunityForm {
177         mod_person_id: actor.id,
178         community_id: community.id,
179         removed: Some(true),
180         reason,
181         expires: None,
182       };
183       blocking(context.pool(), move |conn| {
184         ModRemoveCommunity::create(conn, &form)
185       })
186       .await??;
187       let deleted_community = blocking(context.pool(), move |conn| {
188         Community::update_removed(conn, community.id, true)
189       })
190       .await??;
191
192       send_community_ws_message(deleted_community.id, RemoveCommunity, None, None, context).await?;
193     }
194     DeletableObjects::Post(post) => {
195       let form = ModRemovePostForm {
196         mod_person_id: actor.id,
197         post_id: post.id,
198         removed: Some(true),
199         reason,
200       };
201       blocking(context.pool(), move |conn| {
202         ModRemovePost::create(conn, &form)
203       })
204       .await??;
205       let removed_post = blocking(context.pool(), move |conn| {
206         Post::update_removed(conn, post.id, true)
207       })
208       .await??;
209
210       send_post_ws_message(removed_post.id, RemovePost, None, None, context).await?;
211     }
212     DeletableObjects::Comment(comment) => {
213       let form = ModRemoveCommentForm {
214         mod_person_id: actor.id,
215         comment_id: comment.id,
216         removed: Some(true),
217         reason,
218       };
219       blocking(context.pool(), move |conn| {
220         ModRemoveComment::create(conn, &form)
221       })
222       .await??;
223       let removed_comment = blocking(context.pool(), move |conn| {
224         Comment::update_removed(conn, comment.id, true)
225       })
226       .await??;
227
228       send_comment_ws_message_simple(removed_comment.id, RemoveComment, context).await?;
229     }
230   }
231   Ok(())
232 }