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