From c5813c44415800dfb54a533b20a620d831989f1f Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 28 Jan 2019 21:25:24 -0500 Subject: [PATCH] [WIP] Begin implementing the Create handler --- src/Activities/CreateHandler.php | 108 +++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/Activities/CreateHandler.php diff --git a/src/Activities/CreateHandler.php b/src/Activities/CreateHandler.php new file mode 100644 index 0000000..5ee4184 --- /dev/null +++ b/src/Activities/CreateHandler.php @@ -0,0 +1,108 @@ + 'handleInbox', + OutboxActivityEvent::NAME => 'handleOutbox', + ); + } + + public function __construct( ObjectsService $objectsService, + IdProvider $idProvider ) + { + $this->objectsService = $objectsService; + $this->idProvider = $idProvider; + } + public function handleInbox( InboxActivityEvent $event ) + { + $activity = $event->getActivity(); + if ( $activity['type'] !== 'Create' ) { + return; + } + // normalize an incoming collection + // persist the object + } + + public function handleOutbox( OutboxActivityEvent $event ) + { + $activity = $event->getActivity(); + if ( $activity['type'] !== 'Create' ) { + return; + } + $object = $activity['object']; + if ( ! array_key_exists( 'id', $object ) ) { + $object['id'] = $this->idProvider->getId( + $event->getRequest(), + strtolower( $object['type'] ) + ); + } + $object['attributedTo'] = $this->getActorId( $activity ); + $object = $this->copyFields( + array( 'to', 'cc', 'audience' ), $activity, $object + ); + $activity = $this->copyFields( + array( 'to', 'bto', 'cc', 'bcc', 'audience' ), $object, $activity + ); + $activity['object'] = $object; + // normalize an incoming collection + // persist the object + } + + private function getActorId( array $activity ) + { + $actor = $activity['actor']; + if ( is_string( $actor ) ) { + return $actor; + } else { + return $actor['id']; + } + } + + private function copyFields( array $fields, array $sourceObj, array $targetObj ) + { + foreach( $fields as $field ) { + if ( ! array_key_exists( $field, $sourceObj ) ) { + continue; + } + if ( array_key_exists( $field, $targetObj ) && + $sourceObj[$field] === $targetObj[$field] ) { + continue; + } else if ( ! array_key_exists( $field, $targetObj ) ) { + $targetObj[$field] = $sourceObj[$field]; + } else if ( is_array( $sourceObj[$field] ) && + is_array( $targetObject[$field] ) ) { + $targetObj[$field] = array_unique( + array_merge( $sourceObj[$field], $targetObj[$field] ) + ); + } else if ( is_array( $sourceObj[$field] ) && + ! is_array( $targetObj[$field] ) ) { + $targetObj[$field] = array( $targetObj[$field] ); + $targetObj[$field] = array_unique( + array_merge( $sourceObj[$field], $targetObj[$field] ) + ); + } else if ( ! is_array( $sourceObj[$field] ) && + is_array( $targetObj[$field] ) ) { + $targetObj[$field][] = $sourceObj[$field]; + } else if ( ! is_array( $sourceObj[$field] ) && + ! is_array( $targetObj[$field] ) ) { + $targetObj[$field] = array( $targetObj[$field] ); + $targetObj[$field][] = $sourceObj[$field]; + } + } + return $targetObj; + } +} +?>