. * */ namespace Friendica\Factory\Api\Mastodon; use Friendica\App\BaseURL; use Friendica\BaseFactory; use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Model\APContact; use Friendica\Model\Contact; use Friendica\Network\HTTPException; use Friendica\Repository\PermissionSet; use Friendica\Repository\ProfileField; use ImagickException; use Psr\Log\LoggerInterface; class Account extends BaseFactory { /** @var BaseURL */ private $baseUrl; /** @var ProfileField */ private $profileFieldRepo; /** @var Field */ private $mstdnFieldFactory; public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileFieldRepo, Field $mstdnFieldFactory) { parent::__construct($logger); $this->baseUrl = $baseURL; $this->profileFieldRepo = $profileFieldRepo; $this->mstdnFieldFactory = $mstdnFieldFactory; } /** * @param int $contactId * @param int $uid Public contact (=0) or owner user id * * @return \Friendica\Object\Api\Mastodon\Account * @throws HTTPException\InternalServerErrorException * @throws ImagickException|HTTPException\NotFoundException */ public function createFromContactId(int $contactId, $uid = 0): \Friendica\Object\Api\Mastodon\Account { $cdata = Contact::getPublicAndUserContactID($contactId, $uid); if (!empty($cdata)) { $publicContact = Contact::getById($cdata['public']); $userContact = Contact::getById($cdata['user']); } else { $publicContact = Contact::getById($contactId); $userContact = []; } if (empty($publicContact)) { throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found'); } $apcontact = APContact::getByURL($publicContact['url'], false); $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]); if (!empty($self_contact['uid'])) { $profileFields = $this->profileFieldRepo->select(['uid' => $self_contact['uid'], 'psid' => PermissionSet::PUBLIC]); $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields); } else { $fields = new Fields(); } return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact); } /** * @param int $userId * @return \Friendica\Object\Api\Mastodon\Account * @throws ImagickException|HTTPException\InternalServerErrorException */ public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account { $publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]); $profileFields = $this->profileFieldRepo->select(['uid' => $userId, 'psid' => PermissionSet::PUBLIC]); $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields); $apContact = APContact::getByURL($publicContact['url'], false); return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apContact); } }