acceptInterface = $acceptInterface; $this->addInterface = $addInterface; $this->announceInterface = $announceInterface; $this->blockInterface = $blockInterface; $this->createInterface = $createInterface; $this->deleteInterface = $deleteInterface; $this->documentInterface = $documentInterface; $this->followInterface = $followInterface; $this->imageInterface = $imageInterface; $this->likeInterface = $likeInterface; $this->noteInterface = $noteInterface; $this->notificationInterface = $notificationInterface; $this->personInterface = $personInterface; $this->serviceInterface = $serviceInterface; $this->groupInterface = $groupInterface; $this->organizationInterface = $organizationInterface; $this->applicationInterface = $applicationInterface; $this->rejectInterface = $rejectInterface; $this->removeInterface = $removeInterface; $this->undoInterface = $undoInterface; $this->updateInterface = $updateInterface; $this->configService = $configService; } public static function init() { try { AP::$activityPub = Server::get(AP::class); } catch (QueryException $e) { Server::get(LoggerInterface::class) ->error($e->getMessage(), ['exception' => $e]); } } /** * @throws RedundancyLimitException * @throws SocialAppConfigException * @throws ItemUnknownException */ public function getItemFromData(array $data, ?ACore $parent = null, int $level = 0): ACore { if (++$level > self::REDUNDANCY_LIMIT) { throw new RedundancyLimitException((string)$level); } $item = $this->getSimpleItemFromData($data); if ($parent !== null) { $item->setParent($parent); } $this->getObjectFromData($data, $item, $level); $this->getActorFromData($data, $item, $level); return $item; } /** * @throws RedundancyLimitException * @throws SocialAppConfigException */ public function getObjectFromData(array $data, ACore &$item, int $level) { try { $objectData = $this->getArray('object', $data, []); if (empty($objectData)) { $objectId = $this->get('object', $data, ''); if ($objectId !== '') { // TODO: validate AS_URL $item->setObjectId($objectId); } } else { $object = $this->getItemFromData($objectData, $item, $level); $item->setObject($object); } } catch (ItemUnknownException $e) { } } /** * @throws RedundancyLimitException * @throws SocialAppConfigException */ public function getActorFromData(array $data, ACore &$item, int $level) { try { $actorData = $this->getArray('actor_info', $data, []); if (!empty($actorData)) { /** @var Person $actor */ $actor = $this->getItemFromData($actorData, $item, $level); $item->setActor($actor); } } catch (ItemUnknownException $e) { } } /** * @throws SocialAppConfigException * @throws ItemUnknownException */ public function getSimpleItemFromData(array $data): Acore { $item = $this->getItemFromType($this->get('type', $data, '')); $item->import($data); $item->setSource(json_encode($data, JSON_UNESCAPED_SLASHES)); return $item; } /** * @param string $type * * @return ACore * @throws ItemUnknownException * @throws SocialAppConfigException */ public function getItemFromType(string $type): ACore { switch ($type) { case Accept::TYPE: $item = new Accept(); break; case Add::TYPE: $item = new Add(); break; case Announce::TYPE: $item = new Announce(); $item->setFilterDuplicate(true); break; case Block::TYPE: $item = new Block(); break; case Create::TYPE: $item = new Create(); break; case Delete::TYPE: $item = new Delete(); break; case Document::TYPE: $item = new Document(); break; case Follow::TYPE: $item = new Follow(); break; case Image::TYPE: $item = new Image(); break; case Like::TYPE: $item = new Like(); break; case Note::TYPE: $item = new Note(); break; case OrderedCollection::TYPE: $item = new OrderedCollection(); break; case SocialAppNotification::TYPE: $item = new SocialAppNotification(); break; case Stream::TYPE: $item = new Stream(); break; case Person::TYPE: $item = new Person(); break; case Reject::TYPE: $item = new Reject(); break; case Remove::TYPE: $item = new Remove(); break; case Service::TYPE: $item = new Service(); break; case Group::TYPE: $item = new Group(); break; case Organization::TYPE: $item = new Organization(); break; case Application::TYPE: $item = new Application(); break; case Tombstone::TYPE: $item = new Tombstone(); break; case Undo::TYPE: $item = new Undo(); break; case Update::TYPE: $item = new Update(); break; default: throw new ItemUnknownException(); } $item->setUrlCloud($this->configService->getCloudUrl()); return $item; } /** * @param ACore $activity * * @return IActivityPubInterface * @throws ItemUnknownException */ public function getInterfaceForItem(Acore $activity): IActivityPubInterface { return $this->getInterfaceFromType($activity->getType()); } /** * @param string $type * * @return IActivityPubInterface * @throws ItemUnknownException */ public function getInterfaceFromType(string $type): IActivityPubInterface { switch ($type) { case Accept::TYPE: return $this->acceptInterface; case Add::TYPE: return $this->addInterface; case Announce::TYPE: return $this->announceInterface; case Block::TYPE: return $this->blockInterface; case Create::TYPE: return $this->createInterface; case Delete::TYPE: return $this->deleteInterface; case Document::TYPE: return $this->documentInterface; case Follow::TYPE: return $this->followInterface; case Image::TYPE: return $this->imageInterface; case Like::TYPE: return $this->likeInterface; case Note::TYPE: return $this->noteInterface; case SocialAppNotification::TYPE: return $this->notificationInterface; case Person::TYPE: return $this->personInterface; case Reject::TYPE: return $this->rejectInterface; case Remove::TYPE: return $this->removeInterface; case Service::TYPE: return $this->serviceInterface; case Undo::TYPE: return $this->undoInterface; case Update::TYPE: return $this->updateInterface; default: throw new ItemUnknownException(); } } public function isActor(ACore $item): bool { $types = [ Person::TYPE, Service::TYPE, Group::TYPE, Organization::TYPE, Application::TYPE ]; return (in_array($item->getType(), $types)); } } AP::init();