2 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3 protocol::{ImageObject, Source},
5 use activitystreams::{object::kind::PageType, unparsed::Unparsed};
7 use chrono::{DateTime, FixedOffset};
11 traits::ActivityHandler,
12 values::MediaTypeHtml,
14 use lemmy_utils::LemmyError;
15 use lemmy_websocket::LemmyContext;
16 use serde::{Deserialize, Serialize};
17 use serde_with::skip_serializing_none;
20 #[skip_serializing_none]
21 #[derive(Clone, Debug, Deserialize, Serialize)]
22 #[serde(rename_all = "camelCase")]
24 pub(crate) r#type: PageType,
25 pub(crate) id: ObjectId<ApubPost>,
26 pub(crate) attributed_to: ObjectId<ApubPerson>,
27 pub(crate) to: Vec<Url>,
29 pub(crate) cc: Vec<Url>,
30 pub(crate) name: String,
31 pub(crate) content: Option<String>,
32 pub(crate) media_type: Option<MediaTypeHtml>,
33 pub(crate) source: Option<Source>,
34 pub(crate) url: Option<Url>,
35 pub(crate) image: Option<ImageObject>,
36 pub(crate) comments_enabled: Option<bool>,
37 pub(crate) sensitive: Option<bool>,
38 pub(crate) stickied: Option<bool>,
39 pub(crate) published: Option<DateTime<FixedOffset>>,
40 pub(crate) updated: Option<DateTime<FixedOffset>>,
42 pub(crate) unparsed: Unparsed,
46 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
47 /// the current value, it is a mod action and needs to be verified as such.
49 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
50 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
51 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
52 .dereference_local(context)
55 let is_mod_action = if let Ok(old_post) = old_post {
56 self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
63 pub(crate) async fn extract_community(
65 context: &LemmyContext,
66 request_counter: &mut i32,
67 ) -> Result<ApubCommunity, LemmyError> {
68 let mut to_iter = self.to.iter();
70 if let Some(cid) = to_iter.next() {
71 let cid = ObjectId::new(cid.clone());
72 if let Ok(c) = cid.dereference(context, request_counter).await {
76 return Err(anyhow!("No community found in cc").into());
82 // For Pleroma/Mastodon compat. Unimplemented because its only used for sending.
83 #[async_trait::async_trait(?Send)]
84 impl ActivityHandler for Page {
85 type DataType = LemmyContext;
86 async fn verify(&self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {
87 Err(anyhow!("Announce/Page can only be sent, not received").into())
89 async fn receive(self, _: &Data<Self::DataType>, _: &mut i32) -> Result<(), LemmyError> {