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