3 * @copyright Copyright (C) 2010-2023, 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\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Network\HTTPException;
30 use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
32 use Psr\Log\LoggerInterface;
34 class Account extends BaseFactory
38 /** @var ProfileFieldRepository */
39 private $profileFieldRepo;
41 private $mstdnFieldFactory;
43 public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileFieldRepository $profileFieldRepo, Field $mstdnFieldFactory)
45 parent::__construct($logger);
47 $this->baseUrl = $baseURL;
48 $this->profileFieldRepo = $profileFieldRepo;
49 $this->mstdnFieldFactory = $mstdnFieldFactory;
53 * @param int $contactId
54 * @param int $uid Public contact (=0) or owner user id
56 * @return \Friendica\Object\Api\Mastodon\Account
57 * @throws HTTPException\InternalServerErrorException
58 * @throws ImagickException|HTTPException\NotFoundException
60 public function createFromContactId(int $contactId, int $uid = 0): \Friendica\Object\Api\Mastodon\Account
62 $contact = Contact::getById($contactId, ['uri-id']);
64 if (empty($contact)) {
65 throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
67 if (empty($contact['uri-id'])) {
68 throw new HTTPException\NotFoundException('Contact ' . $contactId . ' has no uri-id set');
71 return self::createFromUriId($contact['uri-id'], $uid);
75 * @param int $contactUriId
76 * @param int $uid Public contact (=0) or owner user id
78 * @return \Friendica\Object\Api\Mastodon\Account
79 * @throws HTTPException\InternalServerErrorException
80 * @throws ImagickException|HTTPException\NotFoundException
82 public function createFromUriId(int $contactUriId, int $uid = 0): \Friendica\Object\Api\Mastodon\Account
84 $account = DBA::selectFirst('account-user-view', [], ['uri-id' => $contactUriId, 'uid' => [0, $uid]], ['order' => ['id' => true]]);
85 if (empty($account)) {
86 throw new HTTPException\NotFoundException('Contact ' . $contactUriId . ' not found');
89 $fields = new Fields();
91 if (Contact::isLocal($account['url'])) {
92 $self_contact = Contact::selectFirst(['uid'], ['nurl' => $account['nurl'], 'self' => true]);
93 if (!empty($self_contact['uid'])) {
94 $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
95 $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
99 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields);
104 * @return \Friendica\Object\Api\Mastodon\Account
105 * @throws ImagickException|HTTPException\InternalServerErrorException
107 public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
109 $account = DBA::selectFirst('account-user-view', [], ['uid' => $userId, 'self' => true]);
110 $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($userId);
111 $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
113 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields);