3 community::send_activity_in_community,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
14 community::ApubCommunity,
17 private_message::ApubPrivateMessage,
20 activities::deletion::{delete::Delete, undo_delete::UndoDelete},
25 use activitypub_federation::{
26 core::object_id::ObjectId,
27 traits::{Actor, ApubObject},
28 utils::verify_domains_match,
30 use activitystreams_kinds::public;
31 use lemmy_db_schema::{
33 comment::{Comment, CommentUpdateForm},
34 community::{Community, CommunityUpdateForm},
36 post::{Post, PostUpdateForm},
37 private_message::{PrivateMessage, PrivateMessageUpdateForm},
41 use lemmy_utils::error::LemmyError;
42 use lemmy_websocket::{
44 send_comment_ws_message_simple,
45 send_community_ws_message,
59 /// Parameter `reason` being set indicates that this is a removal by a mod. If its unset, this
60 /// action was done by a normal user.
61 #[tracing::instrument(skip_all)]
62 pub async fn send_apub_delete_in_community(
65 object: DeletableObjects,
66 reason: Option<String>,
68 context: &LemmyContext,
69 ) -> Result<(), LemmyError> {
70 let actor = ApubPerson::from(actor);
71 let is_mod_action = reason.is_some();
72 let activity = if deleted {
73 let delete = Delete::new(&actor, object, public(), Some(&community), reason, context)?;
74 AnnouncableActivities::Delete(delete)
76 let undo = UndoDelete::new(&actor, object, public(), Some(&community), reason, context)?;
77 AnnouncableActivities::UndoDelete(undo)
79 send_activity_in_community(
90 #[tracing::instrument(skip_all)]
91 pub async fn send_apub_delete_private_message(
95 context: &LemmyContext,
96 ) -> Result<(), LemmyError> {
97 let recipient_id = pm.recipient_id;
98 let recipient: ApubPerson = Person::read(context.pool(), recipient_id).await?.into();
100 let deletable = DeletableObjects::PrivateMessage(Box::new(pm.into()));
101 let inbox = vec![recipient.shared_inbox_or_inbox()];
103 let delete = Delete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
104 send_lemmy_activity(context, delete, actor, inbox, true).await?;
106 let undo = UndoDelete::new(actor, deletable, recipient.actor_id(), None, None, context)?;
107 send_lemmy_activity(context, undo, actor, inbox, true).await?;
112 pub enum DeletableObjects {
113 Community(Box<ApubCommunity>),
114 Comment(Box<ApubComment>),
116 PrivateMessage(Box<ApubPrivateMessage>),
119 impl DeletableObjects {
120 #[tracing::instrument(skip_all)]
121 pub(crate) async fn read_from_db(
123 context: &LemmyContext,
124 ) -> Result<DeletableObjects, LemmyError> {
125 if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
126 return Ok(DeletableObjects::Community(Box::new(c)));
128 if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
129 return Ok(DeletableObjects::Post(Box::new(p)));
131 if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
132 return Ok(DeletableObjects::Comment(Box::new(c)));
134 if let Some(p) = ApubPrivateMessage::read_from_apub_id(ap_id.clone(), context).await? {
135 return Ok(DeletableObjects::PrivateMessage(Box::new(p)));
137 Err(diesel::NotFound.into())
140 pub(crate) fn id(&self) -> Url {
142 DeletableObjects::Community(c) => c.actor_id(),
143 DeletableObjects::Comment(c) => c.ap_id.clone().into(),
144 DeletableObjects::Post(p) => p.ap_id.clone().into(),
145 DeletableObjects::PrivateMessage(p) => p.ap_id.clone().into(),
150 #[tracing::instrument(skip_all)]
151 pub(in crate::activities) async fn verify_delete_activity(
154 context: &LemmyContext,
155 request_counter: &mut i32,
156 ) -> Result<(), LemmyError> {
157 let object = DeletableObjects::read_from_db(activity.object.id(), context).await?;
159 DeletableObjects::Community(community) => {
160 verify_is_public(&activity.to, &[])?;
162 // can only do this check for local community, in remote case it would try to fetch the
163 // deleted community (which fails)
164 verify_person_in_community(&activity.actor, &community, context, request_counter).await?;
166 // community deletion is always a mod (or admin) action
169 activity.object.id(),
176 DeletableObjects::Post(p) => {
177 verify_is_public(&activity.to, &[])?;
178 verify_delete_post_or_comment(
180 &p.ap_id.clone().into(),
181 &activity.community(context, request_counter).await?,
188 DeletableObjects::Comment(c) => {
189 verify_is_public(&activity.to, &[])?;
190 verify_delete_post_or_comment(
192 &c.ap_id.clone().into(),
193 &activity.community(context, request_counter).await?,
200 DeletableObjects::PrivateMessage(_) => {
201 verify_person(&activity.actor, context, request_counter).await?;
202 verify_domains_match(activity.actor.inner(), activity.object.id())?;
208 #[tracing::instrument(skip_all)]
209 async fn verify_delete_post_or_comment(
210 actor: &ObjectId<ApubPerson>,
212 community: &ApubCommunity,
214 context: &LemmyContext,
215 request_counter: &mut i32,
216 ) -> Result<(), LemmyError> {
217 verify_person_in_community(actor, community, context, request_counter).await?;
219 verify_mod_action(actor, object_id, community.id, context, request_counter).await?;
221 // domain of post ap_id and post.creator ap_id are identical, so we just check the former
222 verify_domains_match(actor.inner(), object_id)?;
227 /// Write deletion or restoring of an object to the database, and send websocket message.
228 #[tracing::instrument(skip_all)]
229 async fn receive_delete_action(
231 actor: &ObjectId<ApubPerson>,
233 context: &LemmyContext,
234 request_counter: &mut i32,
235 ) -> Result<(), LemmyError> {
236 match DeletableObjects::read_from_db(object, context).await? {
237 DeletableObjects::Community(community) => {
239 let mod_: Person = actor
240 .dereference(context, local_instance(context).await, request_counter)
244 let object = DeletableObjects::Community(community.clone());
245 let c: Community = community.deref().deref().clone();
246 send_apub_delete_in_community(mod_, c, object, None, true, context).await?;
249 let community = Community::update(
252 &CommunityUpdateForm::builder()
253 .deleted(Some(deleted))
257 send_community_ws_message(
259 UserOperationCrud::DeleteCommunity,
266 DeletableObjects::Post(post) => {
267 if deleted != post.deleted {
268 let deleted_post = Post::update(
271 &PostUpdateForm::builder().deleted(Some(deleted)).build(),
274 send_post_ws_message(
276 UserOperationCrud::DeletePost,
284 DeletableObjects::Comment(comment) => {
285 if deleted != comment.deleted {
286 let deleted_comment = Comment::update(
289 &CommentUpdateForm::builder().deleted(Some(deleted)).build(),
292 send_comment_ws_message_simple(
294 UserOperationCrud::DeleteComment,
300 DeletableObjects::PrivateMessage(pm) => {
301 let deleted_private_message = PrivateMessage::update(
304 &PrivateMessageUpdateForm::builder()
305 .deleted(Some(deleted))
311 deleted_private_message.id,
312 UserOperationCrud::DeletePrivateMessage,