2 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3 protocol::{ImageObject, Source, Unparsed},
5 use chrono::{DateTime, FixedOffset};
9 traits::{ActivityHandler, ApubObject},
10 values::MediaTypeHtml,
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
15 use serde_with::skip_serializing_none;
18 #[derive(Clone, Debug, Deserialize, Serialize)]
24 #[skip_serializing_none]
25 #[derive(Clone, Debug, Deserialize, Serialize)]
26 #[serde(rename_all = "camelCase")]
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>,
34 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
35 pub(crate) cc: Vec<Url>,
36 pub(crate) name: String,
37 pub(crate) content: Option<String>,
38 pub(crate) media_type: Option<MediaTypeHtml>,
39 pub(crate) source: Option<Source>,
40 pub(crate) url: Option<Url>,
41 pub(crate) image: Option<ImageObject>,
42 pub(crate) comments_enabled: Option<bool>,
43 pub(crate) sensitive: Option<bool>,
44 pub(crate) stickied: Option<bool>,
45 pub(crate) published: Option<DateTime<FixedOffset>>,
46 pub(crate) updated: Option<DateTime<FixedOffset>>,
48 pub(crate) unparsed: Unparsed,
52 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
53 /// the current value, it is a mod action and needs to be verified as such.
55 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
56 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
57 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
58 .dereference_local(context)
61 let is_mod_action = if let Ok(old_post) = old_post {
62 self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
69 pub(crate) async fn extract_community(
71 context: &LemmyContext,
72 request_counter: &mut i32,
73 ) -> Result<ApubCommunity, LemmyError> {
74 let mut to_iter = self.to.iter();
76 if let Some(cid) = to_iter.next() {
77 let cid = ObjectId::new(cid.clone());
79 .dereference(context, context.client(), request_counter)
85 return Err(LemmyError::from_message("No community found in cc"));
91 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
92 #[async_trait::async_trait(?Send)]
93 impl ActivityHandler for Page {
94 type DataType = LemmyContext;
97 data: &Data<Self::DataType>,
98 request_counter: &mut i32,
99 ) -> Result<(), LemmyError> {
100 ApubPost::verify(self, self.id.inner(), data, request_counter).await
104 data: &Data<Self::DataType>,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
107 ApubPost::from_apub(self, data, request_counter).await?;