Implement ActivityPersister

This commit is contained in:
Jeremy Dormitzer 2019-03-30 09:48:26 -04:00
parent c67ddcd760
commit 6729d98039
3 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,57 @@
<?php
namespace ActivityPub\ActivityEventHandlers;
use ActivityPub\Objects\CollectionsService;
use ActivityPub\Objects\ObjectsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ActivityPersister implements EventSubscriberInterface
{
/**
* @var CollectionsService
*/
private $collectionsService;
/**
* @var ObjectsService
*/
private $objectsService;
public static function getSubscribedEvents()
{
return array(
InboxActivityEvent::NAME => 'persistActivityToInbox',
OutboxActivityEvent::NAME => 'persistActivityToOutbox',
);
}
public function __construct( CollectionsService $collectionsService,
ObjectsService $objectsService )
{
$this->collectionsService = $collectionsService;
$this->objectsService = $objectsService;
}
public function persistActivityToInbox( InboxActivityEvent $event )
{
$activity = $event->getActivity();
$receivingActor = $event->getReceivingActor();
if ( $receivingActor->hasField( 'inbox' ) ) {
$this->collectionsService->addItem( $receivingActor['inbox'], $activity );
} else {
$this->objectsService->persist( $activity );
}
}
public function persistActivityToOutbox( OutboxActivityEvent $event )
{
$activity = $event->getActivity();
$receivingActor = $event->getReceivingActor();
if ( $receivingActor->hasField( 'outbox' ) ) {
$this->collectionsService->addItem( $receivingActor['outbox'], $activity );
} else {
$this->objectsService->persist( $activity );
}
}
}

View File

@ -5,6 +5,7 @@
namespace ActivityPub;
use ActivityPub\ActivityEventHandlers\AcceptHandler;
use ActivityPub\ActivityEventHandlers\ActivityPersister;
use ActivityPub\ActivityEventHandlers\AddHandler;
use ActivityPub\ActivityEventHandlers\AnnounceHandler;
use ActivityPub\ActivityEventHandlers\CreateHandler;
@ -100,6 +101,7 @@ class ActivityPub
$dispatcher->addSubscriber( $this->module->get( LikeHandler::class ) );
$dispatcher->addSubscriber( $this->module->get( AnnounceHandler::class ) );
$dispatcher->addSubscriber( $this->module->get( UndoHandler::class ) );
$dispatcher->addSubscriber( $this->module->get( ActivityPersister::class ) );
}
/**

View File

@ -5,6 +5,7 @@
namespace ActivityPub\Config;
use ActivityPub\ActivityEventHandlers\AcceptHandler;
use ActivityPub\ActivityEventHandlers\ActivityPersister;
use ActivityPub\ActivityEventHandlers\AddHandler;
use ActivityPub\ActivityEventHandlers\AnnounceHandler;
use ActivityPub\ActivityEventHandlers\CreateHandler;
@ -176,6 +177,10 @@ class ActivityPubModule
$this->injector->register( UndoHandler::class, UndoHandler::class )
->addArgument( new Reference( ObjectsService::class ) )
->addArgument( new Reference( CollectionsService::class ) );
$this->injector->register( ActivityPersister::class, ActivityPersister::class )
->addArgument( new Reference( CollectionsService::class ) )
->addArgument( new Reference( ObjectsService::class ) );
}
/**