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)]
25 #[skip_serializing_none]
26 #[derive(Clone, Debug, Deserialize, Serialize)]
27 #[serde(rename_all = "camelCase")]
29 pub(crate) r#type: PageType,
30 pub(crate) id: ObjectId<ApubPost>,
31 pub(crate) attributed_to: ObjectId<ApubPerson>,
32 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
33 pub(crate) to: Vec<Url>,
34 pub(crate) name: String,
37 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
38 pub(crate) cc: Vec<Url>,
39 pub(crate) content: Option<String>,
40 pub(crate) media_type: Option<MediaTypeHtml>,
41 pub(crate) source: Option<SourceCompat>,
42 pub(crate) url: Option<Url>,
43 pub(crate) image: Option<ImageObject>,
44 pub(crate) comments_enabled: Option<bool>,
45 pub(crate) sensitive: Option<bool>,
46 pub(crate) stickied: Option<bool>,
47 pub(crate) published: Option<DateTime<FixedOffset>>,
48 pub(crate) updated: Option<DateTime<FixedOffset>>,
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 iter = self.to.iter().merge(self.cc.iter());
76 if let Some(cid) = 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?;