2 activities::{verify_is_public, verify_person_in_community},
3 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
4 protocol::{ImageObject, Source},
6 use activitystreams::{object::kind::PageType, unparsed::Unparsed};
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;
16 #[skip_serializing_none]
17 #[derive(Clone, Debug, Deserialize, Serialize)]
18 #[serde(rename_all = "camelCase")]
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>>,
36 pub(crate) unparsed: Unparsed,
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.
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)
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)
57 pub(crate) async fn verify(
59 context: &LemmyContext,
60 request_counter: &mut i32,
61 ) -> Result<(), LemmyError> {
62 let community = self.extract_community(context, request_counter).await?;
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())?;
71 pub(crate) async fn extract_community(
73 context: &LemmyContext,
74 request_counter: &mut i32,
75 ) -> Result<ApubCommunity, LemmyError> {
76 let mut to_iter = self.to.iter();
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 {
84 return Err(anyhow!("No community found in cc").into());