Implement ActivityPersister
This commit is contained in:
parent
c67ddcd760
commit
6729d98039
57
src/ActivityEventHandlers/ActivityPersister.php
Normal file
57
src/ActivityEventHandlers/ActivityPersister.php
Normal 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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@
|
|||||||
namespace ActivityPub;
|
namespace ActivityPub;
|
||||||
|
|
||||||
use ActivityPub\ActivityEventHandlers\AcceptHandler;
|
use ActivityPub\ActivityEventHandlers\AcceptHandler;
|
||||||
|
use ActivityPub\ActivityEventHandlers\ActivityPersister;
|
||||||
use ActivityPub\ActivityEventHandlers\AddHandler;
|
use ActivityPub\ActivityEventHandlers\AddHandler;
|
||||||
use ActivityPub\ActivityEventHandlers\AnnounceHandler;
|
use ActivityPub\ActivityEventHandlers\AnnounceHandler;
|
||||||
use ActivityPub\ActivityEventHandlers\CreateHandler;
|
use ActivityPub\ActivityEventHandlers\CreateHandler;
|
||||||
@ -100,6 +101,7 @@ class ActivityPub
|
|||||||
$dispatcher->addSubscriber( $this->module->get( LikeHandler::class ) );
|
$dispatcher->addSubscriber( $this->module->get( LikeHandler::class ) );
|
||||||
$dispatcher->addSubscriber( $this->module->get( AnnounceHandler::class ) );
|
$dispatcher->addSubscriber( $this->module->get( AnnounceHandler::class ) );
|
||||||
$dispatcher->addSubscriber( $this->module->get( UndoHandler::class ) );
|
$dispatcher->addSubscriber( $this->module->get( UndoHandler::class ) );
|
||||||
|
$dispatcher->addSubscriber( $this->module->get( ActivityPersister::class ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
namespace ActivityPub\Config;
|
namespace ActivityPub\Config;
|
||||||
|
|
||||||
use ActivityPub\ActivityEventHandlers\AcceptHandler;
|
use ActivityPub\ActivityEventHandlers\AcceptHandler;
|
||||||
|
use ActivityPub\ActivityEventHandlers\ActivityPersister;
|
||||||
use ActivityPub\ActivityEventHandlers\AddHandler;
|
use ActivityPub\ActivityEventHandlers\AddHandler;
|
||||||
use ActivityPub\ActivityEventHandlers\AnnounceHandler;
|
use ActivityPub\ActivityEventHandlers\AnnounceHandler;
|
||||||
use ActivityPub\ActivityEventHandlers\CreateHandler;
|
use ActivityPub\ActivityEventHandlers\CreateHandler;
|
||||||
@ -176,6 +177,10 @@ class ActivityPubModule
|
|||||||
$this->injector->register( UndoHandler::class, UndoHandler::class )
|
$this->injector->register( UndoHandler::class, UndoHandler::class )
|
||||||
->addArgument( new Reference( ObjectsService::class ) )
|
->addArgument( new Reference( ObjectsService::class ) )
|
||||||
->addArgument( new Reference( CollectionsService::class ) );
|
->addArgument( new Reference( CollectionsService::class ) );
|
||||||
|
|
||||||
|
$this->injector->register( ActivityPersister::class, ActivityPersister::class )
|
||||||
|
->addArgument( new Reference( CollectionsService::class ) )
|
||||||
|
->addArgument( new Reference( ObjectsService::class ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user