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