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