Implement recipient resolution

This commit is contained in:
Jeremy Dormitzer 2019-04-11 20:45:45 -04:00
parent 97222e6749
commit 1bb7fef9cd

View File

@ -3,6 +3,7 @@
namespace ActivityPub\ActivityEventHandlers; namespace ActivityPub\ActivityEventHandlers;
use ActivityPub\Entities\ActivityPubObject; use ActivityPub\Entities\ActivityPubObject;
use ActivityPub\Objects\CollectionIterator;
use ActivityPub\Objects\ObjectsService; use ActivityPub\Objects\ObjectsService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -36,7 +37,6 @@ class DeliveryHandler implements EventSubscriberInterface
$activity = $event->getActivity(); $activity = $event->getActivity();
$recipientFields = array( 'to', 'bto', 'cc', 'bcc', 'audience' ); $recipientFields = array( 'to', 'bto', 'cc', 'bcc', 'audience' );
$inboxes = array(); $inboxes = array();
// TODO handle Links and Collections
foreach ( $recipientFields as $field ) { foreach ( $recipientFields as $field ) {
if ( array_key_exists( $field, $activity ) ) { if ( array_key_exists( $field, $activity ) ) {
$recipients = $activity[$field]; $recipients = $activity[$field];
@ -49,15 +49,7 @@ class DeliveryHandler implements EventSubscriberInterface
} }
if ( is_string( $recipient ) ) { if ( is_string( $recipient ) ) {
$recipientObj = $this->objectsService->dereference( $recipient ); $recipientObj = $this->objectsService->dereference( $recipient );
if ( $recipientObj && $recipientObj->hasField( 'inbox' ) ) { $inboxes = array_merge( $inboxes, $this->resolveRecipient( $recipientObj ) );
$inbox = $recipientObj['inbox'];
if ( $inbox instanceof ActivityPubObject && $inbox->hasField( 'id' ) ) {
$inbox = $inbox['id'];
}
if ( is_string( $inbox ) ) {
$inboxes[] = $inbox;
}
}
} }
} }
} }
@ -77,4 +69,35 @@ class DeliveryHandler implements EventSubscriberInterface
} }
// deliver activity to all inboxes, signing the request and not blocking // deliver activity to all inboxes, signing the request and not blocking
} }
/**
* Given an ActivityPubObject to deliver to, returns an array of inbox URLs
* @param ActivityPubObject $recipient
* @return array
*/
private function resolveRecipient( ActivityPubObject $recipient )
{
if ( $recipient && $recipient->hasField( 'inbox' ) ) {
$inbox = $recipient['inbox'];
if ( $inbox instanceof ActivityPubObject && $inbox->hasField( 'id' ) ) {
$inbox = $inbox['id'];
}
if ( is_string( $inbox ) ) {
return array( $inbox );
}
} else if (
$recipient &&
$recipient->hasField( 'type' ) &&
in_array( $recipient['type'], array( 'Collection', 'OrderedCollection' ) )
) {
$inboxes = array();
foreach ( CollectionIterator::iterateCollection( $recipient ) as $item ) {
if ( $item instanceof ActivityPubObject ) {
$inboxes = array_unique( array_merge( $inboxes, $this->resolveRecipient( $item ) ) );
}
}
return $inboxes;
}
return array();
}
} }