2019-12-05 08:16:13 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Module\Api\Mastodon;
|
|
|
|
|
2020-01-27 20:01:32 -05:00
|
|
|
use Friendica\Object\Api\Mastodon;
|
|
|
|
use Friendica\Object\Api\Mastodon\Relationship;
|
2019-12-05 08:16:13 -05:00
|
|
|
use Friendica\Core\System;
|
2019-12-15 17:28:01 -05:00
|
|
|
use Friendica\DI;
|
2019-12-05 08:16:13 -05:00
|
|
|
use Friendica\Model\Contact;
|
|
|
|
use Friendica\Module\Base\Api;
|
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
|
|
|
|
/**
|
2020-01-05 17:50:33 -05:00
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/follow_requests
|
2019-12-05 08:16:13 -05:00
|
|
|
*/
|
|
|
|
class FollowRequests extends Api
|
|
|
|
{
|
|
|
|
public static function init(array $parameters = [])
|
|
|
|
{
|
|
|
|
parent::init($parameters);
|
|
|
|
|
2019-12-11 03:37:48 -05:00
|
|
|
if (!self::login()) {
|
|
|
|
throw new HTTPException\UnauthorizedException();
|
|
|
|
}
|
2019-12-05 08:16:13 -05:00
|
|
|
}
|
|
|
|
|
2019-12-25 05:58:54 -05:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @throws HTTPException\BadRequestException
|
|
|
|
* @throws HTTPException\ForbiddenException
|
2020-01-05 17:50:33 -05:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-12-25 05:58:54 -05:00
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
* @throws HTTPException\UnauthorizedException
|
2020-01-05 17:50:33 -05:00
|
|
|
* @throws \ImagickException
|
|
|
|
*
|
2019-12-25 05:58:54 -05: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 03:50:09 -05:00
|
|
|
public static function post(array $parameters = [])
|
|
|
|
{
|
|
|
|
parent::post($parameters);
|
|
|
|
|
2020-01-05 17:50:33 -05:00
|
|
|
$introduction = DI::intro()->selectFirst(['id' => $parameters['id'], 'uid' => self::$current_user_id]);
|
2019-12-11 03:50:09 -05:00
|
|
|
|
2020-01-05 17:50:33 -05:00
|
|
|
$contactId = $introduction->{'contact-id'};
|
2019-12-11 03:50:09 -05:00
|
|
|
|
|
|
|
switch ($parameters['action']) {
|
|
|
|
case 'authorize':
|
2020-01-05 17:50:33 -05:00
|
|
|
$introduction->confirm();
|
|
|
|
|
|
|
|
$relationship = DI::mstdnRelationship()->createFromContactId($contactId);
|
2019-12-11 03:50:09 -05:00
|
|
|
break;
|
|
|
|
case 'ignore':
|
2020-01-05 17:50:33 -05:00
|
|
|
$introduction->ignore();
|
|
|
|
|
|
|
|
$relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
|
2019-12-11 03:50:09 -05:00
|
|
|
break;
|
|
|
|
case 'reject':
|
2020-01-05 17:50:33 -05:00
|
|
|
$introduction->discard();
|
|
|
|
|
|
|
|
$relationship = DI::mstdnRelationship()->createDefaultFromContactId($contactId);
|
2019-12-11 03:50:09 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new HTTPException\BadRequestException('Unexpected action parameter, expecting "authorize", "ignore" or "reject"');
|
|
|
|
}
|
|
|
|
|
|
|
|
System::jsonExit($relationship);
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:16:13 -05:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-12-25 05:58:54 -05:00
|
|
|
* @throws \ImagickException
|
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows
|
2019-12-05 08:16:13 -05:00
|
|
|
*/
|
|
|
|
public static function rawContent(array $parameters = [])
|
|
|
|
{
|
|
|
|
$since_id = $_GET['since_id'] ?? null;
|
|
|
|
$max_id = $_GET['max_id'] ?? null;
|
|
|
|
$limit = intval($_GET['limit'] ?? 40);
|
|
|
|
|
2019-12-25 05:58:54 -05:00
|
|
|
$baseUrl = DI::baseUrl();
|
|
|
|
|
2020-01-05 17:50:33 -05:00
|
|
|
$introductions = DI::intro()->selectByBoundaries(
|
|
|
|
['`uid` = ? AND NOT `ignore`', self::$current_user_id],
|
|
|
|
['order' => ['id' => 'DESC']],
|
|
|
|
$since_id,
|
|
|
|
$max_id,
|
|
|
|
$limit
|
2019-12-05 08:16:13 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$return = [];
|
2019-12-25 05:41:35 -05:00
|
|
|
|
2020-01-05 17:50:33 -05: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 08:16:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$base_query = [];
|
|
|
|
if (isset($_GET['limit'])) {
|
|
|
|
$base_query['limit'] = $limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$links = [];
|
2020-01-05 17:50:33 -05: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 08:16:13 -05:00
|
|
|
}
|
2020-01-05 17:50:33 -05:00
|
|
|
$links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $introductions[0]->id]) . '>; rel="prev"';
|
2019-12-05 08:16:13 -05:00
|
|
|
|
|
|
|
header('Link: ' . implode(', ', $links));
|
|
|
|
|
|
|
|
System::jsonExit($return);
|
|
|
|
}
|
|
|
|
}
|