followsRequest = $followsRequest; $this->cacheActorService = $cacheActorService; $this->accountService = $accountService; $this->activityService = $activityService; $this->miscService = $miscService; } public function confirmFollowRequest(Follow $follow): void { try { $remoteActor = $this->cacheActorService->getFromId($follow->getActorId()); $accept = AP::$activityPub->getItemFromType(Accept::TYPE); $accept->generateUniqueId('#accept/follows'); $accept->setActorId($follow->getObjectId()); $accept->setObject($follow); // $follow->setParent($accept); $accept->addInstancePath( new InstancePath( $remoteActor->getInbox(), InstancePath::TYPE_INBOX, InstancePath::PRIORITY_TOP ) ); $this->activityService->request($accept); $this->followsRequest->accepted($follow); $actor = $this->cacheActorService->getFromId($follow->getObjectId()); $this->accountService->cacheLocalActorDetailCount($actor); $this->generateNotification($follow); } catch (Exception $e) { $this->miscService->log( 'exception while confirmFollowRequest: ' . get_class($e) . ' - ' . $e->getMessage(), 2 ); } } /** * This method is called when saving the Follow object * * @throws InvalidOriginException * @throws InvalidResourceException * @throws MalformedArrayException * @throws RedundancyLimitException * @throws SocialAppConfigException * @throws ItemUnknownException * @throws RequestContentException * @throws RequestNetworkException * @throws RequestResultSizeException * @throws RequestServerException * @throws RequestResultNotJsonException * @throws Exception */ public function processIncomingRequest(ACore $item): void { /** @var Follow $follow */ $follow = $item; $follow->checkOrigin($follow->getActorId()); try { $knownFollow = $this->followsRequest->getByPersons($follow->getActorId(), $follow->getObjectId()); if ($knownFollow->getId() === $follow->getId() && !$knownFollow->isAccepted()) { $this->confirmFollowRequest($follow); } } catch (FollowNotFoundException $e) { $actor = $this->cacheActorService->getFromId($follow->getObjectId()); if ($actor->isLocal()) { $follow->setFollowId($actor->getFollowers()); $this->followsRequest->save($follow); $this->confirmFollowRequest($follow); } } } /** * @param ACore $activity * @param ACore $item * * @throws InvalidOriginException */ public function activity(Acore $activity, ACore $item): void { /** @var Follow $item */ if ($activity->getType() === Undo::TYPE) { $activity->checkOrigin($item->getId()); $activity->checkOrigin($item->getActorId()); $this->followsRequest->delete($item); } if ($activity->getType() === Reject::TYPE) { $activity->checkOrigin($item->getObjectId()); $this->followsRequest->delete($item); } if ($activity->getType() === Accept::TYPE) { $activity->checkOrigin($item->getObjectId()); $this->followsRequest->accepted($item); } } /** * @throws SocialAppConfigException|ItemAlreadyExistsException|ItemUnknownException */ private function generateNotification(Follow $follow): void { /** @var SocialAppNotificationInterface $notificationInterface */ $notificationInterface = AP::$activityPub->getInterfaceFromType(SocialAppNotification::TYPE); try { $follower = $this->cacheActorService->getFromId($follow->getActorId()); } catch (Exception $e) { return; } /** @var SocialAppNotification $notification */ $notification = AP::$activityPub->getItemFromType(SocialAppNotification::TYPE); $notification->setDetail('url', $follower->getId()); $notification->setDetail('account', $follower->getAccount()); $notification->setDetailItem('actor', $follower); $notification->setAttributedTo($follow->getActorId()) ->setId($follow->getId() . '/notification') ->setSubType(Follow::TYPE) ->setActorId($follower->getId()) ->setSummary('{account} is following you') ->setTo($follow->getObjectId()) ->setLocal(true); $notificationInterface->save($notification); } }