From 8101739eddd192314a33abda8a729515ece33b42 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Sun, 19 Feb 2023 17:32:13 -0500 Subject: [PATCH 1/7] Change Mastodon API documentation URL for V1 Instance type --- src/Object/Api/Mastodon/Instance.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index 14914cd7fc..007081e914 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -34,7 +34,7 @@ use Friendica\Network\HTTPException; /** * Class Instance * - * @see https://docs.joinmastodon.org/api/entities/#instance + * @see https://docs.joinmastodon.org/entities/V1_Instance/ */ class Instance extends BaseDataTransferObject { From 0451f2dfa1510258b7f551ba3224e885c711e6b3 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Feb 2023 07:51:20 -0500 Subject: [PATCH 2/7] Add Mastodon Instance V2 API endpoint /api/v2/instance --- src/Module/Api/Mastodon/InstanceV2.php | 75 +++++++++++++++ src/Object/Api/Mastodon/InstanceV2.php | 96 +++++++++++++++++++ .../Api/Mastodon/InstanceV2/Configuration.php | 47 +++++++++ .../Api/Mastodon/InstanceV2/Contact.php | 56 +++++++++++ .../InstanceV2/FriendicaExtensions.php | 31 ++++++ .../InstanceV2/MediaAttachmentsConfig.php | 40 ++++++++ .../Api/Mastodon/InstanceV2/Registrations.php | 47 +++++++++ .../Mastodon/InstanceV2/StatusesConfig.php | 46 +++++++++ .../Api/Mastodon/InstanceV2/Thumbnail.php | 41 ++++++++ src/Object/Api/Mastodon/InstanceV2/Usage.php | 40 ++++++++ .../Api/Mastodon/InstanceV2/UserStats.php | 44 +++++++++ static/routes.config.php | 2 +- 12 files changed, 564 insertions(+), 1 deletion(-) create mode 100644 src/Module/Api/Mastodon/InstanceV2.php create mode 100644 src/Object/Api/Mastodon/InstanceV2.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/Configuration.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/Contact.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/Registrations.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/Thumbnail.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/Usage.php create mode 100644 src/Object/Api/Mastodon/InstanceV2/UserStats.php diff --git a/src/Module/Api/Mastodon/InstanceV2.php b/src/Module/Api/Mastodon/InstanceV2.php new file mode 100644 index 0000000000..e5af34b67a --- /dev/null +++ b/src/Module/Api/Mastodon/InstanceV2.php @@ -0,0 +1,75 @@ +. + * + */ + +namespace Friendica\Module\Api\Mastodon; + +use Friendica\App; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\L10n; +use Friendica\Core\System; +use Friendica\Database\Database; +use Friendica\Module\Api\ApiResponse; +use Friendica\Module\BaseApi; +use Friendica\Object\Api\Mastodon\InstanceV2 as InstanceEntity; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * @see https://docs.joinmastodon.org/methods/instance/ + */ +class InstanceV2 extends BaseApi +{ + /** @var Database */ + private $database; + + /** @var IManageConfigValues */ + private $config; + + public function __construct( + App $app, + L10n $l10n, + App\BaseURL $baseUrl, + App\Arguments $args, + LoggerInterface $logger, + Profiler $profiler, + ApiResponse $response, + Database $database, + IManageConfigValues $config, + array $server, + array $parameters = [] + ) { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->database = $database; + $this->config = $config; + } + + /** + * @param array $request + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \Friendica\Network\HTTPException\NotFoundException + * @throws \ImagickException + */ + protected function rawContent(array $request = []) + { + System::jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database, System::getRules())); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2.php b/src/Object/Api/Mastodon/InstanceV2.php new file mode 100644 index 0000000000..f3551c5646 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2.php @@ -0,0 +1,96 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon; + +use Friendica\App; +use Friendica\App\BaseURL; +use Friendica\BaseDataTransferObject; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Database\Database; +use Friendica\Network\HTTPException; +use Friendica\Object\Api\Mastodon\InstanceV2\Configuration; +use Friendica\Object\Api\Mastodon\InstanceV2\Contact; +use Friendica\Object\Api\Mastodon\InstanceV2\FriendicaExtensions; +use Friendica\Object\Api\Mastodon\InstanceV2\Registrations; +use Friendica\Object\Api\Mastodon\InstanceV2\Thumbnail; +use Friendica\Object\Api\Mastodon\InstanceV2\Usage; + +/** + * Class Instance + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class InstanceV2 extends BaseDataTransferObject +{ + /** @var string */ + protected $domain; + /** @var string */ + protected $title; + /** @var string */ + protected $version; + /** @var string */ + protected $source_url; + /** @var string */ + protected $description; + /** @var Usage */ + protected $usage; + /** @var Thumbnail */ + protected $thumbnail; + /** @var array */ + protected $languages; + /** @var Configuration */ + protected $configuration; + /** @var Registrations */ + protected $registrations; + /** @var Contact */ + protected $contact; + /** @var array */ + protected $rules = []; + /** @var FriendicaExtensions */ + protected $friendica; + + /** + * @param IManageConfigValues $config + * @param BaseURL $baseUrl + * @param Database $database + * @param array $rules + * @throws HTTPException\InternalServerErrorException + * @throws HTTPException\NotFoundException + * @throws \ImagickException + */ + public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, array $rules = []) + { + $this->domain = $baseUrl->getHostname(); + $this->title = $config->get('config', 'sitename'); + $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; + $this->source_url = null; //not supported yet + $this->description = $config->get('config', 'info'); + $this->usage = new Usage($config); + $this->thumbnail = new Thumbnail($baseUrl); + $this->languages = [$config->get('system', 'language')]; + $this->configuration = new Configuration(); + $this->registrations = new Registrations(); + $this->contact = new Contact($database); + $this->rules = $rules; + $this->friendica = new FriendicaExtensions(); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/Configuration.php b/src/Object/Api/Mastodon/InstanceV2/Configuration.php new file mode 100644 index 0000000000..e30561356b --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/Configuration.php @@ -0,0 +1,47 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; +use Friendica\DI; + +/** + * Class Configuration + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class Configuration extends BaseDataTransferObject +{ + /** @var StatusesConfig */ + protected $statuses; + /** @var MediaAttachmentsConfig */ + protected $media_attachments; + /** @var int */ + protected $image_size_limit; + + public function __construct() + { + $this->statuses = new StatusesConfig(); + $this->media_attachments = new MediaAttachmentsConfig(); + $this->image_size_limit = DI::config()->get('system', 'maximagesize'); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/Contact.php b/src/Object/Api/Mastodon/InstanceV2/Contact.php new file mode 100644 index 0000000000..45631b0fb5 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/Contact.php @@ -0,0 +1,56 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; +use Friendica\Database\Database; +use Friendica\DI; +use Friendica\Model\User; +use Friendica\Object\Api\Mastodon\Account; + +/** + * Class Contact + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class Contact extends BaseDataTransferObject +{ + /** @var string */ + protected $email; + /** @var Account */ + protected $account = null; + + + public function __construct(Database $database) + { + $this->email = implode(',', User::getAdminEmailList()); + $administrator = User::getFirstAdmin(); + if ($administrator) { + $adminContact = $database->selectFirst( + 'contact', + ['uri-id'], + ['nick' => $administrator['nickname'], 'self' => true] + ); + $this->account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']); + } + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php new file mode 100644 index 0000000000..b69b5c0267 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php @@ -0,0 +1,31 @@ +version = App::VERSION; + $this->codename = App::CODENAME; + $this->db_version = DI::config()->get('system', 'build'); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php b/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php new file mode 100644 index 0000000000..598736c330 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php @@ -0,0 +1,40 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; +use Friendica\Util\Images; + +/** + * Class MediaAttachmentsConfig + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class MediaAttachmentsConfig extends BaseDataTransferObject +{ + /** @var string[] */ + protected $supported_mime_types; + public function __construct() + { + $this->supported_mime_types = Images::supportedTypes(); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/Registrations.php b/src/Object/Api/Mastodon/InstanceV2/Registrations.php new file mode 100644 index 0000000000..78c5713ea3 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/Registrations.php @@ -0,0 +1,47 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; +use Friendica\DI; +use Friendica\Module\Register; + +/** + * Class Registrations + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class Registrations extends BaseDataTransferObject +{ + /** @var bool */ + protected $enabled; + /** @var bool */ + protected $approval_required; + + public function __construct() + { + $config = DI::config(); + $register_policy = intval($config->get('config', 'register_policy')); + $this->enabled = ($register_policy != Register::CLOSED); + $this->approval_required = ($register_policy == Register::APPROVE); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php new file mode 100644 index 0000000000..5bf20ec974 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php @@ -0,0 +1,46 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; +use Friendica\DI; + +/** + * Class StatusConfig + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class StatusesConfig extends BaseDataTransferObject +{ + /** @var int */ + protected $max_characters = 0; + + public function __construct() + { + $config = DI::config(); + $this->max_characters = (int)$config->get( + 'config', + 'api_import_size', + $config->get('config', 'max_import_size') + ); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php b/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php new file mode 100644 index 0000000000..18cb1bf2a7 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php @@ -0,0 +1,41 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\App\BaseURL; +use Friendica\BaseDataTransferObject; + +/** + * Class Thumbnail + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class Thumbnail extends BaseDataTransferObject +{ + /** @var string (URL) */ + protected $url; + + public function __construct(BaseURL $baseUrl) + { + $this->url = $baseUrl->get() . 'images/friendica-banner.jpg'; + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/Usage.php b/src/Object/Api/Mastodon/InstanceV2/Usage.php new file mode 100644 index 0000000000..cb49348337 --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/Usage.php @@ -0,0 +1,40 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; + +/** + * Class Usage + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class Usage extends BaseDataTransferObject +{ + /** @var UserStats */ + protected $users; + + public function __construct() + { + $this->users = new UserStats(); + } +} diff --git a/src/Object/Api/Mastodon/InstanceV2/UserStats.php b/src/Object/Api/Mastodon/InstanceV2/UserStats.php new file mode 100644 index 0000000000..c82cf563ee --- /dev/null +++ b/src/Object/Api/Mastodon/InstanceV2/UserStats.php @@ -0,0 +1,44 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\InstanceV2; + +use Friendica\BaseDataTransferObject; +use Friendica\DI; + +/** + * Class UserStats + * + * @see https://docs.joinmastodon.org/entities/Instance/ + */ +class UserStats extends BaseDataTransferObject +{ + /** @var int */ + protected $active_monthly = 0; + + public function __construct() + { + $config = DI::config(); + if (!empty($config->get('system', 'nodeinfo'))) { + $this->active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly')); + } + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 58fce72777..3271571c44 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -307,7 +307,7 @@ return [ '/trends/tags' => [Module\Api\Mastodon\Trends\Tags::class, [R::GET ]], ], '/v2' => [ - '/instance' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not supported + '/instance' => [Module\Api\Mastodon\InstanceV2::class, [R::GET ]], // not supported ], '/v{version:\d+}' => [ '/admin/accounts' => [Module\Api\Mastodon\Unimplemented::class, [R::GET ]], // not supported From fcf59dc245c07c4a7b74ef569d602b5b62010162 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Feb 2023 08:05:01 -0500 Subject: [PATCH 3/7] Fix binary_operator_spaces errors --- src/Module/Api/Mastodon/InstanceV2.php | 2 +- src/Object/Api/Mastodon/InstanceV2.php | 26 +++++++++---------- .../Api/Mastodon/InstanceV2/Contact.php | 2 +- .../InstanceV2/FriendicaExtensions.php | 4 +-- .../Api/Mastodon/InstanceV2/Registrations.php | 6 ++--- .../Mastodon/InstanceV2/StatusesConfig.php | 2 +- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Module/Api/Mastodon/InstanceV2.php b/src/Module/Api/Mastodon/InstanceV2.php index e5af34b67a..3177570c94 100644 --- a/src/Module/Api/Mastodon/InstanceV2.php +++ b/src/Module/Api/Mastodon/InstanceV2.php @@ -59,7 +59,7 @@ class InstanceV2 extends BaseApi parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); $this->database = $database; - $this->config = $config; + $this->config = $config; } /** diff --git a/src/Object/Api/Mastodon/InstanceV2.php b/src/Object/Api/Mastodon/InstanceV2.php index f3551c5646..20ea45ba3c 100644 --- a/src/Object/Api/Mastodon/InstanceV2.php +++ b/src/Object/Api/Mastodon/InstanceV2.php @@ -79,18 +79,18 @@ class InstanceV2 extends BaseDataTransferObject */ public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, array $rules = []) { - $this->domain = $baseUrl->getHostname(); - $this->title = $config->get('config', 'sitename'); - $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; - $this->source_url = null; //not supported yet - $this->description = $config->get('config', 'info'); - $this->usage = new Usage($config); - $this->thumbnail = new Thumbnail($baseUrl); - $this->languages = [$config->get('system', 'language')]; - $this->configuration = new Configuration(); - $this->registrations = new Registrations(); - $this->contact = new Contact($database); - $this->rules = $rules; - $this->friendica = new FriendicaExtensions(); + $this->domain = $baseUrl->getHostname(); + $this->title = $config->get('config', 'sitename'); + $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; + $this->source_url = null; //not supported yet + $this->description = $config->get('config', 'info'); + $this->usage = new Usage($config); + $this->thumbnail = new Thumbnail($baseUrl); + $this->languages = [$config->get('system', 'language')]; + $this->configuration = new Configuration(); + $this->registrations = new Registrations(); + $this->contact = new Contact($database); + $this->rules = $rules; + $this->friendica = new FriendicaExtensions(); } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Contact.php b/src/Object/Api/Mastodon/InstanceV2/Contact.php index 45631b0fb5..007c0495bc 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Contact.php +++ b/src/Object/Api/Mastodon/InstanceV2/Contact.php @@ -42,7 +42,7 @@ class Contact extends BaseDataTransferObject public function __construct(Database $database) { - $this->email = implode(',', User::getAdminEmailList()); + $this->email = implode(',', User::getAdminEmailList()); $administrator = User::getFirstAdmin(); if ($administrator) { $adminContact = $database->selectFirst( diff --git a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php index b69b5c0267..2805e48665 100644 --- a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php +++ b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php @@ -24,8 +24,8 @@ class FriendicaExtensions extends BaseDataTransferObject public function __construct() { - $this->version = App::VERSION; - $this->codename = App::CODENAME; + $this->version = App::VERSION; + $this->codename = App::CODENAME; $this->db_version = DI::config()->get('system', 'build'); } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Registrations.php b/src/Object/Api/Mastodon/InstanceV2/Registrations.php index 78c5713ea3..c1e7769e83 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Registrations.php +++ b/src/Object/Api/Mastodon/InstanceV2/Registrations.php @@ -39,9 +39,9 @@ class Registrations extends BaseDataTransferObject public function __construct() { - $config = DI::config(); - $register_policy = intval($config->get('config', 'register_policy')); - $this->enabled = ($register_policy != Register::CLOSED); + $config = DI::config(); + $register_policy = intval($config->get('config', 'register_policy')); + $this->enabled = ($register_policy != Register::CLOSED); $this->approval_required = ($register_policy == Register::APPROVE); } } diff --git a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php index 5bf20ec974..fb74b1d5bd 100644 --- a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php +++ b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php @@ -36,7 +36,7 @@ class StatusesConfig extends BaseDataTransferObject public function __construct() { - $config = DI::config(); + $config = DI::config(); $this->max_characters = (int)$config->get( 'config', 'api_import_size', From 0bb2d00e703230738ebb2d1c3ce3e8041f1ece82 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Feb 2023 08:05:46 -0500 Subject: [PATCH 4/7] Add License header to FriendicaExtensions file --- .../InstanceV2/FriendicaExtensions.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php index 2805e48665..b5ff02ea5d 100644 --- a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php +++ b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php @@ -1,4 +1,23 @@ . + * + */ namespace Friendica\Object\Api\Mastodon\InstanceV2; From 71a7c0a4de5800c33fc92f03c0a388c5a8fd3e75 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Feb 2023 13:22:29 -0500 Subject: [PATCH 5/7] Refactor Mastodon API InstanceV2 so objects are simple and BI in API layer --- src/Module/Api/Mastodon/InstanceV2.php | 95 ++++++++++++++++++- src/Object/Api/Mastodon/InstanceV2.php | 64 ++++++++----- .../Api/Mastodon/InstanceV2/Configuration.php | 21 ++-- .../Api/Mastodon/InstanceV2/Contact.php | 23 ++--- .../InstanceV2/FriendicaExtensions.php | 15 +-- .../InstanceV2/MediaAttachmentsConfig.php | 9 +- .../Api/Mastodon/InstanceV2/Registrations.php | 14 +-- .../Mastodon/InstanceV2/StatusesConfig.php | 15 ++- .../Api/Mastodon/InstanceV2/Thumbnail.php | 8 +- src/Object/Api/Mastodon/InstanceV2/Usage.php | 7 +- .../Api/Mastodon/InstanceV2/UserStats.php | 11 +-- 11 files changed, 197 insertions(+), 85 deletions(-) diff --git a/src/Module/Api/Mastodon/InstanceV2.php b/src/Module/Api/Mastodon/InstanceV2.php index 3177570c94..c1dd8314f0 100644 --- a/src/Module/Api/Mastodon/InstanceV2.php +++ b/src/Module/Api/Mastodon/InstanceV2.php @@ -21,14 +21,19 @@ namespace Friendica\Module\Api\Mastodon; +use Exception; use Friendica\App; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\L10n; use Friendica\Core\System; use Friendica\Database\Database; +use Friendica\DI; +use Friendica\Model\User; use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; +use Friendica\Module\Register; use Friendica\Object\Api\Mastodon\InstanceV2 as InstanceEntity; +use Friendica\Util\Images; use Friendica\Util\Profiler; use Psr\Log\LoggerInterface; @@ -67,9 +72,97 @@ class InstanceV2 extends BaseApi * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\NotFoundException * @throws \ImagickException + * @throws Exception */ protected function rawContent(array $request = []) { - System::jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database, System::getRules())); + $domain = $this->baseUrl->getHostname(); + $title = $this->config->get('config', 'sitename'); + $version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; + $description = $this->config->get('config', 'info'); + $usage = $this->buildUsageInfo(); + $thumbnail = new InstanceEntity\Thumbnail($this->baseUrl->get() . 'images/friendica-banner.jpg'); + $languages = [$this->config->get('system', 'language')]; + $configuration = $this->buildConfigurationInfo(); + $registration = $this->buildRegistrationsInfo(); + $contact = $this->buildContactInfo(); + $friendica_extensions = $this->buildFriendicaExtensionInfo(); + $rules = System::getRules(); + System::jsonExit(new InstanceEntity( + $domain, + $title, + $version, + $description, + $usage, + $thumbnail, + $languages, + $configuration, + $registration, + $contact, + $friendica_extensions, + $rules + )); + } + + private function buildConfigurationInfo():InstanceEntity\Configuration + { + $statuses_config = new InstanceEntity\StatusesConfig((int)$this->config->get( + 'config', + 'api_import_size', + $this->config->get('config', 'max_import_size') + )); + + return new InstanceEntity\Configuration( + $statuses_config, + new InstanceEntity\MediaAttachmentsConfig(Images::supportedTypes()), + $this->config->get('system', 'maximagesize') + ); + } + + private function buildContactInfo():InstanceEntity\Contact + { + $email = implode(',', User::getAdminEmailList()); + $administrator = User::getFirstAdmin(); + $account = null; + + if ($administrator) { + $adminContact = $this->database->selectFirst( + 'contact', + ['uri-id'], + ['nick' => $administrator['nickname'], 'self' => true] + ); + $account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']); + } + + return new InstanceEntity\Contact($email, $account); + } + + private function buildFriendicaExtensionInfo():InstanceEntity\FriendicaExtensions + { + return new InstanceEntity\FriendicaExtensions( + App::VERSION, + App::CODENAME, + $this->config->get('system', 'build') + ); + } + + private function buildRegistrationsInfo():InstanceEntity\Registrations + { + $register_policy = intval($this->config->get('config', 'register_policy')); + $enabled = ($register_policy != Register::CLOSED); + $approval_required = ($register_policy == Register::APPROVE); + + return new InstanceEntity\Registrations($enabled, $approval_required); + } + + private function buildUsageInfo():InstanceEntity\Usage + { + if (!empty($this->config->get('system', 'nodeinfo'))) { + $active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly')); + } else { + $active_monthly = 0; + } + + return new InstanceEntity\Usage(new InstanceEntity\UserStats($active_monthly)); } } diff --git a/src/Object/Api/Mastodon/InstanceV2.php b/src/Object/Api/Mastodon/InstanceV2.php index 20ea45ba3c..ec32b944f0 100644 --- a/src/Object/Api/Mastodon/InstanceV2.php +++ b/src/Object/Api/Mastodon/InstanceV2.php @@ -21,12 +21,7 @@ namespace Friendica\Object\Api\Mastodon; -use Friendica\App; -use Friendica\App\BaseURL; use Friendica\BaseDataTransferObject; -use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Database\Database; -use Friendica\Network\HTTPException; use Friendica\Object\Api\Mastodon\InstanceV2\Configuration; use Friendica\Object\Api\Mastodon\InstanceV2\Contact; use Friendica\Object\Api\Mastodon\InstanceV2\FriendicaExtensions; @@ -69,28 +64,45 @@ class InstanceV2 extends BaseDataTransferObject protected $friendica; /** - * @param IManageConfigValues $config - * @param BaseURL $baseUrl - * @param Database $database - * @param array $rules - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - * @throws \ImagickException + * @param string $domain + * @param string $title + * @param $version + * @param string $description + * @param Usage $usage + * @param Thumbnail $thumbnail + * @param array $languages + * @param Configuration $configuration + * @param Registrations $registrations + * @param Contact $contact + * @param FriendicaExtensions $friendica_extensions + * @param array $rules */ - public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database, array $rules = []) - { - $this->domain = $baseUrl->getHostname(); - $this->title = $config->get('config', 'sitename'); - $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; - $this->source_url = null; //not supported yet - $this->description = $config->get('config', 'info'); - $this->usage = new Usage($config); - $this->thumbnail = new Thumbnail($baseUrl); - $this->languages = [$config->get('system', 'language')]; - $this->configuration = new Configuration(); - $this->registrations = new Registrations(); - $this->contact = new Contact($database); + public function __construct( + string $domain, + string $title, + string $version, + string $description, + Usage $usage, + Thumbnail $thumbnail, + array $languages, + Configuration $configuration, + Registrations $registrations, + Contact $contact, + FriendicaExtensions $friendica_extensions, + array $rules + ) { + $this->domain = $domain; + $this->title = $title; + $this->version = $version; + $this->source_url = null; //not supported yet + $this->description = $description; + $this->usage = $usage; + $this->thumbnail = $thumbnail; + $this->languages = $languages; + $this->configuration = $configuration; + $this->registrations = $registrations; + $this->contact = $contact; $this->rules = $rules; - $this->friendica = new FriendicaExtensions(); + $this->friendica = $friendica_extensions; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Configuration.php b/src/Object/Api/Mastodon/InstanceV2/Configuration.php index e30561356b..a44c2e060f 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Configuration.php +++ b/src/Object/Api/Mastodon/InstanceV2/Configuration.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class Configuration @@ -33,15 +32,23 @@ class Configuration extends BaseDataTransferObject { /** @var StatusesConfig */ protected $statuses; - /** @var MediaAttachmentsConfig */ + /** @var MediaAttachmentsConfig */ protected $media_attachments; /** @var int */ protected $image_size_limit; - public function __construct() - { - $this->statuses = new StatusesConfig(); - $this->media_attachments = new MediaAttachmentsConfig(); - $this->image_size_limit = DI::config()->get('system', 'maximagesize'); + /** + * @param StatusesConfig $statuses + * @param MediaAttachmentsConfig $media_attachments + * @param int $image_size_limit + */ + public function __construct( + StatusesConfig $statuses, + MediaAttachmentsConfig $media_attachments, + int $image_size_limit + ) { + $this->statuses = $statuses; + $this->media_attachments = $media_attachments; + $this->image_size_limit = $image_size_limit; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Contact.php b/src/Object/Api/Mastodon/InstanceV2/Contact.php index 007c0495bc..f48b027cf0 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Contact.php +++ b/src/Object/Api/Mastodon/InstanceV2/Contact.php @@ -22,9 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\Database\Database; -use Friendica\DI; -use Friendica\Model\User; use Friendica\Object\Api\Mastodon\Account; /** @@ -36,21 +33,17 @@ class Contact extends BaseDataTransferObject { /** @var string */ protected $email; - /** @var Account */ + /** @var Account|null */ protected $account = null; - public function __construct(Database $database) + /** + * @param string $email + * @param Account $account + */ + public function __construct(string $email, Account $account) { - $this->email = implode(',', User::getAdminEmailList()); - $administrator = User::getFirstAdmin(); - if ($administrator) { - $adminContact = $database->selectFirst( - 'contact', - ['uri-id'], - ['nick' => $administrator['nickname'], 'self' => true] - ); - $this->account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']); - } + $this->email = $email; + $this->account = $account; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php index b5ff02ea5d..d3a2372105 100644 --- a/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php +++ b/src/Object/Api/Mastodon/InstanceV2/FriendicaExtensions.php @@ -21,9 +21,7 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; -use Friendica\App; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class FriendicaExtensions @@ -41,10 +39,15 @@ class FriendicaExtensions extends BaseDataTransferObject /** @var int */ protected $db_version; - public function __construct() + /** + * @param string $version + * @param string $codename + * @param int $db_version + */ + public function __construct(string $version, string $codename, int $db_version) { - $this->version = App::VERSION; - $this->codename = App::CODENAME; - $this->db_version = DI::config()->get('system', 'build'); + $this->version = $version; + $this->codename = $codename; + $this->db_version = $db_version; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php b/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php index 598736c330..085bdfd0e2 100644 --- a/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php +++ b/src/Object/Api/Mastodon/InstanceV2/MediaAttachmentsConfig.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\Util\Images; /** * Class MediaAttachmentsConfig @@ -33,8 +32,12 @@ class MediaAttachmentsConfig extends BaseDataTransferObject { /** @var string[] */ protected $supported_mime_types; - public function __construct() + + /** + * @param array $supported_mime_types + */ + public function __construct(array $supported_mime_types) { - $this->supported_mime_types = Images::supportedTypes(); + $this->supported_mime_types = $supported_mime_types; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Registrations.php b/src/Object/Api/Mastodon/InstanceV2/Registrations.php index c1e7769e83..b4a9054568 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Registrations.php +++ b/src/Object/Api/Mastodon/InstanceV2/Registrations.php @@ -22,8 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; -use Friendica\Module\Register; /** * Class Registrations @@ -37,11 +35,13 @@ class Registrations extends BaseDataTransferObject /** @var bool */ protected $approval_required; - public function __construct() + /** + * @param bool $enabled + * @param bool $approval_required + */ + public function __construct(bool $enabled, bool $approval_required) { - $config = DI::config(); - $register_policy = intval($config->get('config', 'register_policy')); - $this->enabled = ($register_policy != Register::CLOSED); - $this->approval_required = ($register_policy == Register::APPROVE); + $this->enabled = $enabled; + $this->approval_required = $approval_required; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php index fb74b1d5bd..aa2af4f047 100644 --- a/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php +++ b/src/Object/Api/Mastodon/InstanceV2/StatusesConfig.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class StatusConfig @@ -31,16 +30,14 @@ use Friendica\DI; */ class StatusesConfig extends BaseDataTransferObject { - /** @var int */ + /** @var int */ protected $max_characters = 0; - public function __construct() + /** + * @param int $max_characters + */ + public function __construct(int $max_characters) { - $config = DI::config(); - $this->max_characters = (int)$config->get( - 'config', - 'api_import_size', - $config->get('config', 'max_import_size') - ); + $this->max_characters = $max_characters; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php b/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php index 18cb1bf2a7..3d4989e367 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php +++ b/src/Object/Api/Mastodon/InstanceV2/Thumbnail.php @@ -21,7 +21,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; -use Friendica\App\BaseURL; use Friendica\BaseDataTransferObject; /** @@ -34,8 +33,11 @@ class Thumbnail extends BaseDataTransferObject /** @var string (URL) */ protected $url; - public function __construct(BaseURL $baseUrl) + /** + * @param string $url + */ + public function __construct(string $url) { - $this->url = $baseUrl->get() . 'images/friendica-banner.jpg'; + $this->url = $url; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/Usage.php b/src/Object/Api/Mastodon/InstanceV2/Usage.php index cb49348337..69e26b8ab7 100644 --- a/src/Object/Api/Mastodon/InstanceV2/Usage.php +++ b/src/Object/Api/Mastodon/InstanceV2/Usage.php @@ -33,8 +33,11 @@ class Usage extends BaseDataTransferObject /** @var UserStats */ protected $users; - public function __construct() + /** + * @param UserStats $users + */ + public function __construct(UserStats $users) { - $this->users = new UserStats(); + $this->users = $users; } } diff --git a/src/Object/Api/Mastodon/InstanceV2/UserStats.php b/src/Object/Api/Mastodon/InstanceV2/UserStats.php index c82cf563ee..8f398e0d5a 100644 --- a/src/Object/Api/Mastodon/InstanceV2/UserStats.php +++ b/src/Object/Api/Mastodon/InstanceV2/UserStats.php @@ -22,7 +22,6 @@ namespace Friendica\Object\Api\Mastodon\InstanceV2; use Friendica\BaseDataTransferObject; -use Friendica\DI; /** * Class UserStats @@ -34,11 +33,11 @@ class UserStats extends BaseDataTransferObject /** @var int */ protected $active_monthly = 0; - public function __construct() + /** + * @param $active_monthly + */ + public function __construct($active_monthly) { - $config = DI::config(); - if (!empty($config->get('system', 'nodeinfo'))) { - $this->active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly')); - } + $this->active_monthly = $active_monthly; } } From 06f53d2d1a11783e1e37e757007de5521543ca8a Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Feb 2023 13:31:04 -0500 Subject: [PATCH 6/7] Add missing spaces after colons for function parameters for style check reasons --- src/Module/Api/Mastodon/InstanceV2.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Module/Api/Mastodon/InstanceV2.php b/src/Module/Api/Mastodon/InstanceV2.php index c1dd8314f0..647d292c73 100644 --- a/src/Module/Api/Mastodon/InstanceV2.php +++ b/src/Module/Api/Mastodon/InstanceV2.php @@ -104,7 +104,7 @@ class InstanceV2 extends BaseApi )); } - private function buildConfigurationInfo():InstanceEntity\Configuration + private function buildConfigurationInfo(): InstanceEntity\Configuration { $statuses_config = new InstanceEntity\StatusesConfig((int)$this->config->get( 'config', @@ -119,7 +119,7 @@ class InstanceV2 extends BaseApi ); } - private function buildContactInfo():InstanceEntity\Contact + private function buildContactInfo(): InstanceEntity\Contact { $email = implode(',', User::getAdminEmailList()); $administrator = User::getFirstAdmin(); @@ -137,7 +137,7 @@ class InstanceV2 extends BaseApi return new InstanceEntity\Contact($email, $account); } - private function buildFriendicaExtensionInfo():InstanceEntity\FriendicaExtensions + private function buildFriendicaExtensionInfo(): InstanceEntity\FriendicaExtensions { return new InstanceEntity\FriendicaExtensions( App::VERSION, @@ -146,7 +146,7 @@ class InstanceV2 extends BaseApi ); } - private function buildRegistrationsInfo():InstanceEntity\Registrations + private function buildRegistrationsInfo(): InstanceEntity\Registrations { $register_policy = intval($this->config->get('config', 'register_policy')); $enabled = ($register_policy != Register::CLOSED); @@ -155,7 +155,7 @@ class InstanceV2 extends BaseApi return new InstanceEntity\Registrations($enabled, $approval_required); } - private function buildUsageInfo():InstanceEntity\Usage + private function buildUsageInfo(): InstanceEntity\Usage { if (!empty($this->config->get('system', 'nodeinfo'))) { $active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly')); From 6cd271b10f892e8d85c4e542ad07dfb2cc616096 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Feb 2023 13:33:13 -0500 Subject: [PATCH 7/7] Modify to use the new baseURL methods. --- src/Module/Api/Mastodon/InstanceV2.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Module/Api/Mastodon/InstanceV2.php b/src/Module/Api/Mastodon/InstanceV2.php index 647d292c73..2038081d87 100644 --- a/src/Module/Api/Mastodon/InstanceV2.php +++ b/src/Module/Api/Mastodon/InstanceV2.php @@ -76,12 +76,12 @@ class InstanceV2 extends BaseApi */ protected function rawContent(array $request = []) { - $domain = $this->baseUrl->getHostname(); + $domain = $this->baseUrl->getHost(); $title = $this->config->get('config', 'sitename'); $version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; $description = $this->config->get('config', 'info'); $usage = $this->buildUsageInfo(); - $thumbnail = new InstanceEntity\Thumbnail($this->baseUrl->get() . 'images/friendica-banner.jpg'); + $thumbnail = new InstanceEntity\Thumbnail($this->baseUrl->withPath('images/friendica-banner.jpg')); $languages = [$this->config->get('system', 'language')]; $configuration = $this->buildConfigurationInfo(); $registration = $this->buildRegistrationsInfo();