2 activities::verify_community_matches,
3 fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity},
5 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
6 protocol::{objects::LanguageTag, ImageObject, InCommunity, Source},
8 use activitypub_federation::{
9 core::object_id::ObjectId,
12 helpers::{deserialize_one_or_many, deserialize_skip_error},
13 values::MediaTypeMarkdownOrHtml,
15 traits::{ActivityHandler, ApubObject},
17 use activitystreams_kinds::{
19 object::{DocumentType, ImageType},
21 use chrono::{DateTime, FixedOffset};
22 use itertools::Itertools;
23 use lemmy_api_common::context::LemmyContext;
24 use lemmy_db_schema::newtypes::DbUrl;
25 use lemmy_utils::error::LemmyError;
26 use serde::{Deserialize, Serialize};
27 use serde_with::skip_serializing_none;
30 #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
39 #[skip_serializing_none]
40 #[derive(Clone, Debug, Deserialize, Serialize)]
41 #[serde(rename_all = "camelCase")]
43 #[serde(rename = "type")]
44 pub(crate) kind: PageType,
45 pub(crate) id: ObjectId<ApubPost>,
46 pub(crate) attributed_to: AttributedTo,
47 #[serde(deserialize_with = "deserialize_one_or_many")]
48 pub(crate) to: Vec<Url>,
49 pub(crate) name: String,
51 #[serde(deserialize_with = "deserialize_one_or_many", default)]
52 pub(crate) cc: Vec<Url>,
53 pub(crate) content: Option<String>,
54 pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
55 #[serde(deserialize_with = "deserialize_skip_error", default)]
56 pub(crate) source: Option<Source>,
57 /// most software uses array type for attachment field, so we do the same. nevertheless, we only
58 /// use the first item
60 pub(crate) attachment: Vec<Attachment>,
61 pub(crate) image: Option<ImageObject>,
62 pub(crate) comments_enabled: Option<bool>,
63 pub(crate) sensitive: Option<bool>,
64 pub(crate) stickied: Option<bool>,
65 pub(crate) published: Option<DateTime<FixedOffset>>,
66 pub(crate) updated: Option<DateTime<FixedOffset>>,
67 pub(crate) language: Option<LanguageTag>,
68 pub(crate) audience: Option<ObjectId<ApubCommunity>>,
71 #[derive(Clone, Debug, Deserialize, Serialize)]
72 #[serde(rename_all = "camelCase")]
73 pub(crate) struct Link {
75 pub(crate) r#type: LinkType,
78 #[derive(Clone, Debug, Deserialize, Serialize)]
79 #[serde(rename_all = "camelCase")]
80 pub(crate) struct Image {
81 #[serde(rename = "type")]
82 pub(crate) kind: ImageType,
86 #[derive(Clone, Debug, Deserialize, Serialize)]
87 #[serde(rename_all = "camelCase")]
88 pub(crate) struct Document {
89 #[serde(rename = "type")]
90 pub(crate) kind: DocumentType,
94 #[derive(Clone, Debug, Deserialize, Serialize)]
96 pub(crate) enum Attachment {
103 pub(crate) fn url(self) -> Url {
105 // url as sent by Lemmy (new)
106 Attachment::Link(l) => l.href,
107 // image sent by lotide
108 Attachment::Image(i) => i.url,
110 Attachment::Document(d) => d.url,
115 #[derive(Clone, Debug, Deserialize, Serialize)]
117 pub(crate) enum AttributedTo {
118 Lemmy(ObjectId<ApubPerson>),
119 Peertube([AttributedToPeertube; 2]),
122 #[derive(Clone, Debug, Deserialize, Serialize)]
123 #[serde(rename_all = "camelCase")]
124 pub(crate) struct AttributedToPeertube {
125 #[serde(rename = "type")]
126 pub kind: PersonOrGroupType,
127 pub id: ObjectId<UserOrCommunity>,
131 /// Only mods can change the post's stickied/locked status. So if either of these is changed from
132 /// the current value, it is a mod action and needs to be verified as such.
134 /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
135 pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
136 let old_post = ObjectId::<ApubPost>::new(self.id.clone())
137 .dereference_local(context)
140 let featured_changed = Page::is_featured_changed(&old_post, &self.stickied);
141 let locked_changed = Page::is_locked_changed(&old_post, &self.comments_enabled);
142 Ok(featured_changed || locked_changed)
145 pub(crate) fn is_featured_changed<E>(
146 old_post: &Result<ApubPost, E>,
147 new_featured_community: &Option<bool>,
149 if let Some(new_featured_community) = new_featured_community {
150 if let Ok(old_post) = old_post {
151 return new_featured_community != &old_post.featured_community;
158 pub(crate) fn is_locked_changed<E>(
159 old_post: &Result<ApubPost, E>,
160 new_comments_enabled: &Option<bool>,
162 if let Some(new_comments_enabled) = new_comments_enabled {
163 if let Ok(old_post) = old_post {
164 return new_comments_enabled != &!old_post.locked;
171 pub(crate) fn creator(&self) -> Result<ObjectId<ApubPerson>, LemmyError> {
172 match &self.attributed_to {
173 AttributedTo::Lemmy(l) => Ok(l.clone()),
174 AttributedTo::Peertube(p) => p
176 .find(|a| a.kind == PersonOrGroupType::Person)
177 .map(|a| ObjectId::<ApubPerson>::new(a.id.clone().into_inner()))
178 .ok_or_else(|| LemmyError::from_message("page does not specify creator person")),
184 pub(crate) fn new(url: DbUrl) -> Attachment {
185 Attachment::Link(Link {
187 r#type: Default::default(),
192 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
193 #[async_trait::async_trait(?Send)]
194 impl ActivityHandler for Page {
195 type DataType = LemmyContext;
196 type Error = LemmyError;
197 fn id(&self) -> &Url {
200 fn actor(&self) -> &Url {
205 data: &Data<Self::DataType>,
206 request_counter: &mut i32,
207 ) -> Result<(), LemmyError> {
208 ApubPost::verify(self, self.id.inner(), data, request_counter).await
212 data: &Data<Self::DataType>,
213 request_counter: &mut i32,
214 ) -> Result<(), LemmyError> {
215 ApubPost::from_apub(self, data, request_counter).await?;
220 #[async_trait::async_trait(?Send)]
221 impl InCommunity for Page {
224 context: &LemmyContext,
225 request_counter: &mut i32,
226 ) -> Result<ApubCommunity, LemmyError> {
227 let instance = local_instance(context).await;
228 let community = match &self.attributed_to {
229 AttributedTo::Lemmy(_) => {
230 let mut iter = self.to.iter().merge(self.cc.iter());
232 if let Some(cid) = iter.next() {
233 let cid = ObjectId::new(cid.clone());
234 if let Ok(c) = cid.dereference(context, instance, request_counter).await {
238 return Err(LemmyError::from_message("No community found in cc"));
242 AttributedTo::Peertube(p) => {
244 .find(|a| a.kind == PersonOrGroupType::Group)
245 .map(|a| ObjectId::<ApubCommunity>::new(a.id.clone().into_inner()))
246 .ok_or_else(|| LemmyError::from_message("page does not specify group"))?
247 .dereference(context, instance, request_counter)
251 if let Some(audience) = &self.audience {
252 let audience = audience
253 .dereference(context, instance, request_counter)
255 verify_community_matches(&audience, community.id)?;