Merge pull request #7926 from annando/api-instance

API: Added endpoints /instance and /instance/peers
This commit is contained in:
Hypolite Petovan 2019-12-11 04:07:49 -05:00 committed by GitHub
commit 1c69dda2e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 184 additions and 0 deletions

View File

@ -16,6 +16,8 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/ap
## Implemented endpoints
- [GET /api/v1/follow_requests](https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests)
- [GET /api/v1/instance](https://docs.joinmastodon.org/api/rest/instances)
- GET /api/v1/instance/peers - undocumented, but implemented by Mastodon and Pleroma
## Non-implemented endpoints

View File

@ -0,0 +1,84 @@
<?php
namespace Friendica\Api\Mastodon;
use Friendica\App;
use Friendica\Api\Mastodon\Account;
use Friendica\Api\Mastodon\Stats;
use Friendica\Core\Config;
use Friendica\Database\DBA;
use Friendica\Model\User;
use Friendica\Module\Register;
/**
* Class Account
*
* @see https://docs.joinmastodon.org/api/entities/#instance
*/
class Instance
{
/** @var string (URL) */
var $uri;
/** @var string */
var $title;
/** @var string */
var $description;
/** @var string */
var $email;
/** @var string */
var $version;
/** @var array */
var $urls;
/** @var Stats */
var $stats;
/** @var string */
var $thumbnail;
/** @var array */
var $languages;
/** @var int */
var $max_toot_chars;
/** @var bool */
var $registrations;
/** @var bool */
var $approval_required;
/** @var Account|null */
var $contact_account;
/**
* Creates an instance record
*
* @param App $app
*
* @return Instance
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function get(App $app) {
$register_policy = intval(Config::get('config', 'register_policy'));
$instance = new Instance();
$instance->uri = $app->getBaseURL();
$instance->title = Config::get('config', 'sitename');
$instance->description = Config::get('config', 'info');
$instance->email = Config::get('config', 'admin_email');
$instance->version = FRIENDICA_VERSION;
$instance->urls = []; // Not supported
$instance->stats = Stats::get();
$instance->thumbnail = $app->getBaseURL() . (Config::get('system', 'shortcut_icon') ?? 'images/friendica-32.png');
$instance->languages = [Config::get('system', 'language')];
$instance->max_toot_chars = (int)Config::get('config', 'api_import_size', Config::get('config', 'max_import_size'));
$instance->registrations = ($register_policy != Register::CLOSED);
$instance->approval_required = ($register_policy == Register::APPROVE);
$instance->contact_account = [];
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', [], ['nick' => $administrator['nickname'], 'self' => true]);
$instance->contact_account = Account::createFromContact($adminContact);
}
}
return $instance;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Friendica\Api\Mastodon;
use Friendica\Core\Config;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
/**
* Class Stats
*
* @see https://docs.joinmastodon.org/api/entities/#stats
*/
class Stats
{
/** @var int */
var $user_count;
/** @var int */
var $status_count;
/** @var int */
var $domain_count;
/**
* Creates a stats record
*
* @return Stats
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function get() {
$stats = new Stats();
if (!empty(Config::get('system', 'nodeinfo'))) {
$stats->user_count = intval(Config::get('nodeinfo', 'total_users'));
$stats->status_count = Config::get('nodeinfo', 'local_posts') + Config::get('nodeinfo', 'local_comments');
$stats->domain_count = DBA::count('gserver', ["`network` in (?, ?) AND `last_contact` >= `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]);
}
return $stats;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Friendica\Module\Api\Mastodon;
use Friendica\Api\Mastodon\Instance as InstanceEntity;
use Friendica\Core\System;
use Friendica\Module\Base\Api;
/**
* @see https://docs.joinmastodon.org/api/rest/instances/
*/
class Instance extends Api
{
/**
* @param array $parameters
* @throws HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
System::jsonExit(InstanceEntity::get(self::getApp()));
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Friendica\Module\Api\Mastodon\Instance;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Module\Base\Api;
use Friendica\Network\HTTPException;
use Friendica\Util\Network;
/**
* Undocumented API endpoint that is implemented by both Mastodon and Pleroma
*/
class Peers extends Api
{
/**
* @param array $parameters
* @throws HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
$return = [];
// We only select for Friendica and ActivityPub servers, since it is expected to only deliver AP compatible systems here.
$instances = DBA::select('gserver', ['url'], ["`network` in (?, ?) AND `last_contact` >= `last_failure`", Protocol::DFRN, Protocol::ACTIVITYPUB]);
while ($instance = DBA::fetch($instances)) {
$urldata = parse_url($instance['url']);
unset($urldata['scheme']);
$return[] = ltrim(Network::unparseURL($urldata), '/');
}
DBA::close($instances);
System::jsonExit($return);
}
}

View File

@ -30,6 +30,8 @@ return [
'/api' => [
'/v1' => [
'/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]],
'/instance' => [Module\Api\Mastodon\Instance::class, [R::GET]],
'/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET]],
],
],