Add constructors to a couple of Mastodon API value objects

This commit is contained in:
Hypolite Petovan 2022-02-20 16:17:03 -05:00
parent c86cc83524
commit e0ea51818e
3 changed files with 39 additions and 43 deletions

View file

@ -22,6 +22,7 @@
namespace Friendica\Module\Api\Mastodon; namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\DI;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Object\Api\Mastodon\Instance as InstanceEntity; use Friendica\Object\Api\Mastodon\Instance as InstanceEntity;
@ -35,6 +36,6 @@ class Instance extends BaseApi
*/ */
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
System::jsonExit(InstanceEntity::get()); System::jsonExit(new InstanceEntity(DI::config(), $this->baseUrl, DI::dba()));
} }
} }

View file

@ -21,11 +21,15 @@
namespace Friendica\Object\Api\Mastodon; namespace Friendica\Object\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module\Register; use Friendica\Module\Register;
use Friendica\Network\HTTPException;
/** /**
* Class Instance * Class Instance
@ -68,43 +72,39 @@ class Instance extends BaseDataTransferObject
protected $rules = []; protected $rules = [];
/** /**
* Creates an instance record * @param IManageConfigValues $config
* * @param BaseURL $baseUrl
* @return Instance * @param Database $database
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function get() public function __construct(IManageConfigValues $config, BaseURL $baseUrl, Database $database)
{ {
$register_policy = intval(DI::config()->get('config', 'register_policy')); $register_policy = intval($config->get('config', 'register_policy'));
$baseUrl = DI::baseUrl(); $this->uri = $baseUrl->get();
$this->title = $config->get('config', 'sitename');
$this->short_description = $this->description = $config->get('config', 'info');
$this->email = $config->get('config', 'admin_email');
$this->version = FRIENDICA_VERSION;
$this->urls = null; // Not supported
$this->stats = new Stats($config, $database);
$this->thumbnail = $baseUrl->get() . ($config->get('system', 'shortcut_icon') ?? 'images/friendica-32.png');
$this->languages = [$config->get('system', 'language')];
$this->max_toot_chars = (int)$config->get('config', 'api_import_size', $config->get('config', 'max_import_size'));
$this->registrations = ($register_policy != Register::CLOSED);
$this->approval_required = ($register_policy == Register::APPROVE);
$this->invites_enabled = false;
$this->contact_account = [];
$instance = new Instance(); if (!empty($config->get('config', 'admin_email'))) {
$instance->uri = $baseUrl->get(); $adminList = explode(',', str_replace(' ', '', $config->get('config', 'admin_email')));
$instance->title = DI::config()->get('config', 'sitename');
$instance->short_description = $instance->description = DI::config()->get('config', 'info');
$instance->email = DI::config()->get('config', 'admin_email');
$instance->version = FRIENDICA_VERSION;
$instance->urls = null; // Not supported
$instance->stats = Stats::get();
$instance->thumbnail = $baseUrl->get() . (DI::config()->get('system', 'shortcut_icon') ?? 'images/friendica-32.png');
$instance->languages = [DI::config()->get('system', 'language')];
$instance->max_toot_chars = (int)DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size'));
$instance->registrations = ($register_policy != Register::CLOSED);
$instance->approval_required = ($register_policy == Register::APPROVE);
$instance->invites_enabled = false;
$instance->contact_account = [];
if (!empty(DI::config()->get('config', 'admin_email'))) {
$adminList = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
$administrator = User::getByEmail($adminList[0], ['nickname']); $administrator = User::getByEmail($adminList[0], ['nickname']);
if (!empty($administrator)) { if (!empty($administrator)) {
$adminContact = DBA::selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]); $adminContact = $database->selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
$instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']); $this->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
} }
} }
return $instance;
} }
} }

View file

@ -22,7 +22,9 @@
namespace Friendica\Object\Api\Mastodon; namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseDataTransferObject; use Friendica\BaseDataTransferObject;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
@ -40,19 +42,12 @@ class Stats extends BaseDataTransferObject
/** @var int */ /** @var int */
protected $domain_count = 0; protected $domain_count = 0;
/** public function __construct(IManageConfigValues $config, Database $database)
* Creates a stats record {
* if (!empty($config->get('system', 'nodeinfo'))) {
* @return Stats $this->user_count = intval($config->get('nodeinfo', 'total_users'));
* @throws \Friendica\Network\HTTPException\InternalServerErrorException $this->status_count = $config->get('nodeinfo', 'local_posts') + $config->get('nodeinfo', 'local_comments');
*/ $this->domain_count = $database->count('gserver', ["`network` in (?, ?) AND NOT `failed`", Protocol::DFRN, Protocol::ACTIVITYPUB]);
public static function get() {
$stats = new Stats();
if (!empty(DI::config()->get('system', 'nodeinfo'))) {
$stats->user_count = intval(DI::config()->get('nodeinfo', 'total_users'));
$stats->status_count = DI::config()->get('nodeinfo', 'local_posts') + DI::config()->get('nodeinfo', 'local_comments');
$stats->domain_count = DBA::count('gserver', ["`network` in (?, ?) AND NOT `failed`", Protocol::DFRN, Protocol::ACTIVITYPUB]);
} }
return $stats;
} }
} }