From c286772fb5633aec0b6927d34fe7903c66cce28f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 25 Dec 2019 05:58:54 -0500 Subject: [PATCH] Correct value of Mastodon API Account acct field for local users --- src/Api/Mastodon/Account.php | 13 +++++++++---- src/Api/Mastodon/Instance.php | 2 +- src/Module/Api/Mastodon/FollowRequests.php | 22 ++++++++++++++++------ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Api/Mastodon/Account.php b/src/Api/Mastodon/Account.php index 0a86be7e7..077b02f82 100644 --- a/src/Api/Mastodon/Account.php +++ b/src/Api/Mastodon/Account.php @@ -2,6 +2,7 @@ namespace Friendica\Api\Mastodon; +use Friendica\App\BaseURL; use Friendica\Content\Text\BBCode; use Friendica\Database\DBA; use Friendica\Model\Contact; @@ -62,17 +63,21 @@ class Account /** * Creates an account record from a public contact record. Expects all contact table fields to be set. * - * @param array $publicContact Full contact table record with uid = 0 - * @param array $apcontact Optional full apcontact table record + * @param BaseURL $baseUrl + * @param array $publicContact Full contact table record with uid = 0 + * @param array $apcontact Optional full apcontact table record * @return Account * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function create(array $publicContact, array $apcontact = []) + public static function create(BaseURL $baseUrl, array $publicContact, array $apcontact = []) { $account = new Account(); $account->id = $publicContact['id']; $account->username = $publicContact['nick']; - $account->acct = $publicContact['addr']; + $account->acct = + strpos($publicContact['url'], $baseUrl->get() . '/') === 0 ? + $publicContact['nick'] : + $publicContact['addr']; $account->display_name = $publicContact['name']; $account->locked = !empty($apcontact['manually-approve']); $account->created_at = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::ATOM); diff --git a/src/Api/Mastodon/Instance.php b/src/Api/Mastodon/Instance.php index 6d1dfb733..6652641b4 100644 --- a/src/Api/Mastodon/Instance.php +++ b/src/Api/Mastodon/Instance.php @@ -79,7 +79,7 @@ class Instance if (!empty($administrator)) { $adminContact = DBA::selectFirst('contact', [], ['nick' => $administrator['nickname'], 'self' => true]); $apcontact = APContact::getByURL($adminContact['url'], false); - $instance->contact_account = Account::create($adminContact, $apcontact); + $instance->contact_account = Account::create($baseUrl, $adminContact, $apcontact); } } diff --git a/src/Module/Api/Mastodon/FollowRequests.php b/src/Module/Api/Mastodon/FollowRequests.php index 304f78767..e1e70577b 100644 --- a/src/Module/Api/Mastodon/FollowRequests.php +++ b/src/Module/Api/Mastodon/FollowRequests.php @@ -26,6 +26,15 @@ class FollowRequests extends Api } } + /** + * @param array $parameters + * @throws HTTPException\BadRequestException + * @throws HTTPException\ForbiddenException + * @throws HTTPException\NotFoundException + * @throws HTTPException\UnauthorizedException + * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#accept-follow + * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#reject-follow + */ public static function post(array $parameters = []) { parent::post($parameters); @@ -58,7 +67,8 @@ class FollowRequests extends Api /** * @param array $parameters * @throws HTTPException\InternalServerErrorException - * @see https://docs.joinmastodon.org/api/rest/follow-requests/#get-api-v1-follow-requests + * @throws \ImagickException + * @see https://docs.joinmastodon.org/methods/accounts/follow_requests#pending-follows */ public static function rawContent(array $parameters = []) { @@ -66,6 +76,8 @@ class FollowRequests extends Api $max_id = $_GET['max_id'] ?? null; $limit = intval($_GET['limit'] ?? 40); + $baseUrl = DI::baseUrl(); + if (isset($since_id) && isset($max_id)) { $condition = ['`uid` = ? AND NOT `ignore` AND `id` > ? AND `id` < ?', self::$current_user_id, $since_id, $max_id]; } elseif (isset($since_id)) { @@ -94,7 +106,7 @@ class FollowRequests extends Api $publicContact = Contact::getById($cdata['public']); $apcontact = APContact::getByURL($publicContact['url'], false); - $account = Mastodon\Account::create($publicContact, $apcontact); + $account = Mastodon\Account::create($baseUrl, $publicContact, $apcontact); // Not ideal, the same "account" can have multiple ids depending on the context $account->id = $intro['id']; @@ -107,13 +119,11 @@ class FollowRequests extends Api $base_query['limit'] = $limit; } - $BaseURL = DI::baseUrl(); - $links = []; if ($count > $limit) { - $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"'; + $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['max_id' => $intros[count($intros) - 1]['id']]) . '>; rel="next"'; } - $links[] = '<' . $BaseURL->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"'; + $links[] = '<' . $baseUrl->get() . '/api/v1/follow_requests?' . http_build_query($base_query + ['since_id' => $intros[0]['id']]) . '>; rel="prev"'; header('Link: ' . implode(', ', $links));