From c0302758152dff4f60841fc6f7c513ce591f489a Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 31 Oct 2020 22:32:26 +0000 Subject: [PATCH] API: accounts and trends --- src/DI.php | 8 ++ src/Factory/Api/Mastodon/Emoji.php | 5 - src/Factory/Api/Mastodon/Error.php | 38 ++++++++ src/Module/Api/Mastodon/Accounts.php | 52 ++++++++++ src/Module/Api/Mastodon/Accounts/Statuses.php | 94 +++++++++++++++++++ .../Api/Mastodon/Timelines/PublicTimeline.php | 3 +- src/Module/Api/Mastodon/Trends.php | 53 +++++++++++ src/Object/Api/Mastodon/Error.php | 66 +++++++++++++ static/routes.config.php | 3 + 9 files changed, 316 insertions(+), 6 deletions(-) create mode 100644 src/Factory/Api/Mastodon/Error.php create mode 100644 src/Module/Api/Mastodon/Accounts.php create mode 100644 src/Module/Api/Mastodon/Accounts/Statuses.php create mode 100644 src/Module/Api/Mastodon/Trends.php create mode 100644 src/Object/Api/Mastodon/Error.php diff --git a/src/DI.php b/src/DI.php index 06397d0e2..6461059a8 100644 --- a/src/DI.php +++ b/src/DI.php @@ -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 */ diff --git a/src/Factory/Api/Mastodon/Emoji.php b/src/Factory/Api/Mastodon/Emoji.php index a9b4bba54..f02ca8789 100644 --- a/src/Factory/Api/Mastodon/Emoji.php +++ b/src/Factory/Api/Mastodon/Emoji.php @@ -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 { diff --git a/src/Factory/Api/Mastodon/Error.php b/src/Factory/Api/Mastodon/Error.php new file mode 100644 index 000000000..e7f645fdf --- /dev/null +++ b/src/Factory/Api/Mastodon/Error.php @@ -0,0 +1,38 @@ +. + * + */ + +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()); + } +} diff --git a/src/Module/Api/Mastodon/Accounts.php b/src/Module/Api/Mastodon/Accounts.php new file mode 100644 index 000000000..7b11e7527 --- /dev/null +++ b/src/Module/Api/Mastodon/Accounts.php @@ -0,0 +1,52 @@ +. + * + */ + +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); + } +} diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php new file mode 100644 index 000000000..2959739a8 --- /dev/null +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -0,0 +1,94 @@ +. + * + */ + +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); + } +} diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 598cfe5c6..8ed5ecd5d 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -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); diff --git a/src/Module/Api/Mastodon/Trends.php b/src/Module/Api/Mastodon/Trends.php new file mode 100644 index 000000000..5fe01f8f1 --- /dev/null +++ b/src/Module/Api/Mastodon/Trends.php @@ -0,0 +1,53 @@ +. + * + */ + +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)); + } +} diff --git a/src/Object/Api/Mastodon/Error.php b/src/Object/Api/Mastodon/Error.php new file mode 100644 index 000000000..0bfd1826c --- /dev/null +++ b/src/Object/Api/Mastodon/Error.php @@ -0,0 +1,66 @@ +. + * + */ + +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; + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 8ddd7250c..8466aec67 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -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 ]],