Implement DeleteHandler

This commit is contained in:
Jeremy Dormitzer 2019-01-31 10:51:11 -05:00
parent 05221cf91f
commit a72b6cbd54
3 changed files with 86 additions and 1 deletions

View File

@ -0,0 +1,71 @@
<?php
namespace ActivityPub\Activities;
use ActivityPub\Activities\ActivityEvent;
use ActivityPub\Activities\InboxActivityEvent;
use ActivityPub\Activities\OutboxActivityEvent;
use ActivityPub\Utils\DateTimeProvider;
use DateTime;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class DeleteHandler implements EventSubscriberInterface
{
/**
* @var DateTimeProvider
*/
private $dateTimeProvider;
/**
* @var ObjectsService
*/
private $objectsService;
public static function getSubscribedEvents()
{
return array(
InboxActivityEvent::NAME => '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 );
}
}
?>

View File

@ -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 ) );
}
}
?>

View File

@ -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 ) );
}
/**