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