2 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3 protocol::{ImageObject, Source},
5 use activitystreams_kinds::link::LinkType;
6 use chrono::{DateTime, FixedOffset};
7 use itertools::Itertools;
11 traits::{ActivityHandler, ApubObject},
12 values::MediaTypeHtml,
14 use lemmy_db_schema::newtypes::DbUrl;
15 use lemmy_utils::LemmyError;
16 use lemmy_websocket::LemmyContext;
17 use serde::{Deserialize, Serialize};
18 use serde_with::skip_serializing_none;
21 #[derive(Clone, Debug, Deserialize, Serialize)]
28 #[skip_serializing_none]
29 #[derive(Clone, Debug, Deserialize, Serialize)]
30 #[serde(rename_all = "camelCase")]
32 pub(crate) r#type: PageType,
33 pub(crate) id: ObjectId<ApubPost>,
34 pub(crate) attributed_to: ObjectId<ApubPerson>,
35 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
36 pub(crate) to: Vec<Url>,
37 pub(crate) name: String,
40 #[serde(deserialize_with = "crate::deserialize_one_or_many")]
41 pub(crate) cc: Vec<Url>,
42 pub(crate) content: Option<String>,
43 pub(crate) media_type: Option<MediaTypeHtml>,
45 #[serde(deserialize_with = "crate::deserialize_skip_error")]
46 pub(crate) source: Option<Source>,
47 /// deprecated, use attachment field
48 pub(crate) url: Option<Url>,
49 /// most software uses array type for attachment field, so we do the same. nevertheless, we only
50 /// use the first item
52 pub(crate) attachment: Vec<Attachment>,
53 pub(crate) image: Option<ImageObject>,
54 pub(crate) comments_enabled: Option<bool>,
55 pub(crate) sensitive: Option<bool>,
56 pub(crate) stickied: Option<bool>,
57 pub(crate) published: Option<DateTime<FixedOffset>>,
58 pub(crate) updated: Option<DateTime<FixedOffset>>,
61 #[derive(Clone, Debug, Deserialize, Serialize)]
62 #[serde(rename_all = "camelCase")]
63 pub struct Attachment {
65 pub(crate) r#type: LinkType,
69 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
70 /// the current value, it is a mod action and needs to be verified as such.
72 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
73 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
74 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
75 .dereference_local(context)
78 let is_mod_action = Page::is_stickied_changed(&old_post, &self.stickied)
79 || Page::is_locked_changed(&old_post, &self.comments_enabled);
83 pub(crate) fn is_stickied_changed<E>(
84 old_post: &Result<ApubPost, E>,
85 new_stickied: &Option<bool>,
87 if let Some(new_stickied) = new_stickied {
88 if let Ok(old_post) = old_post {
89 return new_stickied != &old_post.stickied;
96 pub(crate) fn is_locked_changed<E>(
97 old_post: &Result<ApubPost, E>,
98 new_comments_enabled: &Option<bool>,
100 if let Some(new_comments_enabled) = new_comments_enabled {
101 if let Ok(old_post) = old_post {
102 return new_comments_enabled != &!old_post.locked;
109 pub(crate) async fn extract_community(
111 context: &LemmyContext,
112 request_counter: &mut i32,
113 ) -> Result<ApubCommunity, LemmyError> {
114 let mut iter = self.to.iter().merge(self.cc.iter());
116 if let Some(cid) = iter.next() {
117 let cid = ObjectId::new(cid.clone());
119 .dereference(context, context.client(), request_counter)
125 return Err(LemmyError::from_message("No community found in cc"));
132 pub(crate) fn new(url: DbUrl) -> Attachment {
135 r#type: Default::default(),
140 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
141 #[async_trait::async_trait(?Send)]
142 impl ActivityHandler for Page {
143 type DataType = LemmyContext;
146 data: &Data<Self::DataType>,
147 request_counter: &mut i32,
148 ) -> Result<(), LemmyError> {
149 ApubPost::verify(self, self.id.inner(), data, request_counter).await
153 data: &Data<Self::DataType>,
154 request_counter: &mut i32,
155 ) -> Result<(), LemmyError> {
156 ApubPost::from_apub(self, data, request_counter).await?;