Add Mastodon Instance V2 API endpoint /api/v2/instance

This commit is contained in:
Hank Grabowski 2023-02-20 07:51:20 -05:00
parent 8101739edd
commit 0451f2dfa1
12 changed files with 564 additions and 1 deletions

View file

@ -0,0 +1,75 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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()));
}
}

View file

@ -0,0 +1,96 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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();
}
}

View file

@ -0,0 +1,47 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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');
}
}

View file

@ -0,0 +1,56 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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']);
}
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace Friendica\Object\Api\Mastodon\InstanceV2;
use Friendica\App;
use Friendica\BaseDataTransferObject;
use Friendica\DI;
/**
* Class FriendicaExtensions
*
* Friendica specific additional fields on the Instance V2 object
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class FriendicaExtensions extends BaseDataTransferObject
{
/** @var string */
protected $version;
/** @var string */
protected $codename;
/** @var int */
protected $db_version;
public function __construct()
{
$this->version = App::VERSION;
$this->codename = App::CODENAME;
$this->db_version = DI::config()->get('system', 'build');
}
}

View file

@ -0,0 +1,40 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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();
}
}

View file

@ -0,0 +1,47 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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);
}
}

View file

@ -0,0 +1,46 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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')
);
}
}

View file

@ -0,0 +1,41 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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';
}
}

View file

@ -0,0 +1,40 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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();
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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'));
}
}
}

View file

@ -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