diff --git a/src/ActivityEventHandlers/ActivityPersister.php b/src/ActivityEventHandlers/ActivityPersister.php new file mode 100644 index 0000000..8e67238 --- /dev/null +++ b/src/ActivityEventHandlers/ActivityPersister.php @@ -0,0 +1,57 @@ + '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 ); + } + } +} \ No newline at end of file diff --git a/src/ActivityPub.php b/src/ActivityPub.php index 874e64d..ba1414d 100644 --- a/src/ActivityPub.php +++ b/src/ActivityPub.php @@ -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 ) ); } /** diff --git a/src/Config/ActivityPubModule.php b/src/Config/ActivityPubModule.php index 8369391..160c0ad 100644 --- a/src/Config/ActivityPubModule.php +++ b/src/Config/ActivityPubModule.php @@ -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 ) ); } /**