]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/deletion/delete.rs
d92ac2456c044d774a0c8eb1fe79d8682e706c8d
[lemmy.git] / crates / apub / src / protocol / activities / deletion / delete.rs
1 use crate::{
2   activities::{deletion::DeletableObjects, verify_community_matches},
3   local_instance,
4   objects::{community::ApubCommunity, person::ApubPerson},
5   protocol::{objects::tombstone::Tombstone, IdOrNestedObject, InCommunity},
6 };
7 use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
8 use activitystreams_kinds::activity::DeleteType;
9 use anyhow::anyhow;
10 use lemmy_api_common::context::LemmyContext;
11 use lemmy_db_schema::{
12   source::{community::Community, post::Post},
13   traits::Crud,
14 };
15 use lemmy_utils::error::LemmyError;
16 use serde::{Deserialize, Serialize};
17 use serde_with::skip_serializing_none;
18 use url::Url;
19
20 #[skip_serializing_none]
21 #[derive(Clone, Debug, Deserialize, Serialize)]
22 #[serde(rename_all = "camelCase")]
23 pub struct Delete {
24   pub(crate) actor: ObjectId<ApubPerson>,
25   #[serde(deserialize_with = "deserialize_one_or_many")]
26   pub(crate) to: Vec<Url>,
27   pub(crate) object: IdOrNestedObject<Tombstone>,
28   #[serde(rename = "type")]
29   pub(crate) kind: DeleteType,
30   pub(crate) id: Url,
31   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
32
33   #[serde(deserialize_with = "deserialize_one_or_many")]
34   #[serde(default)]
35   #[serde(skip_serializing_if = "Vec::is_empty")]
36   pub(crate) cc: Vec<Url>,
37   /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
38   /// deleting their own content.
39   pub(crate) summary: Option<String>,
40 }
41
42 #[async_trait::async_trait(?Send)]
43 impl InCommunity for Delete {
44   async fn community(
45     &self,
46     context: &LemmyContext,
47     request_counter: &mut i32,
48   ) -> Result<ApubCommunity, LemmyError> {
49     let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
50       DeletableObjects::Community(c) => c.id,
51       DeletableObjects::Comment(c) => {
52         let post = Post::read(context.pool(), c.post_id).await?;
53         post.community_id
54       }
55       DeletableObjects::Post(p) => p.community_id,
56       DeletableObjects::PrivateMessage(_) => {
57         return Err(anyhow!("Private message is not part of community").into())
58       }
59     };
60     if let Some(audience) = &self.audience {
61       let audience = audience
62         .dereference(context, local_instance(context).await, request_counter)
63         .await?;
64       verify_community_matches(&audience, community_id)?;
65       Ok(audience)
66     } else {
67       let community = Community::read(context.pool(), community_id).await?;
68       Ok(community.into())
69     }
70   }
71 }