]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/page.rs
GNU social compatibility (#2100)
[lemmy.git] / crates / apub / src / protocol / objects / page.rs
1 use crate::{
2   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3   protocol::{ImageObject, SourceCompat},
4 };
5 use chrono::{DateTime, FixedOffset};
6 use itertools::Itertools;
7 use lemmy_apub_lib::{
8   data::Data,
9   object_id::ObjectId,
10   traits::{ActivityHandler, ApubObject},
11   values::MediaTypeHtml,
12 };
13 use lemmy_utils::LemmyError;
14 use lemmy_websocket::LemmyContext;
15 use serde::{Deserialize, Serialize};
16 use serde_with::skip_serializing_none;
17 use url::Url;
18
19 #[derive(Clone, Debug, Deserialize, Serialize)]
20 pub enum PageType {
21   Page,
22   Note,
23 }
24
25 #[skip_serializing_none]
26 #[derive(Clone, Debug, Deserialize, Serialize)]
27 #[serde(rename_all = "camelCase")]
28 pub struct Page {
29   pub(crate) r#type: PageType,
30   pub(crate) id: ObjectId<ApubPost>,
31   pub(crate) attributed_to: ObjectId<ApubPerson>,
32   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
33   pub(crate) to: Vec<Url>,
34   pub(crate) name: String,
35
36   #[serde(default)]
37   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
38   pub(crate) cc: Vec<Url>,
39   pub(crate) content: Option<String>,
40   pub(crate) media_type: Option<MediaTypeHtml>,
41   pub(crate) source: Option<SourceCompat>,
42   pub(crate) url: Option<Url>,
43   pub(crate) image: Option<ImageObject>,
44   pub(crate) comments_enabled: Option<bool>,
45   pub(crate) sensitive: Option<bool>,
46   pub(crate) stickied: Option<bool>,
47   pub(crate) published: Option<DateTime<FixedOffset>>,
48   pub(crate) updated: Option<DateTime<FixedOffset>>,
49 }
50
51 impl Page {
52   /// Only mods can change the post's stickied/locked status. So if either of these is changed from
53   /// the current value, it is a mod action and needs to be verified as such.
54   ///
55   /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
56   pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
57     let old_post = ObjectId::<ApubPost>::new(self.id.clone())
58       .dereference_local(context)
59       .await;
60
61     let is_mod_action = if let Ok(old_post) = old_post {
62       self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
63     } else {
64       false
65     };
66     Ok(is_mod_action)
67   }
68
69   pub(crate) async fn extract_community(
70     &self,
71     context: &LemmyContext,
72     request_counter: &mut i32,
73   ) -> Result<ApubCommunity, LemmyError> {
74     let mut iter = self.to.iter().merge(self.cc.iter());
75     loop {
76       if let Some(cid) = iter.next() {
77         let cid = ObjectId::new(cid.clone());
78         if let Ok(c) = cid
79           .dereference(context, context.client(), request_counter)
80           .await
81         {
82           break Ok(c);
83         }
84       } else {
85         return Err(LemmyError::from_message("No community found in cc"));
86       }
87     }
88   }
89 }
90
91 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
92 #[async_trait::async_trait(?Send)]
93 impl ActivityHandler for Page {
94   type DataType = LemmyContext;
95   async fn verify(
96     &self,
97     data: &Data<Self::DataType>,
98     request_counter: &mut i32,
99   ) -> Result<(), LemmyError> {
100     ApubPost::verify(self, self.id.inner(), data, request_counter).await
101   }
102   async fn receive(
103     self,
104     data: &Data<Self::DataType>,
105     request_counter: &mut i32,
106   ) -> Result<(), LemmyError> {
107     ApubPost::from_apub(self, data, request_counter).await?;
108     Ok(())
109   }
110 }