From: Hypolite Petovan <hypolite@mrpetovan.com>
Date: Thu, 5 Dec 2019 13:16:13 +0000 (-0500)
Subject: Add GET /api/v1/follow_requests Mastodon API endpoint
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8016cb3ceeff134896464faa1ab5f2d60ad8a6fd;p=friendica.git

Add GET /api/v1/follow_requests Mastodon API endpoint
---

diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php
new file mode 100644
index 0000000000..515dc451cd
--- /dev/null
+++ b/src/Module/Api/Mastodon/FollowRequests.php
@@ -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);
+	}
+}
diff --git a/static/routes.config.php b/static/routes.config.php
index f5b44aec22..d8113c5c53 100644
--- a/static/routes.config.php
+++ b/static/routes.config.php
@@ -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]],