From df49fcd1a756f2570651f82727c48ace66d9e8a3 Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 19 Jun 2020 22:32:52 -0500 Subject: [PATCH] Add checked actor method --- src/activity.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/activity.rs b/src/activity.rs index 414fb73..6f48cdd 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -23,7 +23,8 @@ //! # } //! ``` use crate::{ - base::{AnyBase, AsBase, Base, Extends}, + base::{AnyBase, AsBase, Base, BaseExt, Extends}, + error::DomainError, markers, object::{ApObject, AsObject, Object}, primitives::OneOrMany, @@ -445,7 +446,8 @@ pub trait ActivityExt: AsActivity { /// Documentation for the fields related to these methods can be found on the /// `ActorAndObject` struct pub trait ActorAndObjectRefExt: ActorAndObjectRef { - /// Fetch the actor for the current activity + /// Fetch the actor for the current activity, erroring if the actor's domain does not match the + /// ID's domain /// /// ```rust /// # use activitystreams_new::{context, activity::Create}; @@ -456,7 +458,34 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef { /// let actor_ref = create.actor(); /// println!("{:?}", actor_ref); /// ``` - fn actor(&self) -> &OneOrMany { + fn actor<'a, Kind>(&'a self) -> Result<&OneOrMany, DomainError> + where + Self: BaseExt, + Kind: 'a, + { + let unchecked = self.actor_unchecked(); + + if unchecked.as_single_id().and_then(|id| id.domain()) + != self.id_unchecked().and_then(|id| id.domain()) + { + return Err(DomainError); + } + + Ok(unchecked) + } + + /// Fetch the actor for the current activity + /// + /// ```rust + /// # use activitystreams_new::{context, activity::Create}; + /// # let mut create = Create::new(context(), context()); + /// # + /// use activitystreams_new::prelude::*; + /// + /// let actor_ref = create.actor_unchecked(); + /// println!("{:?}", actor_ref); + /// ``` + fn actor_unchecked(&self) -> &OneOrMany { self.actor_field_ref() } @@ -475,7 +504,7 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef { /// # } /// ``` fn actor_is(&self, id: &Url) -> bool { - self.actor().is_single_id(id) + self.actor_unchecked().is_single_id(id) } /// Set the actor for the current activity