]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/deletion/delete.rs
Implement separate mod activities for feature, lock post (#2716)
[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::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
7 use activitystreams_kinds::activity::DeleteType;
8 use anyhow::anyhow;
9 use lemmy_api_common::context::LemmyContext;
10 use lemmy_db_schema::{
11   source::{community::Community, post::Post},
12   traits::Crud,
13 };
14 use lemmy_utils::error::LemmyError;
15 use serde::{Deserialize, Serialize};
16 use serde_with::skip_serializing_none;
17 use url::Url;
18
19 #[skip_serializing_none]
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
22 pub struct Delete {
23   pub(crate) actor: ObjectId<ApubPerson>,
24   #[serde(deserialize_with = "deserialize_one_or_many")]
25   pub(crate) to: Vec<Url>,
26   pub(crate) object: IdOrNestedObject<Tombstone>,
27   #[serde(rename = "type")]
28   pub(crate) kind: DeleteType,
29   pub(crate) id: Url,
30   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
31
32   #[serde(deserialize_with = "deserialize_one_or_many")]
33   #[serde(default)]
34   #[serde(skip_serializing_if = "Vec::is_empty")]
35   pub(crate) cc: Vec<Url>,
36   /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
37   /// deleting their own content.
38   pub(crate) summary: Option<String>,
39 }
40
41 #[async_trait::async_trait(?Send)]
42 impl InCommunity for Delete {
43   async fn community(
44     &self,
45     context: &LemmyContext,
46     _request_counter: &mut i32,
47   ) -> 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 }