From f249237dc500d207e2eaf658603c1ccb88724823 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 6 Jun 2024 19:57:32 +0200 Subject: [PATCH] fix: better type check for normalizing AP document --- upub/core/src/traits/normalize.rs | 56 +++++++++++++++++++------------ 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/upub/core/src/traits/normalize.rs b/upub/core/src/traits/normalize.rs index c733048..afe4881 100644 --- a/upub/core/src/traits/normalize.rs +++ b/upub/core/src/traits/normalize.rs @@ -6,6 +6,9 @@ pub enum NormalizerError { #[error("normalized document misses required field: {0:?}")] Malformed(#[from] apb::FieldErr), + #[error("wrong object type: expected {0}, got {1}")] + WrongType(apb::BaseType, apb::BaseType), + #[error("database error while normalizing object: {0:?}")] DbErr(#[from] sea_orm::DbErr), } @@ -150,7 +153,11 @@ impl Normalizer for crate::Context { pub struct AP; impl AP { - pub fn activity(activity: &impl apb::Activity) -> Result { + pub fn activity(activity: &impl apb::Activity) -> Result { + let t = activity.base_type()?; + if !matches!(t, apb::BaseType::Object(apb::ObjectType::Activity(_))) { + return Err(NormalizerError::WrongType(apb::BaseType::Object(apb::ObjectType::Activity(apb::ActivityType::Activity)), t)); + } Ok(crate::model::activity::Model { internal: 0, id: activity.id()?.to_string(), @@ -166,7 +173,7 @@ impl AP { }) } - pub fn activity_q(activity: &impl apb::Activity) -> Result { + pub fn activity_q(activity: &impl apb::Activity) -> Result { let mut m = AP::activity(activity)?.into_active_model(); m.internal = NotSet; Ok(m) @@ -175,7 +182,11 @@ impl AP { - pub fn attachment(document: &impl apb::Document, parent: i64) -> Result { + pub fn attachment(document: &impl apb::Document, parent: i64) -> Result { + let t = document.base_type()?; + if !matches!(t, apb::BaseType::Object(apb::ObjectType::Document(_))) { + return Err(NormalizerError::WrongType(apb::BaseType::Object(apb::ObjectType::Document(apb::DocumentType::Document)), t)); + } Ok(crate::model::attachment::Model { internal: 0, url: document.url().id().str().unwrap_or_default(), @@ -187,7 +198,7 @@ impl AP { }) } - pub fn attachment_q(document: &impl apb::Document, parent: i64) -> Result { + pub fn attachment_q(document: &impl apb::Document, parent: i64) -> Result { let mut m = AP::attachment(document, parent)?.into_active_model(); m.internal = NotSet; Ok(m) @@ -195,26 +206,25 @@ impl AP { - pub fn object(object: &impl apb::Object) -> Result { - let t = object.object_type()?; - if matches!(t, - apb::ObjectType::Activity(_) - | apb::ObjectType::Actor(_) - | apb::ObjectType::Collection(_) - | apb::ObjectType::Document( - // TODO lemmy posts are PAGEs... - apb::DocumentType::Document - | apb::DocumentType::Audio - | apb::DocumentType::Image - | apb::DocumentType::Video + pub fn object(object: &impl apb::Object) -> Result { + let t = object.base_type()?; + if !matches!(t, + apb::BaseType::Object( + apb::ObjectType::Object + | apb::ObjectType::Note + | apb::ObjectType::Article + | apb::ObjectType::Event + | apb::ObjectType::Place + | apb::ObjectType::Profile + | apb::ObjectType::Document(apb::DocumentType::Page) // why Document lemmy?????? ) ) { - return Err(apb::FieldErr("type")); + return Err(NormalizerError::WrongType(apb::BaseType::Object(apb::ObjectType::Object), t)); } Ok(crate::model::object::Model { internal: 0, id: object.id()?.to_string(), - object_type: t, + object_type: object.object_type()?, attributed_to: object.attributed_to().id().str(), name: object.name().str(), summary: object.summary().str(), @@ -239,7 +249,7 @@ impl AP { }) } - pub fn object_q(object: &impl apb::Object) -> Result { + pub fn object_q(object: &impl apb::Object) -> Result { let mut m = AP::object(object)?.into_active_model(); m.internal = NotSet; Ok(m) @@ -247,7 +257,11 @@ impl AP { - pub fn actor(actor: &impl apb::Actor) -> Result { + pub fn actor(actor: &impl apb::Actor) -> Result { + let t = actor.base_type()?; + if !matches!(t, apb::BaseType::Object(apb::ObjectType::Actor(_))) { + return Err(NormalizerError::WrongType(apb::BaseType::Object(apb::ObjectType::Actor(apb::ActorType::Person)), t)); + } let ap_id = actor.id()?.to_string(); let (domain, fallback_preferred_username) = { let clean = ap_id @@ -283,7 +297,7 @@ impl AP { }) } - pub fn actor_q(actor: &impl apb::Actor) -> Result { + pub fn actor_q(actor: &impl apb::Actor) -> Result { let mut m = AP::actor(actor)?.into_active_model(); m.internal = NotSet; Ok(m)