1
0
Fork 0

Merge pull request #11267 from MrPetovan/task/11264-mastodon-api-version

Update Mastodon API version string to include Mastodon version compatibility
This commit is contained in:
Michael Vogel 2022-02-23 05:38:25 +01:00 committed by GitHub
commit 948405a486
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 43 deletions

View file

@ -21,20 +21,44 @@
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\Instance as InstanceEntity;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* @see https://docs.joinmastodon.org/api/rest/instances/
*/
class Instance 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(InstanceEntity::get());
System::jsonExit(new InstanceEntity($this->config, $this->baseUrl, $this->database));
}
}

View file

@ -21,11 +21,15 @@
namespace Friendica\Object\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseDataTransferObject;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\User;
use Friendica\Module\Register;
use Friendica\Network\HTTPException;
/**
* Class Instance
@ -68,43 +72,39 @@ class Instance extends BaseDataTransferObject
protected $rules = [];
/**
* Creates an instance record
*
* @return Instance
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @param IManageConfigValues $config
* @param BaseURL $baseUrl
* @param Database $database
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @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 = '2.8.0 (compatible; Friendica ' . 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();
$instance->uri = $baseUrl->get();
$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')));
if (!empty($config->get('config', 'admin_email'))) {
$adminList = explode(',', str_replace(' ', '', $config->get('config', 'admin_email')));
$administrator = User::getByEmail($adminList[0], ['nickname']);
if (!empty($administrator)) {
$adminContact = DBA::selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
$instance->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
$adminContact = $database->selectFirst('contact', ['id'], ['nick' => $administrator['nickname'], 'self' => true]);
$this->contact_account = DI::mstdnAccount()->createFromContactId($adminContact['id']);
}
}
return $instance;
}
}

View file

@ -22,7 +22,9 @@
namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseDataTransferObject;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Protocol;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
@ -40,19 +42,12 @@ class Stats extends BaseDataTransferObject
/** @var int */
protected $domain_count = 0;
/**
* Creates a stats record
*
* @return Stats
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
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]);
public function __construct(IManageConfigValues $config, Database $database)
{
if (!empty($config->get('system', 'nodeinfo'))) {
$this->user_count = intval($config->get('nodeinfo', 'total_users'));
$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]);
}
return $stats;
}
}