X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fapub%2Fsrc%2Fprotocol%2Fobjects%2Fpage.rs;h=f3308b0753078a03bec43d314e37887e2d507d1e;hb=92568956353f21649ed9aff68b42699c9d036f30;hp=7887f19c1b171b2287daec8978d42c2a542cedb7;hpb=5ff044346f1f94a7d37107b4ca081cf1fbd6eff8;p=lemmy.git diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 7887f19c..f3308b07 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -1,97 +1,254 @@ use crate::{ - activities::{verify_is_public, verify_person_in_community}, - fetcher::object_id::ObjectId, + activities::verify_community_matches, + fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity}, objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost}, - protocol::{ImageObject, Source}, + protocol::{objects::LanguageTag, ImageObject, InCommunity, Source}, +}; +use activitypub_federation::{ + config::Data, + fetch::object_id::ObjectId, + kinds::{ + link::LinkType, + object::{DocumentType, ImageType}, + }, + protocol::{ + helpers::{deserialize_one_or_many, deserialize_skip_error}, + values::MediaTypeMarkdownOrHtml, + }, + traits::{ActivityHandler, Object}, }; -use activitystreams::{object::kind::PageType, unparsed::Unparsed}; -use anyhow::anyhow; use chrono::{DateTime, FixedOffset}; -use lemmy_apub_lib::{values::MediaTypeHtml, verify::verify_domains_match}; -use lemmy_utils::{utils::check_slurs, LemmyError}; -use lemmy_websocket::LemmyContext; -use serde::{Deserialize, Serialize}; +use itertools::Itertools; +use lemmy_api_common::context::LemmyContext; +use lemmy_db_schema::newtypes::DbUrl; +use lemmy_utils::error::{LemmyError, LemmyErrorType}; +use serde::{de::Error, Deserialize, Deserializer, Serialize}; use serde_with::skip_serializing_none; use url::Url; +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] +pub enum PageType { + Page, + Article, + Note, + Video, + Event, +} + #[skip_serializing_none] #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Page { - pub(crate) r#type: PageType, - pub(crate) id: Url, - pub(crate) attributed_to: ObjectId, + #[serde(rename = "type")] + pub(crate) kind: PageType, + pub(crate) id: ObjectId, + pub(crate) attributed_to: AttributedTo, + #[serde(deserialize_with = "deserialize_one_or_many")] pub(crate) to: Vec, - pub(crate) name: String, + // If there is inReplyTo field this is actually a comment and must not be parsed + #[serde(deserialize_with = "deserialize_not_present", default)] + pub(crate) in_reply_to: Option, + + pub(crate) name: Option, + #[serde(deserialize_with = "deserialize_one_or_many", default)] + pub(crate) cc: Vec, pub(crate) content: Option, - pub(crate) media_type: Option, + pub(crate) media_type: Option, + #[serde(deserialize_with = "deserialize_skip_error", default)] pub(crate) source: Option, - pub(crate) url: Option, + /// most software uses array type for attachment field, so we do the same. nevertheless, we only + /// use the first item + #[serde(default)] + pub(crate) attachment: Vec, pub(crate) image: Option, pub(crate) comments_enabled: Option, pub(crate) sensitive: Option, - pub(crate) stickied: Option, pub(crate) published: Option>, pub(crate) updated: Option>, - #[serde(flatten)] - pub(crate) unparsed: Unparsed, + pub(crate) language: Option, + pub(crate) audience: Option>, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct Link { + pub(crate) href: Url, + pub(crate) r#type: LinkType, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct Image { + #[serde(rename = "type")] + pub(crate) kind: ImageType, + pub(crate) url: Url, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct Document { + #[serde(rename = "type")] + pub(crate) kind: DocumentType, + pub(crate) url: Url, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(untagged)] +pub(crate) enum Attachment { + Link(Link), + Image(Image), + Document(Document), +} + +impl Attachment { + pub(crate) fn url(self) -> Url { + match self { + // url as sent by Lemmy (new) + Attachment::Link(l) => l.href, + // image sent by lotide + Attachment::Image(i) => i.url, + // sent by mobilizon + Attachment::Document(d) => d.url, + } + } +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(untagged)] +pub(crate) enum AttributedTo { + Lemmy(ObjectId), + Peertube([AttributedToPeertube; 2]), +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct AttributedToPeertube { + #[serde(rename = "type")] + pub kind: PersonOrGroupType, + pub id: ObjectId, } impl Page { - pub(crate) fn id_unchecked(&self) -> &Url { - &self.id + /// Only mods can change the post's locked status. So if it is changed from the default value, + /// it is a mod action and needs to be verified as such. + /// + /// Locked needs to be false on a newly created post (verified in [[CreatePost]]. + pub(crate) async fn is_mod_action( + &self, + context: &Data, + ) -> Result { + let old_post = self.id.clone().dereference_local(context).await; + Ok(Page::is_locked_changed(&old_post, &self.comments_enabled)) } - pub(crate) fn id(&self, expected_domain: &Url) -> Result<&Url, LemmyError> { - verify_domains_match(&self.id, expected_domain)?; - Ok(&self.id) + + pub(crate) fn is_locked_changed( + old_post: &Result, + new_comments_enabled: &Option, + ) -> bool { + if let Some(new_comments_enabled) = new_comments_enabled { + if let Ok(old_post) = old_post { + return new_comments_enabled != &!old_post.locked; + } + } + + false } - /// Only mods can change the post's stickied/locked status. So if either of these is changed from - /// the current value, it is a mod action and needs to be verified as such. - /// - /// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]]. - pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result { - let old_post = ObjectId::::new(self.id.clone()) - .dereference_local(context) - .await; - - let is_mod_action = if let Ok(old_post) = old_post { - self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked) - } else { - false - }; - Ok(is_mod_action) + pub(crate) fn creator(&self) -> Result, LemmyError> { + match &self.attributed_to { + AttributedTo::Lemmy(l) => Ok(l.clone()), + AttributedTo::Peertube(p) => p + .iter() + .find(|a| a.kind == PersonOrGroupType::Person) + .map(|a| ObjectId::::from(a.id.clone().into_inner())) + .ok_or_else(|| LemmyErrorType::PageDoesNotSpecifyCreator.into()), + } } +} - pub(crate) async fn verify( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - let community = self.extract_community(context, request_counter).await?; - - check_slurs(&self.name, &context.settings().slur_regex())?; - verify_domains_match(self.attributed_to.inner(), &self.id.clone())?; - verify_person_in_community(&self.attributed_to, &community, context, request_counter).await?; - verify_is_public(&self.to.clone())?; +impl Attachment { + pub(crate) fn new(url: DbUrl) -> Attachment { + Attachment::Link(Link { + href: url.into(), + r#type: Default::default(), + }) + } +} + +// Used for community outbox, so that it can be compatible with Pleroma/Mastodon. +#[async_trait::async_trait] +impl ActivityHandler for Page { + type DataType = LemmyContext; + type Error = LemmyError; + fn id(&self) -> &Url { + unimplemented!() + } + fn actor(&self) -> &Url { + unimplemented!() + } + async fn verify(&self, data: &Data) -> Result<(), LemmyError> { + ApubPost::verify(self, self.id.inner(), data).await + } + async fn receive(self, data: &Data) -> Result<(), LemmyError> { + ApubPost::from_json(self, data).await?; Ok(()) } +} - pub(crate) async fn extract_community( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result { - let mut to_iter = self.to.iter(); - loop { - if let Some(cid) = to_iter.next() { - let cid = ObjectId::new(cid.clone()); - if let Ok(c) = cid.dereference(context, request_counter).await { - break Ok(c); +#[async_trait::async_trait] +impl InCommunity for Page { + async fn community(&self, context: &Data) -> Result { + let community = match &self.attributed_to { + AttributedTo::Lemmy(_) => { + let mut iter = self.to.iter().merge(self.cc.iter()); + loop { + if let Some(cid) = iter.next() { + let cid = ObjectId::from(cid.clone()); + if let Ok(c) = cid.dereference(context).await { + break c; + } + } else { + return Err(LemmyErrorType::NoCommunityFoundInCc)?; + } } - } else { - return Err(anyhow!("No community found in cc").into()); } + AttributedTo::Peertube(p) => { + p.iter() + .find(|a| a.kind == PersonOrGroupType::Group) + .map(|a| ObjectId::::from(a.id.clone().into_inner())) + .ok_or(LemmyErrorType::PageDoesNotSpecifyGroup)? + .dereference(context) + .await? + } + }; + if let Some(audience) = &self.audience { + verify_community_matches(audience, community.actor_id.clone())?; } + Ok(community) + } +} + +/// Only allows deserialization if the field is missing or null. If it is present, throws an error. +pub fn deserialize_not_present<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + let result: Option = Deserialize::deserialize(deserializer)?; + match result { + None => Ok(None), + Some(_) => Err(D::Error::custom("Post must not have inReplyTo property")), + } +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + #![allow(clippy::indexing_slicing)] + + use crate::protocol::{objects::page::Page, tests::test_parse_lemmy_item}; + + #[test] + fn test_not_parsing_note_as_page() { + assert!(test_parse_lemmy_item::("assets/lemmy/objects/note.json").is_err()); } }