From 2d479529214d62fc1bc90e1e914f864f03b4fee3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 11 Dec 2019 03:50:09 -0500 Subject: [PATCH] Add POST follow request Mastodon API endpoint --- doc/API-Mastodon.md | 15 +++++++--- src/Module/Api/Mastodon/FollowRequests.php | 32 ++++++++++++++++++++++ static/routes.config.php | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/doc/API-Mastodon.md b/doc/API-Mastodon.md index a711e367f..546aca243 100644 --- a/doc/API-Mastodon.md +++ b/doc/API-Mastodon.md @@ -16,11 +16,18 @@ These endpoints use the [Mastodon API entities](https://docs.joinmastodon.org/ap ## Implemented endpoints - [GET /api/v1/follow_requests](https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests) +- [POST /api/v1/follow_requests/:id/authorize](https://docs.joinmastodon.org/api/rest/follow-requests/#post-api-v1-follow-requests-id-authorize) + - Returns a [Relationship](https://docs.joinmastodon.org/api/entities/#relationship) object. +- [POST /api/v1/follow_requests/:id/reject](https://docs.joinmastodon.org/api/rest/follow-requests/#post-api-v1-follow-requests-id-reject) + - Returns a [Relationship](https://docs.joinmastodon.org/api/entities/#relationship) object. +- POST /api/v1/follow_requests/:id/ignore + - Friendica-specific, hides the follow request from the list and prevents the remote contact from retrying. + - Returns a [Relationship](https://docs.joinmastodon.org/api/entities/#relationship) object. + + - [GET /api/v1/instance](https://docs.joinmastodon.org/api/rest/instances) - GET /api/v1/instance/peers - undocumented, but implemented by Mastodon and Pleroma + + ## Non-implemented endpoints - -- [POST /api/v1/follow_requests/:id/authorize](https://docs.joinmastodon.org/api/rest/follow-requests/#post-api-v1-follow-requests-id-authorize) -- [POST /api/v1/follow_requests/:id/reject](https://docs.joinmastodon.org/api/rest/follow-requests/#post-api-v1-follow-requests-id-reject) - diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index 16417b469..55bf70a95 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -7,6 +7,7 @@ use Friendica\App\BaseURL; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Model\Contact; +use Friendica\Model\Introduction; use Friendica\Module\Base\Api; use Friendica\Network\HTTPException; @@ -24,6 +25,37 @@ class FollowRequests extends Api } } + public static function post(array $parameters = []) + { + parent::post($parameters); + + /** @var Introduction $Intro */ + $Intro = self::getClass(Introduction::class); + $Intro->fetch(['id' => $parameters['id'], 'uid' => self::$current_user_id]); + + $contactId = $Intro->{'contact-id'}; + + $relationship = new Mastodon\Relationship(); + $relationship->id = $contactId; + + switch ($parameters['action']) { + case 'authorize': + $Intro->confirm(); + $relationship = Mastodon\Relationship::createFromContact(Contact::getById($contactId)); + break; + case 'ignore': + $Intro->ignore(); + break; + case 'reject': + $Intro->discard(); + break; + default: + throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"'); + } + + System::jsonExit($relationship); + } + /** * @param array $parameters * @throws HTTPException\InternalServerErrorException diff --git a/static/routes.config.php b/static/routes.config.php index 824354690..d23b09216 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -30,6 +30,7 @@ return [ '/api' => [ '/v1' => [ '/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]], '/instance/peers' => [Module\Api\Mastodon\Instance\Peers::class, [R::GET]], ],