]> git.mxchange.org Git - friendica.git/commitdiff
API: accounts and trends
authorMichael <heluecht@pirati.ca>
Sat, 31 Oct 2020 22:32:26 +0000 (22:32 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 31 Oct 2020 22:32:26 +0000 (22:32 +0000)
src/DI.php
src/Factory/Api/Mastodon/Emoji.php
src/Factory/Api/Mastodon/Error.php [new file with mode: 0644]
src/Module/Api/Mastodon/Accounts.php [new file with mode: 0644]
src/Module/Api/Mastodon/Accounts/Statuses.php [new file with mode: 0644]
src/Module/Api/Mastodon/Timelines/PublicTimeline.php
src/Module/Api/Mastodon/Trends.php [new file with mode: 0644]
src/Object/Api/Mastodon/Error.php [new file with mode: 0644]
static/routes.config.php

index 06397d0e235970bf97244633352b7b27d700106f..6461059a8392a4de3c86fc8dd36bf4b8c46b2934 100644 (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
         */
index a9b4bba5493bbd32f2290c1b24f6314b860a0f63..f02ca87898114983d4048c48d9a841de1236198c 100644 (file)
 
 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 (file)
index 0000000..e7f645f
--- /dev/null
@@ -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 (file)
index 0000000..7b11e75
--- /dev/null
@@ -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 (file)
index 0000000..2959739
--- /dev/null
@@ -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);
+       }
+}
index 598cfe5c66e6a520fb1f08bd6062637713acdaa1..8ed5ecd5d13f85f48c4a229522350a56bf394932 100644 (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);
diff --git a/src/Module/Api/Mastodon/Trends.php b/src/Module/Api/Mastodon/Trends.php
new file mode 100644 (file)
index 0000000..5fe01f8
--- /dev/null
@@ -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 (file)
index 0000000..0bfd182
--- /dev/null
@@ -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;
+       }
+}
index 8ddd7250cd274f106b40a71d76c25fa755023d32..8466aec672d7856a2877934559cad555750fb9c2 100644 (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         ]],