]> git.mxchange.org Git - friendica.git/commitdiff
Add GET /api/v1/follow_requests Mastodon API endpoint
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 5 Dec 2019 13:16:13 +0000 (08:16 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 10 Dec 2019 03:50:36 +0000 (22:50 -0500)
src/Module/Api/Mastodon/FollowRequests.php [new file with mode: 0644]
static/routes.config.php

diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php
new file mode 100644 (file)
index 0000000..515dc45
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+namespace Friendica\Module\Api\Mastodon;
+
+use Friendica\Api\Mastodon\Account;
+use Friendica\App\BaseURL;
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\Model\Contact;
+use Friendica\Module\Base\Api;
+use Friendica\Network\HTTPException;
+
+/**
+ * @see https://docs.joinmastodon.org/api/rest/follow-requests/
+ */
+class FollowRequests extends Api
+{
+       public static function init(array $parameters = [])
+       {
+               parent::init($parameters);
+
+               self::login();
+       }
+
+       /**
+        * @param array $parameters
+        * @throws HTTPException\InternalServerErrorException
+        * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests
+        */
+       public static function rawContent(array $parameters = [])
+       {
+               $since_id = $_GET['since_id'] ?? null;
+               $max_id = $_GET['max_id'] ?? null;
+               $limit = intval($_GET['limit'] ?? 40);
+
+               if (isset($since_id) && isset($max_id)) {
+                       $condition = ['`uid` = ? AND NOT `self` AND `pending` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id];
+               } elseif (isset($since_id)) {
+                       $condition = ['`uid` = ? AND NOT `self` AND `pending` AND `id` > ?', self::$current_user_id, $since_id];
+               } elseif (isset($max_id)) {
+                       $condition = ['`uid` = ? AND NOT `self` AND `pending` AND `id` < ?', self::$current_user_id, $max_id];
+               } else {
+                       $condition = ['`uid` = ? AND NOT `self` AND `pending`', self::$current_user_id];
+               }
+
+               $count = DBA::count('contact', $condition);
+
+               $contacts = Contact::selectToArray(
+                       [],
+                       $condition,
+                       ['order' => ['id' => 'DESC'], 'limit' => $limit]
+               );
+
+               $return = [];
+               foreach ($contacts as $contact) {
+                       $account = Account::createFromContact($contact);
+
+                       $return[] = $account;
+               }
+
+               $base_query = [];
+               if (isset($_GET['limit'])) {
+                       $base_query['limit'] = $limit;
+               }
+
+               /** @var BaseURL $BaseURL */
+               $BaseURL = self::getClass(BaseURL::class);
+
+               $links = [];
+               if ($count > $limit) {
+                       $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $contacts[count($contacts) - 1]['id']]) . '>; rel="next"';
+               }
+               $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $contacts[0]['id']]) . '>; rel="prev"';
+
+               header('Link: ' . implode(', ', $links));
+
+               System::jsonExit($return);
+       }
+}
index f5b44aec22c80d6c512c54e4c65de89baa42eb2f..d8113c5c53ec4d0f64af3316d365c6ea353c1b92 100644 (file)
@@ -27,6 +27,12 @@ return [
                '/recovery' => [Module\TwoFactor\Recovery::class, [R::GET, R::POST]],
        ],
 
+       '/api' => [
+               '/v1' => [
+                       '/follow_requests'                   => [Module\Api\Mastodon\FollowRequests::class, [R::GET         ]],
+               ],
+       ],
+
        '/admin'               => [
                '[/]' => [Module\Admin\Summary::class, [R::GET]],