From: Michael <heluecht@pirati.ca>
Date: Sat, 31 Oct 2020 22:32:26 +0000 (+0000)
Subject: API: accounts and trends
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c0302758152dff4f60841fc6f7c513ce591f489a;p=friendica.git

API: accounts and trends
---

diff --git a/src/DI.php b/src/DI.php
index 06397d0e23..6461059a83 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 a9b4bba549..f02ca87898 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 0000000000..e7f645fdfa
--- /dev/null
+++ b/src/Factory/Api/Mastodon/Error.php
@@ -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());
+	}
+}
diff --git a/src/Module/Api/Mastodon/Accounts.php b/src/Module/Api/Mastodon/Accounts.php
new file mode 100644
index 0000000000..7b11e75271
--- /dev/null
+++ b/src/Module/Api/Mastodon/Accounts.php
@@ -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);
+	}
+}
diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php
new file mode 100644
index 0000000000..2959739a8c
--- /dev/null
+++ b/src/Module/Api/Mastodon/Accounts/Statuses.php
@@ -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);
+	}
+}
diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php
index 598cfe5c66..8ed5ecd5d1 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 0000000000..5fe01f8f1d
--- /dev/null
+++ b/src/Module/Api/Mastodon/Trends.php
@@ -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));
+	}
+}
diff --git a/src/Object/Api/Mastodon/Error.php b/src/Object/Api/Mastodon/Error.php
new file mode 100644
index 0000000000..0bfd1826c6
--- /dev/null
+++ b/src/Object/Api/Mastodon/Error.php
@@ -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;
+	}
+}
diff --git a/static/routes.config.php b/static/routes.config.php
index 8ddd7250cd..8466aec672 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         ]],