]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/InstanceV2.php
Merge pull request #13110 from anubis2814/develop
[friendica.git] / src / Module / Api / Mastodon / InstanceV2.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Api\Mastodon;
23
24 use Exception;
25 use Friendica\App;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\Core\L10n;
28 use Friendica\Core\System;
29 use Friendica\Database\Database;
30 use Friendica\DI;
31 use Friendica\Model\User;
32 use Friendica\Module\Api\ApiResponse;
33 use Friendica\Module\BaseApi;
34 use Friendica\Module\Register;
35 use Friendica\Object\Api\Mastodon\InstanceV2 as InstanceEntity;
36 use Friendica\Util\Images;
37 use Friendica\Util\Profiler;
38 use Friendica\Util\Strings;
39 use Psr\Log\LoggerInterface;
40
41 /**
42  * @see https://docs.joinmastodon.org/methods/instance/
43  */
44 class InstanceV2 extends BaseApi
45 {
46         /** @var Database */
47         private $database;
48
49         /** @var IManageConfigValues */
50         private $config;
51
52         public function __construct(
53                 App $app,
54                 L10n $l10n,
55                 App\BaseURL $baseUrl,
56                 App\Arguments $args,
57                 LoggerInterface $logger,
58                 Profiler $profiler,
59                 ApiResponse $response,
60                 Database $database,
61                 IManageConfigValues $config,
62                 array $server,
63                 array $parameters = []
64         ) {
65                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
66
67                 $this->database = $database;
68                 $this->config   = $config;
69         }
70
71         /**
72          * @param array $request
73          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
74          * @throws \Friendica\Network\HTTPException\NotFoundException
75          * @throws \ImagickException
76          * @throws Exception
77          */
78         protected function rawContent(array $request = [])
79         {
80                 $domain               = $this->baseUrl->getHost();
81                 $title                = $this->config->get('config', 'sitename');
82                 $version              = '2.8.0 (compatible; Friendica ' . App::VERSION . ')';
83                 $description          = $this->config->get('config', 'info');
84                 $usage                = $this->buildUsageInfo();
85                 $thumbnail            = new InstanceEntity\Thumbnail($this->baseUrl->withPath('images/friendica-banner.jpg'));
86                 $languages            = [$this->config->get('system', 'language')];
87                 $configuration        = $this->buildConfigurationInfo();
88                 $registration         = $this->buildRegistrationsInfo();
89                 $contact              = $this->buildContactInfo();
90                 $friendica_extensions = $this->buildFriendicaExtensionInfo();
91                 $rules                = System::getRules();
92                 System::jsonExit(new InstanceEntity(
93                         $domain,
94                         $title,
95                         $version,
96                         $description,
97                         $usage,
98                         $thumbnail,
99                         $languages,
100                         $configuration,
101                         $registration,
102                         $contact,
103                         $friendica_extensions,
104                         $rules
105                 ));
106         }
107
108         private function buildConfigurationInfo(): InstanceEntity\Configuration
109         {
110                 $statuses_config = new InstanceEntity\StatusesConfig((int)$this->config->get(
111                         'config',
112                         'api_import_size',
113                         $this->config->get('config', 'max_import_size')
114                 ));
115
116                 $image_size_limit = Strings::getBytesFromShorthand($this->config->get('system', 'maximagesize'));
117
118                 return new InstanceEntity\Configuration(
119                         $statuses_config,
120                         new InstanceEntity\MediaAttachmentsConfig(Images::supportedTypes(), $image_size_limit),
121                 );
122         }
123
124         private function buildContactInfo(): InstanceEntity\Contact
125         {
126                 $email         = implode(',', User::getAdminEmailList());
127                 $administrator = User::getFirstAdmin();
128                 $account       = null;
129
130                 if ($administrator) {
131                         $adminContact = $this->database->selectFirst(
132                                 'contact',
133                                 ['uri-id'],
134                                 ['nick' => $administrator['nickname'], 'self' => true]
135                         );
136                         $account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']);
137                 }
138
139                 return new InstanceEntity\Contact($email, $account);
140         }
141
142         private function buildFriendicaExtensionInfo(): InstanceEntity\FriendicaExtensions
143         {
144                 return new InstanceEntity\FriendicaExtensions(
145                         App::VERSION,
146                         App::CODENAME,
147                         $this->config->get('system', 'build')
148                 );
149         }
150
151         private function buildRegistrationsInfo(): InstanceEntity\Registrations
152         {
153                 $register_policy   = intval($this->config->get('config', 'register_policy'));
154                 $enabled           = ($register_policy != Register::CLOSED);
155                 $approval_required = ($register_policy == Register::APPROVE);
156
157                 return new InstanceEntity\Registrations($enabled, $approval_required);
158         }
159
160         private function buildUsageInfo(): InstanceEntity\Usage
161         {
162                 if (!empty($this->config->get('system', 'nodeinfo'))) {
163                         $active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly'));
164                 } else {
165                         $active_monthly = 0;
166                 }
167
168                 return new InstanceEntity\Usage(new InstanceEntity\UserStats($active_monthly));
169         }
170 }