API: accounts and trends

This commit is contained in:
Michael 2020-10-31 22:32:26 +00:00
parent 0e60520a36
commit c030275815
9 changed files with 316 additions and 6 deletions

View file

@ -255,6 +255,14 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\Emoji::class);
}
/**
* @return Factory\Api\Mastodon\Error
*/
public static function mstdnError()
{
return self::$dice->create(Factory\Api\Mastodon\Error::class);
}
/**
* @return Factory\Api\Mastodon\Field
*/

View file

@ -21,13 +21,8 @@
namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Emojis;
use Friendica\Model\APContact;
use Friendica\Model\Contact;
use Friendica\Network\HTTPException;
use Psr\Log\LoggerInterface;
class Emoji extends BaseFactory
{

View file

@ -0,0 +1,38 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\Factory\Api\Mastodon;
use Friendica\BaseFactory;
use Friendica\Core\System;
use Friendica\DI;
class Error extends BaseFactory
{
public function RecordNotFound()
{
$error = DI::l10n()->t('Record not found');
$error_description = '';
$errorobj = New \Friendica\Object\Api\Mastodon\Error($error, $error_description);
System::jsonError(404, $errorobj->toArray());
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/accounts/
*/
class Accounts extends BaseApi
{
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
if (empty($parameters['id'])) {
DI::mstdnError()->RecordNotFound();
}
$id = $parameters['id'];
if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
DI::mstdnError()->RecordNotFound();
}
$account = DI::mstdnAccount()->createFromContactId($id);
System::jsonExit($account);
}
}

View file

@ -0,0 +1,94 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\Accounts;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/accounts/
*/
class Statuses extends BaseApi
{
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
if (empty($parameters['id'])) {
DI::mstdnError()->RecordNotFound();
}
$id = $parameters['id'];
if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
DI::mstdnError()->RecordNotFound();
}
// Show only statuses with media attached? Defaults to false.
$only_media = (bool)!isset($_REQUEST['only_media']) ? false : ($_REQUEST['only_media'] == 'true'); // Currently not supported
// Return results older than this id
$max_id = (int)!isset($_REQUEST['max_id']) ? 0 : $_REQUEST['max_id'];
// Return results newer than this id
$since_id = (int)!isset($_REQUEST['since_id']) ? 0 : $_REQUEST['since_id'];
// Return results immediately newer than this id
$min_id = (int)!isset($_REQUEST['min_id']) ? 0 : $_REQUEST['min_id'];
// Maximum number of results to return. Defaults to 20.
$limit = (int)!isset($_REQUEST['limit']) ? 20 : $_REQUEST['limit'];
$params = ['order' => ['uri-id' => true], 'limit' => $limit];
$condition = ['author-id' => $id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
'private' => Item::PUBLIC, 'uid' => 0, 'network' => Protocol::FEDERATED];
if (!empty($max_id)) {
$condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $max_id]);
}
if (!empty($since_id)) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $since_id]);
}
if (!empty($min_id)) {
$condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $min_id]);
$params['order'] = ['uri-id'];
}
$items = Item::selectForUser(0, ['uri-id', 'uid'], $condition, $params);
$statuses = [];
while ($item = Item::fetch($items)) {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $item['uid']);
}
DBA::close($items);
if (!empty($min_id)) {
array_reverse($statuses);
}
System::jsonExit($statuses);
}
}

View file

@ -84,9 +84,10 @@ class PublicTimeline extends BaseApi
$items = Item::selectForUser(0, ['uri-id', 'uid'], $condition, $params);
$statuses = [];
foreach ($items as $item) {
while ($item = Item::fetch($items)) {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $item['uid']);
}
DBA::close($items);
if (!empty($min_id)) {
array_reverse($statuses);

View file

@ -0,0 +1,53 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\Core\System;
use Friendica\DI;
use Friendica\Model\Tag;
use Friendica\Module\BaseApi;
/**
* @see https://docs.joinmastodon.org/methods/instance/trends/
*/
class Trends extends BaseApi
{
/**
* @param array $parameters
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function rawContent(array $parameters = [])
{
// Maximum number of results to return. Defaults to 10.
$limit = (int)!isset($_REQUEST['limit']) ? 10 : $_REQUEST['limit'];
$trending = [];
$tags = Tag::getGlobalTrendingHashtags(24, 20);
foreach ($tags as $tag) {
$tag['name'] = $tag['term'];
$hashtag = new \Friendica\Object\Api\Mastodon\Tag(DI::baseUrl(), $tag);
$trending[] = $hashtag->toArray();
}
System::jsonExit(array_slice($trending, 0, $limit));
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @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\BaseEntity;
/**
* Class Error
*
* @see https://docs.joinmastodon.org/entities/error
*/
class Error extends BaseEntity
{
/** @var string */
protected $error;
/** @var string */
protected $error_description;
/**
* Creates an error record
*
* @param string $error
* @param string error_description
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(string $error, string $error_description)
{
$this->error = $error;
$this->error_description = $error_description;
}
/**
* Returns the current entity as an array
*
* @return array
*/
public function toArray()
{
$error = parent::toArray();
if (empty($error['error_description'])) {
unset($error['error_description']);
}
return $error;
}
}

View file

@ -56,6 +56,8 @@ return [
'/api' => [
'/v1' => [
'/accounts/{id:\d+}' => [Module\Api\Mastodon\Accounts::class, [R::GET ]],
'/accounts/{id:\d+}/statuses' => [Module\Api\Mastodon\Accounts\Statuses::class, [R::GET ]],
'/custom_emojis' => [Module\Api\Mastodon\CustomEmojis::class, [R::GET ]],
'/directory' => [Module\Api\Mastodon\Directory::class, [R::GET ]],
'/follow_requests' => [Module\Api\Mastodon\FollowRequests::class, [R::GET ]],
@ -63,6 +65,7 @@ return [
'/instance' => [Module\Api\Mastodon\Instance::class, [R::GET ]],
'/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET ]],
'/timelines/public' => [Module\Api\Mastodon\Timelines\PublicTimeline::class, [R::GET ]],
'/trends' => [Module\Api\Mastodon\Trends::class, [R::GET ]],
],
'/friendica' => [
'/profile/show' => [Module\Api\Friendica\Profile\Show::class , [R::GET ]],