2 fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity},
4 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
5 protocol::{ImageObject, Source},
7 use activitypub_federation::{
8 core::object_id::ObjectId,
11 helpers::{deserialize_one_or_many, deserialize_skip_error},
12 values::MediaTypeMarkdownOrHtml,
14 traits::{ActivityHandler, ApubObject},
16 use activitystreams_kinds::{link::LinkType, object::ImageType};
17 use chrono::{DateTime, FixedOffset};
18 use itertools::Itertools;
19 use lemmy_db_schema::newtypes::DbUrl;
20 use lemmy_utils::error::LemmyError;
21 use lemmy_websocket::LemmyContext;
22 use serde::{Deserialize, Serialize};
23 use serde_with::skip_serializing_none;
26 #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
34 #[skip_serializing_none]
35 #[derive(Clone, Debug, Deserialize, Serialize)]
36 #[serde(rename_all = "camelCase")]
38 #[serde(rename = "type")]
39 pub(crate) kind: PageType,
40 pub(crate) id: ObjectId<ApubPost>,
41 pub(crate) attributed_to: AttributedTo,
42 #[serde(deserialize_with = "deserialize_one_or_many")]
43 pub(crate) to: Vec<Url>,
44 pub(crate) name: String,
46 #[serde(deserialize_with = "deserialize_one_or_many", default)]
47 pub(crate) cc: Vec<Url>,
48 pub(crate) content: Option<String>,
49 pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
50 #[serde(deserialize_with = "deserialize_skip_error", default)]
51 pub(crate) source: Option<Source>,
52 /// deprecated, use attachment field
53 #[serde(deserialize_with = "deserialize_skip_error", default)]
54 pub(crate) url: Option<Url>,
55 /// most software uses array type for attachment field, so we do the same. nevertheless, we only
56 /// use the first item
58 pub(crate) attachment: Vec<Attachment>,
59 pub(crate) image: Option<ImageObject>,
60 pub(crate) comments_enabled: Option<bool>,
61 pub(crate) sensitive: Option<bool>,
62 pub(crate) stickied: Option<bool>,
63 pub(crate) published: Option<DateTime<FixedOffset>>,
64 pub(crate) updated: Option<DateTime<FixedOffset>>,
67 #[derive(Clone, Debug, Deserialize, Serialize)]
68 #[serde(rename_all = "camelCase")]
69 pub(crate) struct Link {
71 pub(crate) r#type: LinkType,
74 #[derive(Clone, Debug, Deserialize, Serialize)]
75 #[serde(rename_all = "camelCase")]
76 pub(crate) struct Image {
77 #[serde(rename = "type")]
78 pub(crate) kind: ImageType,
82 #[derive(Clone, Debug, Deserialize, Serialize)]
84 pub(crate) enum Attachment {
89 #[derive(Clone, Debug, Deserialize, Serialize)]
91 pub(crate) enum AttributedTo {
92 Lemmy(ObjectId<ApubPerson>),
93 Peertube([AttributedToPeertube; 2]),
96 #[derive(Clone, Debug, Deserialize, Serialize)]
97 #[serde(rename_all = "camelCase")]
98 pub(crate) struct AttributedToPeertube {
99 #[serde(rename = "type")]
100 pub kind: PersonOrGroupType,
101 pub id: ObjectId<UserOrCommunity>,
105 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
106 /// the current value, it is a mod action and needs to be verified as such.
108 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
109 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
110 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
111 .dereference_local(context)
114 let stickied_changed = Page::is_stickied_changed(&old_post, &self.stickied);
115 let locked_changed = Page::is_locked_changed(&old_post, &self.comments_enabled);
116 Ok(stickied_changed || locked_changed)
119 pub(crate) fn is_stickied_changed<E>(
120 old_post: &Result<ApubPost, E>,
121 new_stickied: &Option<bool>,
123 if let Some(new_stickied) = new_stickied {
124 if let Ok(old_post) = old_post {
125 return new_stickied != &old_post.stickied;
132 pub(crate) fn is_locked_changed<E>(
133 old_post: &Result<ApubPost, E>,
134 new_comments_enabled: &Option<bool>,
136 if let Some(new_comments_enabled) = new_comments_enabled {
137 if let Ok(old_post) = old_post {
138 return new_comments_enabled != &!old_post.locked;
145 pub(crate) async fn extract_community(
147 context: &LemmyContext,
148 request_counter: &mut i32,
149 ) -> Result<ApubCommunity, LemmyError> {
150 match &self.attributed_to {
151 AttributedTo::Lemmy(_) => {
152 let mut iter = self.to.iter().merge(self.cc.iter());
154 if let Some(cid) = iter.next() {
155 let cid = ObjectId::new(cid.clone());
157 .dereference(context, local_instance(context), request_counter)
163 return Err(LemmyError::from_message("No community found in cc"));
167 AttributedTo::Peertube(p) => {
169 .find(|a| a.kind == PersonOrGroupType::Group)
170 .map(|a| ObjectId::<ApubCommunity>::new(a.id.clone().into_inner()))
171 .ok_or_else(|| LemmyError::from_message("page does not specify group"))?
172 .dereference(context, local_instance(context), request_counter)
178 pub(crate) fn creator(&self) -> Result<ObjectId<ApubPerson>, LemmyError> {
179 match &self.attributed_to {
180 AttributedTo::Lemmy(l) => Ok(l.clone()),
181 AttributedTo::Peertube(p) => p
183 .find(|a| a.kind == PersonOrGroupType::Person)
184 .map(|a| ObjectId::<ApubPerson>::new(a.id.clone().into_inner()))
185 .ok_or_else(|| LemmyError::from_message("page does not specify creator person")),
191 pub(crate) fn new(url: DbUrl) -> Attachment {
192 Attachment::Link(Link {
194 r#type: Default::default(),
199 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
200 #[async_trait::async_trait(?Send)]
201 impl ActivityHandler for Page {
202 type DataType = LemmyContext;
203 type Error = LemmyError;
204 fn id(&self) -> &Url {
207 fn actor(&self) -> &Url {
212 data: &Data<Self::DataType>,
213 request_counter: &mut i32,
214 ) -> Result<(), LemmyError> {
215 ApubPost::verify(self, self.id.inner(), data, request_counter).await
219 data: &Data<Self::DataType>,
220 request_counter: &mut i32,
221 ) -> Result<(), LemmyError> {
222 ApubPost::from_apub(self, data, request_counter).await?;