]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/page.rs
Federation: dont overwrite local object from Announce activity (#2232)
[lemmy.git] / crates / apub / src / protocol / objects / page.rs
1 use crate::{
2   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3   protocol::{ImageObject, Source},
4 };
5 use activitystreams_kinds::link::LinkType;
6 use chrono::{DateTime, FixedOffset};
7 use itertools::Itertools;
8 use lemmy_apub_lib::{
9   data::Data,
10   object_id::ObjectId,
11   traits::{ActivityHandler, ApubObject},
12   values::MediaTypeHtml,
13 };
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;
19 use url::Url;
20
21 #[derive(Clone, Debug, Deserialize, Serialize)]
22 pub enum PageType {
23   Page,
24   Article,
25   Note,
26 }
27
28 #[skip_serializing_none]
29 #[derive(Clone, Debug, Deserialize, Serialize)]
30 #[serde(rename_all = "camelCase")]
31 pub struct Page {
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,
38
39   #[serde(default)]
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>,
44   #[serde(default)]
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
51   #[serde(default)]
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>>,
59 }
60
61 #[derive(Clone, Debug, Deserialize, Serialize)]
62 #[serde(rename_all = "camelCase")]
63 pub struct Attachment {
64   pub(crate) href: Url,
65   pub(crate) r#type: LinkType,
66 }
67
68 impl Page {
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.
71   ///
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)
76       .await;
77
78     let stickied_changed = Page::is_stickied_changed(&old_post, &self.stickied);
79     let locked_changed = Page::is_locked_changed(&old_post, &self.comments_enabled);
80     Ok(stickied_changed || locked_changed)
81   }
82
83   pub(crate) fn is_stickied_changed<E>(
84     old_post: &Result<ApubPost, E>,
85     new_stickied: &Option<bool>,
86   ) -> bool {
87     if let Some(new_stickied) = new_stickied {
88       if let Ok(old_post) = old_post {
89         return new_stickied != &old_post.stickied;
90       }
91     }
92
93     false
94   }
95
96   pub(crate) fn is_locked_changed<E>(
97     old_post: &Result<ApubPost, E>,
98     new_comments_enabled: &Option<bool>,
99   ) -> 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;
103       }
104     }
105
106     false
107   }
108
109   pub(crate) async fn extract_community(
110     &self,
111     context: &LemmyContext,
112     request_counter: &mut i32,
113   ) -> Result<ApubCommunity, LemmyError> {
114     let mut iter = self.to.iter().merge(self.cc.iter());
115     loop {
116       if let Some(cid) = iter.next() {
117         let cid = ObjectId::new(cid.clone());
118         if let Ok(c) = cid
119           .dereference(context, context.client(), request_counter)
120           .await
121         {
122           break Ok(c);
123         }
124       } else {
125         return Err(LemmyError::from_message("No community found in cc"));
126       }
127     }
128   }
129 }
130
131 impl Attachment {
132   pub(crate) fn new(url: DbUrl) -> Attachment {
133     Attachment {
134       href: url.into(),
135       r#type: Default::default(),
136     }
137   }
138 }
139
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;
144   async fn verify(
145     &self,
146     data: &Data<Self::DataType>,
147     request_counter: &mut i32,
148   ) -> Result<(), LemmyError> {
149     ApubPost::verify(self, self.id.inner(), data, request_counter).await
150   }
151   async fn receive(
152     self,
153     data: &Data<Self::DataType>,
154     request_counter: &mut i32,
155   ) -> Result<(), LemmyError> {
156     ApubPost::from_apub(self, data, request_counter).await?;
157     Ok(())
158   }
159 }