]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/objects/page.rs
Consolidate reqwest clients, use reqwest-middleware for tracing
[lemmy.git] / crates / apub / src / protocol / objects / page.rs
1 use crate::{
2   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
3   protocol::{ImageObject, Source, Unparsed},
4 };
5 use activitystreams_kinds::object::PageType;
6 use chrono::{DateTime, FixedOffset};
7 use lemmy_apub_lib::{
8   data::Data,
9   object_id::ObjectId,
10   traits::{ActivityHandler, ApubObject},
11   values::MediaTypeHtml,
12 };
13 use lemmy_utils::LemmyError;
14 use lemmy_websocket::LemmyContext;
15 use serde::{Deserialize, Serialize};
16 use serde_with::skip_serializing_none;
17 use url::Url;
18
19 #[skip_serializing_none]
20 #[derive(Clone, Debug, Deserialize, Serialize)]
21 #[serde(rename_all = "camelCase")]
22 pub struct Page {
23   pub(crate) r#type: PageType,
24   pub(crate) id: ObjectId<ApubPost>,
25   pub(crate) attributed_to: ObjectId<ApubPerson>,
26   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
27   pub(crate) to: Vec<Url>,
28   #[serde(default)]
29   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
30   pub(crate) cc: Vec<Url>,
31   pub(crate) name: String,
32   pub(crate) content: Option<String>,
33   pub(crate) media_type: Option<MediaTypeHtml>,
34   pub(crate) source: Option<Source>,
35   pub(crate) url: Option<Url>,
36   pub(crate) image: Option<ImageObject>,
37   pub(crate) comments_enabled: Option<bool>,
38   pub(crate) sensitive: Option<bool>,
39   pub(crate) stickied: Option<bool>,
40   pub(crate) published: Option<DateTime<FixedOffset>>,
41   pub(crate) updated: Option<DateTime<FixedOffset>>,
42   #[serde(flatten)]
43   pub(crate) unparsed: Unparsed,
44 }
45
46 impl Page {
47   /// Only mods can change the post's stickied/locked status. So if either of these is changed from
48   /// the current value, it is a mod action and needs to be verified as such.
49   ///
50   /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
51   pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
52     let old_post = ObjectId::<ApubPost>::new(self.id.clone())
53       .dereference_local(context)
54       .await;
55
56     let is_mod_action = if let Ok(old_post) = old_post {
57       self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked)
58     } else {
59       false
60     };
61     Ok(is_mod_action)
62   }
63
64   pub(crate) async fn extract_community(
65     &self,
66     context: &LemmyContext,
67     request_counter: &mut i32,
68   ) -> Result<ApubCommunity, LemmyError> {
69     let mut to_iter = self.to.iter();
70     loop {
71       if let Some(cid) = to_iter.next() {
72         let cid = ObjectId::new(cid.clone());
73         if let Ok(c) = cid
74           .dereference(context, context.client(), request_counter)
75           .await
76         {
77           break Ok(c);
78         }
79       } else {
80         return Err(LemmyError::from_message("No community found in cc"));
81       }
82     }
83   }
84 }
85
86 // Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
87 #[async_trait::async_trait(?Send)]
88 impl ActivityHandler for Page {
89   type DataType = LemmyContext;
90   async fn verify(
91     &self,
92     data: &Data<Self::DataType>,
93     request_counter: &mut i32,
94   ) -> Result<(), LemmyError> {
95     ApubPost::verify(self, self.id.inner(), data, request_counter).await
96   }
97   async fn receive(
98     self,
99     data: &Data<Self::DataType>,
100     request_counter: &mut i32,
101   ) -> Result<(), LemmyError> {
102     ApubPost::from_apub(self, data, request_counter).await?;
103     Ok(())
104   }
105 }