]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/community/lock_page.rs
Implement separate mod activities for feature, lock post (#2716)
[lemmy.git] / crates / apub / src / protocol / activities / community / lock_page.rs
1 use crate::{
2   activities::verify_community_matches,
3   local_instance,
4   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
5   protocol::InCommunity,
6 };
7 use activitypub_federation::{core::object_id::ObjectId, deser::helpers::deserialize_one_or_many};
8 use activitystreams_kinds::activity::UndoType;
9 use lemmy_api_common::context::LemmyContext;
10 use lemmy_db_schema::{source::community::Community, traits::Crud};
11 use lemmy_utils::error::LemmyError;
12 use serde::{Deserialize, Serialize};
13 use strum_macros::Display;
14 use url::Url;
15
16 #[derive(Clone, Debug, Deserialize, Serialize, Display)]
17 pub enum LockType {
18   Lock,
19 }
20
21 #[derive(Clone, Debug, Deserialize, Serialize)]
22 #[serde(rename_all = "camelCase")]
23 pub struct LockPage {
24   pub(crate) actor: ObjectId<ApubPerson>,
25   #[serde(deserialize_with = "deserialize_one_or_many")]
26   pub(crate) to: Vec<Url>,
27   pub(crate) object: ObjectId<ApubPost>,
28   #[serde(deserialize_with = "deserialize_one_or_many")]
29   pub(crate) cc: Vec<Url>,
30   #[serde(rename = "type")]
31   pub(crate) kind: LockType,
32   pub(crate) id: Url,
33   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
34 }
35
36 #[derive(Clone, Debug, Deserialize, Serialize)]
37 #[serde(rename_all = "camelCase")]
38 pub struct UndoLockPage {
39   pub(crate) actor: ObjectId<ApubPerson>,
40   #[serde(deserialize_with = "deserialize_one_or_many")]
41   pub(crate) to: Vec<Url>,
42   pub(crate) object: LockPage,
43   #[serde(deserialize_with = "deserialize_one_or_many")]
44   pub(crate) cc: Vec<Url>,
45   #[serde(rename = "type")]
46   pub(crate) kind: UndoType,
47   pub(crate) id: Url,
48   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
49 }
50
51 #[async_trait::async_trait(?Send)]
52 impl InCommunity for LockPage {
53   async fn community(
54     &self,
55     context: &LemmyContext,
56     request_counter: &mut i32,
57   ) -> Result<ApubCommunity, LemmyError> {
58     let post = self
59       .object
60       .dereference(context, local_instance(context).await, request_counter)
61       .await?;
62     let community = Community::read(context.pool(), post.community_id).await?;
63     if let Some(audience) = &self.audience {
64       verify_community_matches(audience, community.actor_id.clone())?;
65     }
66     Ok(community.into())
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl InCommunity for UndoLockPage {
72   async fn community(
73     &self,
74     context: &LemmyContext,
75     request_counter: &mut i32,
76   ) -> Result<ApubCommunity, LemmyError> {
77     let community = self.object.community(context, request_counter).await?;
78     if let Some(audience) = &self.audience {
79       verify_community_matches(audience, community.actor_id.clone())?;
80     }
81     Ok(community)
82   }
83 }