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