Merge pull request #12818 from HankG/mastodon-instance-v2-implementation

Mastodon instance v2 implementation
This commit is contained in:
Hypolite Petovan 2023-02-20 14:16:17 -05:00 committed by GitHub
commit dec5a40aac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 696 additions and 2 deletions

View file

@ -0,0 +1,168 @@
<?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 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;
/**
* @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
* @throws Exception
*/
protected function rawContent(array $request = [])
{
$domain = $this->baseUrl->getHost();
$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->withPath('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

@ -34,7 +34,7 @@ use Friendica\Network\HTTPException;
/**
* Class Instance
*
* @see https://docs.joinmastodon.org/api/entities/#instance
* @see https://docs.joinmastodon.org/entities/V1_Instance/
*/
class Instance extends BaseDataTransferObject
{

View file

@ -0,0 +1,108 @@
<?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\BaseDataTransferObject;
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 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
*/
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 = $description;
$this->usage = $usage;
$this->thumbnail = $thumbnail;
$this->languages = $languages;
$this->configuration = $configuration;
$this->registrations = $registrations;
$this->contact = $contact;
$this->rules = $rules;
$this->friendica = $friendica_extensions;
}
}

View file

@ -0,0 +1,54 @@
<?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 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;
/**
* @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;
}
}

View file

@ -0,0 +1,49 @@
<?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\Object\Api\Mastodon\Account;
/**
* Class Contact
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class Contact extends BaseDataTransferObject
{
/** @var string */
protected $email;
/** @var Account|null */
protected $account = null;
/**
* @param string $email
* @param Account $account
*/
public function __construct(string $email, Account $account)
{
$this->email = $email;
$this->account = $account;
}
}

View file

@ -0,0 +1,53 @@
<?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 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;
/**
* @param string $version
* @param string $codename
* @param int $db_version
*/
public function __construct(string $version, string $codename, int $db_version)
{
$this->version = $version;
$this->codename = $codename;
$this->db_version = $db_version;
}
}

View file

@ -0,0 +1,43 @@
<?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 MediaAttachmentsConfig
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class MediaAttachmentsConfig extends BaseDataTransferObject
{
/** @var string[] */
protected $supported_mime_types;
/**
* @param array $supported_mime_types
*/
public function __construct(array $supported_mime_types)
{
$this->supported_mime_types = $supported_mime_types;
}
}

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;
/**
* Class Registrations
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class Registrations extends BaseDataTransferObject
{
/** @var bool */
protected $enabled;
/** @var bool */
protected $approval_required;
/**
* @param bool $enabled
* @param bool $approval_required
*/
public function __construct(bool $enabled, bool $approval_required)
{
$this->enabled = $enabled;
$this->approval_required = $approval_required;
}
}

View file

@ -0,0 +1,43 @@
<?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 StatusConfig
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class StatusesConfig extends BaseDataTransferObject
{
/** @var int */
protected $max_characters = 0;
/**
* @param int $max_characters
*/
public function __construct(int $max_characters)
{
$this->max_characters = $max_characters;
}
}

View file

@ -0,0 +1,43 @@
<?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 Thumbnail
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class Thumbnail extends BaseDataTransferObject
{
/** @var string (URL) */
protected $url;
/**
* @param string $url
*/
public function __construct(string $url)
{
$this->url = $url;
}
}

View file

@ -0,0 +1,43 @@
<?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;
/**
* @param UserStats $users
*/
public function __construct(UserStats $users)
{
$this->users = $users;
}
}

View file

@ -0,0 +1,43 @@
<?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 UserStats
*
* @see https://docs.joinmastodon.org/entities/Instance/
*/
class UserStats extends BaseDataTransferObject
{
/** @var int */
protected $active_monthly = 0;
/**
* @param $active_monthly
*/
public function __construct($active_monthly)
{
$this->active_monthly = $active_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