mirror of
https://github.com/friendica/friendica
synced 2024-11-10 02:22:55 +00:00
Refactor Mastodon API InstanceV2 so objects are simple and BI in API layer
This commit is contained in:
parent
0bb2d00e70
commit
71a7c0a4de
11 changed files with 197 additions and 85 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 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
|
||||
* @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 . ')';
|
||||
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 = $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->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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
namespace Friendica\Object\Api\Mastodon\InstanceV2;
|
||||
|
||||
use Friendica\BaseDataTransferObject;
|
||||
use Friendica\DI;
|
||||
|
||||
/**
|
||||
* Class Configuration
|
||||
|
@ -38,10 +37,18 @@ class Configuration extends BaseDataTransferObject
|
|||
/** @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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
namespace Friendica\Object\Api\Mastodon\InstanceV2;
|
||||
|
||||
use Friendica\BaseDataTransferObject;
|
||||
use Friendica\DI;
|
||||
|
||||
/**
|
||||
* Class StatusConfig
|
||||
|
@ -34,13 +33,11 @@ class StatusesConfig extends BaseDataTransferObject
|
|||
/** @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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue