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