]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Account.php
Use Args::getMethod() at various places
[friendica.git] / src / Factory / Api / Mastodon / Account.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Factory\Api\Mastodon;
23
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\Model\FContact;
30 use Friendica\Network\HTTPException;
31 use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
32 use ImagickException;
33 use Psr\Log\LoggerInterface;
34
35 class Account extends BaseFactory
36 {
37         /** @var BaseURL */
38         private $baseUrl;
39         /** @var ProfileFieldRepository */
40         private $profileFieldRepo;
41         /** @var Field */
42         private $mstdnFieldFactory;
43
44         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileFieldRepository $profileFieldRepo, Field $mstdnFieldFactory)
45         {
46                 parent::__construct($logger);
47
48                 $this->baseUrl           = $baseURL;
49                 $this->profileFieldRepo  = $profileFieldRepo;
50                 $this->mstdnFieldFactory = $mstdnFieldFactory;
51         }
52
53         /**
54          * @param int $contactId
55          * @param int $uid        Public contact (=0) or owner user id
56          *
57          * @return \Friendica\Object\Api\Mastodon\Account
58          * @throws HTTPException\InternalServerErrorException
59          * @throws ImagickException|HTTPException\NotFoundException
60          */
61         public function createFromContactId(int $contactId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
62         {
63                 $cdata = Contact::getPublicAndUserContactID($contactId, $uid);
64                 if (!empty($cdata)) {
65                         $publicContact = Contact::getById($cdata['public']);
66                         $userContact   = Contact::getById($cdata['user']);
67                 } else {
68                         $publicContact = Contact::getById($contactId);
69                         $userContact   = [];
70                 }
71
72                 if (empty($publicContact)) {
73                         throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
74                 }
75
76                 $apcontact = APContact::getByURL($publicContact['url'], false);
77                 $fcontact  = FContact::getByURL($publicContact['url'], false);
78
79                 $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]);
80                 if (!empty($self_contact['uid'])) {
81                         $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
82                         $fields        = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
83                 } else {
84                         $fields = new Fields();
85                 }
86
87                 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact, $fcontact);
88         }
89
90         /**
91          * @param int $userId
92          * @return \Friendica\Object\Api\Mastodon\Account
93          * @throws ImagickException|HTTPException\InternalServerErrorException
94          */
95         public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
96         {
97                 $publicContact = Contact::selectFirst([], ['uid' => $userId, 'self' => true]);
98
99                 $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($userId);
100                 $fields        = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
101
102                 $apContact = APContact::getByURL($publicContact['url'], false);
103
104                 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apContact);
105         }
106 }