From a72b6cbd54c9cd3c3763164a65926c80a94e573d Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 31 Jan 2019 10:51:11 -0500 Subject: [PATCH] Implement DeleteHandler --- src/Activities/DeleteHandler.php | 71 ++++++++++++++++++++++++++++++++ src/ActivityPub.php | 5 ++- src/Config/ActivityPubModule.php | 11 +++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/Activities/DeleteHandler.php diff --git a/src/Activities/DeleteHandler.php b/src/Activities/DeleteHandler.php new file mode 100644 index 0000000..b61ec08 --- /dev/null +++ b/src/Activities/DeleteHandler.php @@ -0,0 +1,71 @@ + 'handleDelete', + OutboxActivityEvent::NAME => 'handleDelete', + ); + } + + public function __construct( DateTimeProvider $dateTimeProvider, + ObjectsService $objectsService ) + { + $this->dateTimeProvider = $dateTimeProvider; + $this->objectsService = $objectsService; + } + + private function handleDelete( ActivityEvent $event ) + { + $activity = $event->getActivity(); + if ( $activity['type'] !== 'Delete' ) { + return; + } + $objectId = $activity['object']; + if ( ! is_string( $objectId ) ) { + if ( is_array( $objectId ) && array_key_exists( 'id', $objectId ) ) { + $objectId = $objectId['id']; + } else { + throw new BadRequestHttpException( 'Object must have an "id" field' ); + } + } + $tombstone = array( + '@context' => 'https://www.w3.org/ns/activitystreams', + 'type' => 'Tombstone', + 'deleted' => $this->getNowTimestamp(), + ); + $existing = $this->objectsService->dereference( $objectId ); + if ( $existing ) { + $tombstone['formerType'] = $existing['type']; + } + $this->objectsService->replace( $objectId, $tombstone ); + } + + private function getNowTimestamp() + { + return $this->dateTimeProvider->getTime( 'activities.delete' ) + ->format( DateTime::ISO8601 ); + } +} +?> diff --git a/src/ActivityPub.php b/src/ActivityPub.php index 02054b1..ee1dfbb 100644 --- a/src/ActivityPub.php +++ b/src/ActivityPub.php @@ -3,6 +3,7 @@ namespace ActivityPub; use ActivityPub\Activities\CreateHandler; use ActivityPub\Activities\NonActivityHandler; +use ActivityPub\Activities\UpdateHandler; use ActivityPub\Activities\ValidationHandler; use ActivityPub\Auth\AuthListener; use ActivityPub\Auth\SignatureListener; @@ -56,7 +57,7 @@ class ActivityPub $dispatcher->addSubscriber( $this->module->get( SignatureListener::class ) ); $dispatcher->addSubscriber( new ExceptionListener() ); - $this->subscribeActivityHandler( $dispatcher ); + $this->subscribeActivityHandlers( $dispatcher ); $controllerResolver = new ControllerResolver(); $argumentResolver = new ArgumentResolver(); @@ -93,6 +94,8 @@ class ActivityPub $dispatcher->addSubscriber( $this->module->get( NonActivityHandler::class ) ); $dispatcher->addSubscriber( $this->module->get( ValidationHandler::class ) ); $dispatcher->addSubscriber( $this->module->get( CreateHandler::class ) ); + $dispatcher->addSubscriber( $this->module->get( UpdateHandler::class ) ); + $dispatcher->addSubscriber( $this->module->get( DeleteHandler::class ) ); } } ?> diff --git a/src/Config/ActivityPubModule.php b/src/Config/ActivityPubModule.php index b886004..d2d709d 100644 --- a/src/Config/ActivityPubModule.php +++ b/src/Config/ActivityPubModule.php @@ -3,6 +3,8 @@ namespace ActivityPub\Config; use ActivityPub\Activities\CreateHandler; use ActivityPub\Activities\NonActivityHandler; +use ActivityPub\Activities\DeleteHandler; +use ActivityPub\Activities\UpdateHandler; use ActivityPub\Activities\ValidationHandler; use ActivityPub\Auth\AuthListener; use ActivityPub\Auth\AuthService; @@ -109,11 +111,20 @@ class ActivityPubModule ->addArgument( new Reference( PostController::class ) ); $this->injector->register( NonActivityHandler::class, NonActivityHandler::class ); + $this->injector->register( ValidationHandler::class, ValidationHandler::class ); + $this->injector->register( CreateHandler::class, CreateHandler::class ) ->addArgument( new Reference( ObjectsService::class ) ) ->addArgument( new Reference( IdProvider::class ) ) ->addArgument( new Reference( CollectionsService::class ) ); + + $this->injector->register( UpdateHandler::class, UpdateHandler::class ) + ->addArgument( new Reference( ObjectsService::class ) ); + + $this->injector->register( DeleteHandler::class, DeleteHandler::class ) + ->addArgument( new Reference( SimpleDateTimeProvider::class ) ) + ->addArgument( new Reference( ObjectsService::class ) ); } /**