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