]> git.mxchange.org Git - friendica.git/blob - src/Object/Api/Mastodon/Instance.php
Merge pull request #10116 from mexon/mat/addon-console-command
[friendica.git] / src / Object / Api / Mastodon / Instance.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Object\Api\Mastodon;
23
24 use Friendica\BaseDataTransferObject;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\User;
28 use Friendica\Module\Register;
29
30 /**
31  * Class Instance
32  *
33  * @see https://docs.joinmastodon.org/api/entities/#instance
34  */
35 class Instance extends BaseDataTransferObject
36 {
37         /** @var string (URL) */
38         protected $uri;
39         /** @var string */
40         protected $title;
41         /** @var string */
42         protected $description;
43         /** @var string */
44         protected $email;
45         /** @var string */
46         protected $version;
47         /** @var array */
48         protected $urls;
49         /** @var Stats */
50         protected $stats;
51         /** @var string|null */
52         protected $thumbnail = null;
53         /** @var array */
54         protected $languages;
55         /** @var int */
56         protected $max_toot_chars;
57         /** @var bool */
58         protected $registrations;
59         /** @var bool */
60         protected $approval_required;
61         /** @var Account|null */
62         protected $contact_account = null;
63
64         /**
65          * Creates an instance record
66          *
67          * @return Instance
68          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
69          * @throws \ImagickException
70          */
71         public static function get()
72         {
73                 $register_policy = intval(DI::config()->get('config', 'register_policy'));
74
75                 $baseUrl = DI::baseUrl();
76
77                 $instance = new Instance();
78                 $instance->uri = $baseUrl->get();
79                 $instance->title = DI::config()->get('config', 'sitename');
80                 $instance->description = DI::config()->get('config', 'info');
81                 $instance->email = DI::config()->get('config', 'admin_email');
82                 $instance->version = FRIENDICA_VERSION;
83                 $instance->urls = []; // Not supported
84                 $instance->stats = Stats::get();
85                 $instance->thumbnail = $baseUrl->get() . (DI::config()->get('system', 'shortcut_icon') ?? 'images/friendica-32.png');
86                 $instance->languages = [DI::config()->get('system', 'language')];
87                 $instance->max_toot_chars = (int)DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size'));
88                 $instance->registrations = ($register_policy != Register::CLOSED);
89                 $instance->approval_required = ($register_policy == Register::APPROVE);
90                 $instance->contact_account = [];
91
92                 if (!empty(DI::config()->get('config', 'admin_email'))) {
93                         $adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
94                         $administrator = User::getByEmail($adminList[0], ['nickname']);
95                         if (!empty($administrator)) {
96                                 $adminContact = DBA::selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
97                                 $instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
98                         }
99                 }
100
101                 return $instance;
102         }
103 }