]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Account.php
API: Speed improvements when fetching posts
[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\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Network\HTTPException;
30 use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
31 use ImagickException;
32 use Psr\Log\LoggerInterface;
33
34 class Account extends BaseFactory
35 {
36         /** @var BaseURL */
37         private $baseUrl;
38         /** @var ProfileFieldRepository */
39         private $profileFieldRepo;
40         /** @var Field */
41         private $mstdnFieldFactory;
42
43         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileFieldRepository $profileFieldRepo, Field $mstdnFieldFactory)
44         {
45                 parent::__construct($logger);
46
47                 $this->baseUrl           = $baseURL;
48                 $this->profileFieldRepo  = $profileFieldRepo;
49                 $this->mstdnFieldFactory = $mstdnFieldFactory;
50         }
51
52         /**
53          * @param int $contactId
54          * @param int $uid        Public contact (=0) or owner user id
55          *
56          * @return \Friendica\Object\Api\Mastodon\Account
57          * @throws HTTPException\InternalServerErrorException
58          * @throws ImagickException|HTTPException\NotFoundException
59          */
60         public function createFromContactId(int $contactId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
61         {
62                 $contact = Contact::getById($contactId, ['uri-id']);
63                 if (empty($contact)) {
64                         throw new HTTPException\NotFoundException('Contact ' . $contactId . ' not found');
65                 }
66                 return self::createFromUriId($contact['uri-id'], $uid);
67         }
68
69         /**
70          * @param int $contactUriId
71          * @param int $uid          Public contact (=0) or owner user id
72          *
73          * @return \Friendica\Object\Api\Mastodon\Account
74          * @throws HTTPException\InternalServerErrorException
75          * @throws ImagickException|HTTPException\NotFoundException
76          */
77         public function createFromUriId(int $contactUriId, $uid = 0): \Friendica\Object\Api\Mastodon\Account
78         {
79                 $contact = DBA::selectFirst('account-user-view', [], ['uri-id' => $contactUriId, 'uid' => [0, $uid]], ['order' => ['id' => true]]);
80                 if (empty($contact)) {
81                         throw new HTTPException\NotFoundException('Contact ' . $contactUriId . ' not found');
82                 }
83
84                 $fields = new Fields();
85
86                 if (Contact::isLocal($contact['url'])) {
87                         $self_contact = Contact::selectFirst(['uid'], ['nurl' => $contact['nurl'], 'self' => true]);
88                         if (!empty($self_contact['uid'])) {
89                                 $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']);
90                                 $fields        = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
91                         }
92                 }
93
94                 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $contact, $fields);
95         }
96
97         /**
98          * @param int $userId
99          * @return \Friendica\Object\Api\Mastodon\Account
100          * @throws ImagickException|HTTPException\InternalServerErrorException
101          */
102         public function createFromUserId(int $userId): \Friendica\Object\Api\Mastodon\Account
103         {
104                 $contact       = DBA::selectFirst('account-user-view', [], ['uid' => $userId, 'self' => true]);
105                 $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($userId);
106                 $fields        = $this->mstdnFieldFactory->createFromProfileFields($profileFields);
107
108                 return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $contact, $fields);
109         }
110 }