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