From 3b5c4f948cbef2a37c434da59fc6cfa6cd084edd Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 24 Feb 2019 13:52:15 -0500 Subject: [PATCH] Implement Accept coming into the inbox --- src/Activities/AcceptHandler.php | 47 ++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/Activities/AcceptHandler.php b/src/Activities/AcceptHandler.php index 910e3f3..45bc154 100644 --- a/src/Activities/AcceptHandler.php +++ b/src/Activities/AcceptHandler.php @@ -3,8 +3,10 @@ namespace ActivityPub\Activities; use ActivityPub\Objects\CollectionsService; +use ActivityPub\Objects\ContextProvider; use ActivityPub\Objects\ObjectsService; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class AcceptHandler implements EventSubscriberInterface { @@ -18,11 +20,18 @@ class AcceptHandler implements EventSubscriberInterface */ private $collectionsService; + /** + * @var ContextProvider + */ + private $contextProvider; + public function __construct( ObjectsService $objectsService, - CollectionsService $collectionsService ) + CollectionsService $collectionsService, + ContextProvider $contextProvider ) { $this->objectsService = $objectsService; $this->collectionsService = $collectionsService; + $this->contextProvider = $contextProvider; } public static function getSubscribedEvents() @@ -39,7 +48,35 @@ class AcceptHandler implements EventSubscriberInterface if ( $activity['type'] !== 'Accept' ) { return; } - // add to following collection + $localActor = $event->getActor(); + $followId = $activity['object']; + if ( is_array( $followId ) && array_key_exists( 'id', $followId ) ) { + $followId = $followId['id']; + } + if ( ! is_string( $followId ) ) { + return; + } + $follow = $this->objectsService->dereference( $followId ); + if ( ! $follow ) { + return; + } + if ( ! $follow->hasField( 'object') || ! $follow['object'] == $localActor ) { + return; + } + if ( $localActor->hasField( 'following' ) ) { + $following = $localActor['following']; + } else { + $updatedLocalActor = $localActor->asArray(); + $updatedLocalActor['following'] = array( + '@context' => $this->contextProvider->getContext(), + 'id' => rtrim( $updatedLocalActor['id'], '/' ) . '/following', + 'type' => 'OrderedCollection', + 'orderedItems' => array(), + ); + $localActor = $this->objectsService->update( $localActor['id'], $updatedLocalActor ); + $following = $localActor['following']; + } + $this->collectionsService->addItem( $following, $activity['actor'] ); } public function handleOutbox( OutboxActivityEvent $event ) @@ -63,7 +100,11 @@ class AcceptHandler implements EventSubscriberInterface if ( !is_string( $followId ) ) { return; } - $follow = $this->objectsService->dereference( $followId )->asArray( -1 ); + $follow = $this->objectsService->dereference( $followId ); + if ( ! $follow ) { + return; + } + $follow = $follow->asArray( 0 ); } if ( !$follow || !array_key_exists( 'object', $follow ) ) { return;