Implement AddHandler

This commit is contained in:
Jeremy Dormitzer 2019-03-02 17:23:34 -05:00
parent 6d4e3391f4
commit 15dd5d3e9f
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?php
namespace ActivityPub\Activities;
use ActivityPub\Objects\CollectionsService;
use ActivityPub\Objects\ObjectsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class AddHandler implements EventSubscriberInterface
{
/**
* @var ObjectsService
*/
private $objectsService;
/**
* @var CollectionsService
*/
private $collectionsService;
public static function getSubscribedEvents()
{
return array(
InboxActivityEvent::NAME => 'handleAdd',
OutboxActivityEvent::NAME => 'handleAdd',
);
}
public function __construct( ObjectsService $objectsService,
CollectionsService $collectionsService )
{
$this->objectsService = $objectsService;
$this->collectionsService = $collectionsService;
}
public function handleAdd( ActivityEvent $event )
{
$activity = $event->getActivity();
if ( $activity['type'] !== 'Add' ) {
return;
}
$collectionId = $activity['target'];
if ( is_array( $collectionId ) && array_key_exists( 'id', $collectionId ) ) {
$collectionId = $collectionId['id'];
}
$collection = $this->objectsService->dereference( $collectionId );
$requestActor = $event->getRequest()->attributes->get( 'actor' );
$requestActorHost = parse_url( $requestActor['id'], PHP_URL_HOST );
$collectionHost = parse_url( $collection['id'], PHP_URL_HOST );
if ( $requestActorHost !== $collectionHost ) {
throw new AccessDeniedHttpException();
}
$object = $activity['object'];
$this->collectionsService->addItem( $collection, $object );
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace ActivityPub\Test\Activities;
use ActivityPub\Test\TestConfig\APTestCase;
class AddHandlerTest extends APTestCase
{
public function testHandleAdd()
{
// TODO implement me
$this->assertTrue( false );
}
}