2 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3 protocol::{ImageObject, Source, Unparsed},
5 use activitystreams_kinds::object::PageType;
6 use chrono::{DateTime, FixedOffset};
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 #[skip_serializing_none]
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
23 pub(crate) r#type: PageType,
24 pub(crate) id: ObjectId<ApubPost>,
25 pub(crate) attributed_to: ObjectId<ApubPerson>,
26 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
27 pub(crate) to: Vec<Url>,
29 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
30 pub(crate) cc: Vec<Url>,
31 pub(crate) name: String,
32 pub(crate) content: Option<String>,
33 pub(crate) media_type: Option<MediaTypeHtml>,
34 pub(crate) source: Option<Source>,
35 pub(crate) url: Option<Url>,
36 pub(crate) image: Option<ImageObject>,
37 pub(crate) comments_enabled: Option<bool>,
38 pub(crate) sensitive: Option<bool>,
39 pub(crate) stickied: Option<bool>,
40 pub(crate) published: Option<DateTime<FixedOffset>>,
41 pub(crate) updated: Option<DateTime<FixedOffset>>,
43 pub(crate) unparsed: Unparsed,
47 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
48 /// the current value, it is a mod action and needs to be verified as such.
50 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
51 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
52 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
53 .dereference_local(context)
56 let is_mod_action = if let Ok(old_post) = old_post {
57 self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
64 pub(crate) async fn extract_community(
66 context: &LemmyContext,
67 request_counter: &mut i32,
68 ) -> Result<ApubCommunity, LemmyError> {
69 let mut to_iter = self.to.iter();
71 if let Some(cid) = to_iter.next() {
72 let cid = ObjectId::new(cid.clone());
73 if let Ok(c) = cid.dereference(context, request_counter).await {
77 return Err(LemmyError::from_message("No community found in cc"));
83 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
84 #[async_trait::async_trait(?Send)]
85 impl ActivityHandler for Page {
86 type DataType = LemmyContext;
89 data: &Data<Self::DataType>,
90 request_counter: &mut i32,
91 ) -> Result<(), LemmyError> {
92 ApubPost::verify(self, self.id.inner(), data, request_counter).await
96 data: &Data<Self::DataType>,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
99 ApubPost::from_apub(self, data, request_counter).await?;