2 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3 protocol::{ImageObject, SourceCompat},
5 use chrono::{DateTime, FixedOffset};
6 use itertools::Itertools;
10 traits::{ActivityHandler, ApubObject},
11 values::MediaTypeHtml,
13 use lemmy_utils::LemmyError;
14 use lemmy_websocket::LemmyContext;
15 use serde::{Deserialize, Serialize};
16 use serde_with::skip_serializing_none;
19 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[skip_serializing_none]
27 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[serde(rename_all = "camelCase")]
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,
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>>,
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.
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)
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)
70 pub(crate) async fn extract_community(
72 context: &LemmyContext,
73 request_counter: &mut i32,
74 ) -> Result<ApubCommunity, LemmyError> {
75 let mut iter = self.to.iter().merge(self.cc.iter());
77 if let Some(cid) = iter.next() {
78 let cid = ObjectId::new(cid.clone());
80 .dereference(context, context.client(), request_counter)
86 return Err(LemmyError::from_message("No community found in cc"));
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;
98 data: &Data<Self::DataType>,
99 request_counter: &mut i32,
100 ) -> Result<(), LemmyError> {
101 ApubPost::verify(self, self.id.inner(), data, request_counter).await
105 data: &Data<Self::DataType>,
106 request_counter: &mut i32,
107 ) -> Result<(), LemmyError> {
108 ApubPost::from_apub(self, data, request_counter).await?;