]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/page.rs
Migrate towards using page.attachment field for url (ref #2144) (#2182)
[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 is_mod_action = if let Ok(old_post) = old_post {
79       self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
80     } else {
81       false
82     };
83     Ok(is_mod_action)
84   }
85
86   pub(crate) async fn extract_community(
87     &self,
88     context: &LemmyContext,
89     request_counter: &mut i32,
90   ) -> Result<ApubCommunity, LemmyError> {
91     let mut iter = self.to.iter().merge(self.cc.iter());
92     loop {
93       if let Some(cid) = iter.next() {
94         let cid = ObjectId::new(cid.clone());
95         if let Ok(c) = cid
96           .dereference(context, context.client(), request_counter)
97           .await
98         {
99           break Ok(c);
100         }
101       } else {
102         return Err(LemmyError::from_message("No community found in cc"));
103       }
104     }
105   }
106 }
107
108 impl Attachment {
109   pub(crate) fn new(url: DbUrl) -> Attachment {
110     Attachment {
111       href: url.into(),
112       r#type: Default::default(),
113     }
114   }
115 }
116
117 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
118 #[async_trait::async_trait(?Send)]
119 impl ActivityHandler for Page {
120   type DataType = LemmyContext;
121   async fn verify(
122     &self,
123     data: &Data<Self::DataType>,
124     request_counter: &mut i32,
125   ) -> Result<(), LemmyError> {
126     ApubPost::verify(self, self.id.inner(), data, request_counter).await
127   }
128   async fn receive(
129     self,
130     data: &Data<Self::DataType>,
131     request_counter: &mut i32,
132   ) -> Result<(), LemmyError> {
133     ApubPost::from_apub(self, data, request_counter).await?;
134     Ok(())
135   }
136 }