2019-12-05 14:16:13 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-12-05 14:16:13 +01:00
|
|
|
|
|
|
|
namespace Friendica\Module\Api\Mastodon;
|
|
|
|
|
|
|
|
use Friendica\Core\System;
|
2019-12-15 23:28:01 +01:00
|
|
|
use Friendica\DI;
|
2020-01-28 03:18:42 +01:00
|
|
|
use Friendica\Module\BaseApi;
|
2019-12-05 14:16:13 +01:00
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
|
|
|
|
/**
|
2020-01-05 23:50:33 +01:00
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/follow_requests
|
2019-12-05 14:16:13 +01:00
|
|
|
*/
|
2020-01-28 03:18:42 +01:00
|
|
|
class FollowRequests extends BaseApi
|
2019-12-05 14:16:13 +01:00
|
|
|
{
|
|
|
|
public static function init(array $parameters = [])
|
|
|
|
{
|
|
|
|
parent::init($parameters);
|
|
|
|
|
2019-12-11 09:37:48 +01:00
|
|
|
if (!self::login()) {
|
|
|
|
throw new HTTPException\UnauthorizedException();
|
|
|
|
}
|
2019-12-05 14:16:13 +01:00
|
|
|
}
|
|
|
|
|
2019-12-25 11:58:54 +01:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @throws HTTPException\BadRequestException
|
|
|
|
* @throws HTTPException\ForbiddenException
|
2020-01-05 23:50:33 +01:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-12-25 11:58:54 +01:00
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
* @throws HTTPException\UnauthorizedException
|
2020-01-05 23:50:33 +01:00
|
|
|
* @throws \ImagickException
|
|
|
|
*
|
2019-12-25 11:58:54 +01:00
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow
|
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow
|
|
|
|
*/
|
2019-12-11 09:50:09 +01:00
|
|
|
public static function post(array $parameters = [])
|
|
|
|
{
|
|
|
|
parent::post($parameters);
|
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
$introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
|
2019-12-11 09:50:09 +01:00
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
$contactId = $introduction->{'contact-id'};
|
2019-12-11 09:50:09 +01:00
|
|
|
|
|
|
|
switch ($parameters['action']) {
|
|
|
|
case 'authorize':
|
2020-01-05 23:50:33 +01:00
|
|
|
$introduction->confirm();
|
|
|
|
|
|
|
|
$relationship = DI::mstdnRelationship()->createFromContactId($contactId);
|
2019-12-11 09:50:09 +01:00
|
|
|
break;
|
|
|
|
case 'ignore':
|
2020-01-05 23:50:33 +01:00
|
|
|
$introduction->ignore();
|
|
|
|
|
|
|
|
$relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
|
2019-12-11 09:50:09 +01:00
|
|
|
break;
|
|
|
|
case 'reject':
|
2020-01-05 23:50:33 +01:00
|
|
|
$introduction->discard();
|
|
|
|
|
|
|
|
$relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
|
2019-12-11 09:50:09 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
|
|
|
|
}
|
|
|
|
|
|
|
|
System::jsonExit($relationship);
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:16:13 +01:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-12-25 11:58:54 +01:00
|
|
|
* @throws \ImagickException
|
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
|
2019-12-05 14:16:13 +01:00
|
|
|
*/
|
|
|
|
public static function rawContent(array $parameters = [])
|
|
|
|
{
|
2020-10-13 05:45:02 +02:00
|
|
|
$min_id = $_GET['min_id'] ?? null;
|
2019-12-05 14:16:13 +01:00
|
|
|
$max_id = $_GET['max_id'] ?? null;
|
|
|
|
$limit = intval($_GET['limit'] ?? 40);
|
|
|
|
|
2019-12-25 11:58:54 +01:00
|
|
|
$baseUrl = DI::baseUrl();
|
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
$introductions = DI::intro()->selectByBoundaries(
|
|
|
|
['`uid` = ? AND NOT `ignore`', self::$current_user_id],
|
|
|
|
['order' => ['id' => 'DESC']],
|
2020-10-13 05:45:02 +02:00
|
|
|
$min_id,
|
2020-01-05 23:50:33 +01:00
|
|
|
$max_id,
|
|
|
|
$limit
|
2019-12-05 14:16:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$return = [];
|
2019-12-25 11:41:35 +01:00
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
foreach ($introductions as $key => $introduction) {
|
|
|
|
try {
|
|
|
|
$return[] = DI::mstdnFollowRequest()->createFromIntroduction($introduction);
|
|
|
|
} catch (HTTPException\InternalServerErrorException $exception) {
|
|
|
|
DI::intro()->delete($introduction);
|
|
|
|
unset($introductions[$key]);
|
|
|
|
}
|
2019-12-05 14:16:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$base_query = [];
|
|
|
|
if (isset($_GET['limit'])) {
|
|
|
|
$base_query['limit'] = $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$links = [];
|
2020-01-05 23:50:33 +01:00
|
|
|
if ($introductions->getTotalCount() > $limit) {
|
|
|
|
$links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $introductions[count($introductions) - 1]->id]) . '>; rel="next"';
|
2019-12-05 14:16:13 +01:00
|
|
|
}
|
2020-02-02 03:38:25 +01:00
|
|
|
|
|
|
|
if (count($introductions)) {
|
2020-10-13 05:45:02 +02:00
|
|
|
$links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['min_id' => $introductions[0]->id]) . '>; rel="prev"';
|
2020-02-02 03:38:25 +01:00
|
|
|
}
|
2019-12-05 14:16:13 +01:00
|
|
|
|
|
|
|
header('Link: ' . implode(', ', $links));
|
|
|
|
|
|
|
|
System::jsonExit($return);
|
|
|
|
}
|
|
|
|
}
|