Refactor Mastodon API InstanceV2 so objects are simple and BI in API layer

This commit is contained in:
Hank Grabowski 2023-02-20 13:22:29 -05:00
parent 0bb2d00e70
commit 71a7c0a4de
11 changed files with 197 additions and 85 deletions

View File

@ -21,14 +21,19 @@
namespace Friendica\Module\Api\Mastodon; namespace Friendica\Module\Api\Mastodon;
use Exception;
use Friendica\App; use Friendica\App;
use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model\User;
use Friendica\Module\Api\ApiResponse; use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Module\Register;
use Friendica\Object\Api\Mastodon\InstanceV2 as InstanceEntity; use Friendica\Object\Api\Mastodon\InstanceV2 as InstanceEntity;
use Friendica\Util\Images;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -67,9 +72,97 @@ class InstanceV2 extends BaseApi
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\NotFoundException * @throws \Friendica\Network\HTTPException\NotFoundException
* @throws \ImagickException * @throws \ImagickException
* @throws Exception
*/ */
protected function rawContent(array $request = []) 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));
} }
} }

View File

@ -21,12 +21,7 @@
namespace Friendica\Object\Api\Mastodon; namespace Friendica\Object\Api\Mastodon;
use Friendica\App;
use Friendica\App\BaseURL;
use Friendica\BaseDataTransferObject; 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\Configuration;
use Friendica\Object\Api\Mastodon\InstanceV2\Contact; use Friendica\Object\Api\Mastodon\InstanceV2\Contact;
use Friendica\Object\Api\Mastodon\InstanceV2\FriendicaExtensions; use Friendica\Object\Api\Mastodon\InstanceV2\FriendicaExtensions;
@ -69,28 +64,45 @@ class InstanceV2 extends BaseDataTransferObject
protected $friendica; protected $friendica;
/** /**
* @param IManageConfigValues $config * @param string $domain
* @param BaseURL $baseUrl * @param string $title
* @param Database $database * @param $version
* @param array $rules * @param string $description
* @throws HTTPException\InternalServerErrorException * @param Usage $usage
* @throws HTTPException\NotFoundException * @param Thumbnail $thumbnail
* @throws \ImagickException * @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 = []) public function __construct(
{ string $domain,
$this->domain = $baseUrl->getHostname(); string $title,
$this->title = $config->get('config', 'sitename'); string $version,
$this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; string $description,
$this->source_url = null; //not supported yet Usage $usage,
$this->description = $config->get('config', 'info'); Thumbnail $thumbnail,
$this->usage = new Usage($config); array $languages,
$this->thumbnail = new Thumbnail($baseUrl); Configuration $configuration,
$this->languages = [$config->get('system', 'language')]; Registrations $registrations,
$this->configuration = new Configuration(); Contact $contact,
$this->registrations = new Registrations(); FriendicaExtensions $friendica_extensions,
$this->contact = new Contact($database); 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->rules = $rules;
$this->friendica = new FriendicaExtensions(); $this->friendica = $friendica_extensions;
} }
} }

View File

@ -22,7 +22,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\DI;
/** /**
* Class Configuration * Class Configuration
@ -33,15 +32,23 @@ class Configuration extends BaseDataTransferObject
{ {
/** @var StatusesConfig */ /** @var StatusesConfig */
protected $statuses; protected $statuses;
/** @var MediaAttachmentsConfig */ /** @var MediaAttachmentsConfig */
protected $media_attachments; protected $media_attachments;
/** @var int */ /** @var int */
protected $image_size_limit; protected $image_size_limit;
public function __construct() /**
{ * @param StatusesConfig $statuses
$this->statuses = new StatusesConfig(); * @param MediaAttachmentsConfig $media_attachments
$this->media_attachments = new MediaAttachmentsConfig(); * @param int $image_size_limit
$this->image_size_limit = DI::config()->get('system', 'maximagesize'); */
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;
} }
} }

View File

@ -22,9 +22,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model\User;
use Friendica\Object\Api\Mastodon\Account; use Friendica\Object\Api\Mastodon\Account;
/** /**
@ -36,21 +33,17 @@ class Contact extends BaseDataTransferObject
{ {
/** @var string */ /** @var string */
protected $email; protected $email;
/** @var Account */ /** @var Account|null */
protected $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()); $this->email = $email;
$administrator = User::getFirstAdmin(); $this->account = $account;
if ($administrator) {
$adminContact = $database->selectFirst(
'contact',
['uri-id'],
['nick' => $administrator['nickname'], 'self' => true]
);
$this->account = DI::mstdnAccount()->createFromUriId($adminContact['uri-id']);
}
} }
} }

View File

@ -21,9 +21,7 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\App;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\DI;
/** /**
* Class FriendicaExtensions * Class FriendicaExtensions
@ -41,10 +39,15 @@ class FriendicaExtensions extends BaseDataTransferObject
/** @var int */ /** @var int */
protected $db_version; 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->version = $version;
$this->codename = App::CODENAME; $this->codename = $codename;
$this->db_version = DI::config()->get('system', 'build'); $this->db_version = $db_version;
} }
} }

View File

@ -22,7 +22,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\Util\Images;
/** /**
* Class MediaAttachmentsConfig * Class MediaAttachmentsConfig
@ -33,8 +32,12 @@ class MediaAttachmentsConfig extends BaseDataTransferObject
{ {
/** @var string[] */ /** @var string[] */
protected $supported_mime_types; 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;
} }
} }

View File

@ -22,8 +22,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\DI;
use Friendica\Module\Register;
/** /**
* Class Registrations * Class Registrations
@ -37,11 +35,13 @@ class Registrations extends BaseDataTransferObject
/** @var bool */ /** @var bool */
protected $approval_required; protected $approval_required;
public function __construct() /**
* @param bool $enabled
* @param bool $approval_required
*/
public function __construct(bool $enabled, bool $approval_required)
{ {
$config = DI::config(); $this->enabled = $enabled;
$register_policy = intval($config->get('config', 'register_policy')); $this->approval_required = $approval_required;
$this->enabled = ($register_policy != Register::CLOSED);
$this->approval_required = ($register_policy == Register::APPROVE);
} }
} }

View File

@ -22,7 +22,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\DI;
/** /**
* Class StatusConfig * Class StatusConfig
@ -31,16 +30,14 @@ use Friendica\DI;
*/ */
class StatusesConfig extends BaseDataTransferObject class StatusesConfig extends BaseDataTransferObject
{ {
/** @var int */ /** @var int */
protected $max_characters = 0; protected $max_characters = 0;
public function __construct() /**
* @param int $max_characters
*/
public function __construct(int $max_characters)
{ {
$config = DI::config(); $this->max_characters = $max_characters;
$this->max_characters = (int)$config->get(
'config',
'api_import_size',
$config->get('config', 'max_import_size')
);
} }
} }

View File

@ -21,7 +21,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\App\BaseURL;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
/** /**
@ -34,8 +33,11 @@ class Thumbnail extends BaseDataTransferObject
/** @var string (URL) */ /** @var string (URL) */
protected $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;
} }
} }

View File

@ -33,8 +33,11 @@ class Usage extends BaseDataTransferObject
/** @var UserStats */ /** @var UserStats */
protected $users; protected $users;
public function __construct() /**
* @param UserStats $users
*/
public function __construct(UserStats $users)
{ {
$this->users = new UserStats(); $this->users = $users;
} }
} }

View File

@ -22,7 +22,6 @@
namespace Friendica\Object\Api\Mastodon\InstanceV2; namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\DI;
/** /**
* Class UserStats * Class UserStats
@ -34,11 +33,11 @@ class UserStats extends BaseDataTransferObject
/** @var int */ /** @var int */
protected $active_monthly = 0; protected $active_monthly = 0;
public function __construct() /**
* @param $active_monthly
*/
public function __construct($active_monthly)
{ {
$config = DI::config(); $this->active_monthly = $active_monthly;
if (!empty($config->get('system', 'nodeinfo'))) {
$this->active_monthly = intval(DI::keyValue()->get('nodeinfo_active_users_monthly'));
}
} }
} }