3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Factory\Api\Mastodon;
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Collection\Api\Mastodon\Fields;
27 use Friendica\Model\APContact;
28 use Friendica\Model\Contact;
29 use Friendica\Network\HTTPException;
30 use Friendica\Repository\PermissionSet;
31 use Friendica\Repository\ProfileField;
33 use Psr\Log\LoggerInterface;
35 class Account extends BaseFactory
39 /** @var ProfileField */
40 private $profileFieldRepo;
42 private $mstdnFieldFactory;
44 public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileFieldRepo, Field $mstdnFieldFactory)
46 parent::__construct($logger);
48 $this->baseUrl = $baseURL;
49 $this->profileFieldRepo = $profileFieldRepo;
50 $this->mstdnFieldFactory = $mstdnFieldFactory;
54 * @param int $contactId
55 * @param int $uid Public contact (=0) or owner user id
57 * @return \Friendica\Object\Api\Mastodon\Account
58 * @throws HTTPException\InternalServerErrorException
59 * @throws ImagickException|HTTPException\NotFoundException
61 public function createFromContactId(int $contactId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
63 $cdata = Contact::getPublicAndUserContactID($contactId, $uid);
65 $publicContact = Contact::getById($cdata['public']);
66 $userContact = Contact::getById($cdata['user']);
68 $publicContact = Contact::getById($contactId);
72 if (empty($publicContact)) {
73 throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
76 $apcontact = APContact::getByURL($publicContact['url'], false);
78 $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]);
79 if (!empty($self_contact['uid'])) {
80 $profileFields = $this->profileFieldRepo->select(['uid' => $self_contact['uid'], 'psid' => PermissionSet::PUBLIC]);
81 $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
83 $fields = new Fields();
86 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact);
91 * @return \Friendica\Object\Api\Mastodon\Account
92 * @throws ImagickException|HTTPException\InternalServerErrorException
94 public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
96 $publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]);
98 $profileFields = $this->profileFieldRepo->select(['uid' => $userId, 'psid' => PermissionSet::PUBLIC]);
99 $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
101 $apContact = APContact::getByURL($publicContact['url'], false);
103 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apContact);