]> git.mxchange.org Git - friendica.git/commitdiff
API: Support for the "directory" endpoint
authorMichael <heluecht@pirati.ca>
Tue, 1 Sep 2020 19:29:39 +0000 (19:29 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 1 Sep 2020 19:29:39 +0000 (19:29 +0000)
src/Module/Api/Mastodon/Directory.php [new file with mode: 0644]
static/routes.config.php

diff --git a/src/Module/Api/Mastodon/Directory.php b/src/Module/Api/Mastodon/Directory.php
new file mode 100644 (file)
index 0000000..7d18637
--- /dev/null
@@ -0,0 +1,72 @@
+<?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\Logger;
+use Friendica\Core\Protocol;
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Module\BaseApi;
+use Friendica\Network\HTTPException;
+
+/**
+ * @see https://docs.joinmastodon.org/methods/instance/directory/
+ */
+class Directory extends BaseApi
+{
+       /**
+        * @param array $parameters
+        * @throws HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        * @see https://docs.joinmastodon.org/methods/instance/directory/
+        */
+       public static function rawContent(array $parameters = [])
+       {
+               $offset = (int)!isset($_REQUEST['offset']) ? 0 : $_REQUEST['offset'];
+               $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
+               $order = !isset($_REQUEST['order']) ? 'active' : $_REQUEST['order'];
+               $local = (bool)!isset($_REQUEST['local']) ? false : ($_REQUEST['local'] == 'true');
+
+               Logger::info('directory', ['offset' => $offset, 'limit' => $limit, 'order' => $order, 'local' => $local]);
+
+               if ($local) {
+                       $table = 'owner-view';
+                       $condition = ['net-publish' => true];
+               } else {
+                       $table = 'contact';
+                       $condition = ['uid' => 0, 'hidden' => false, 'network' => Protocol::FEDERATED];
+               }
+
+               $params = ['limit' => [$offset, $limit],
+                       'order' => [($order == 'active') ? 'last-item' : 'created' => true]];
+
+               $accounts = [];
+               $contacts = DBA::select($table, ['id'], $condition, $params);
+               while ($contact = DBA::fetch($contacts)) {
+                       $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id']);
+               }
+               DBA::close($contacts);
+
+               System::jsonExit($accounts);
+       }
+}
index 4227db240641f1bd432a2a4d4e343c39312613a1..8387366b3dcb8513797066b773e62e586efa99fb 100644 (file)
@@ -49,6 +49,7 @@ return [
        '/api' => [
                '/v1' => [
                        '/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         ]],
                        '/follow_requests/{id:\d+}/{action}' => [Module\Api\Mastodon\FollowRequests::class, [        R::POST]],
                        '/instance'                          => [Module\Api\Mastodon\Instance::class,       [R::GET         ]],