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