From d576e920d5c0f02386b51c514bb82c6bbc136875 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 19 Nov 2021 20:15:12 +0000 Subject: [PATCH] Fix legacy API --- include/api.php | 52 ++++-------------------------- src/Module/Api/Friendica/Index.php | 1 + tests/legacy/ApiTest.php | 23 ------------- 3 files changed, 8 insertions(+), 68 deletions(-) diff --git a/include/api.php b/include/api.php index d1ff1458ed..a442b1e89a 100644 --- a/include/api.php +++ b/include/api.php @@ -221,6 +221,7 @@ function api_call(App $a, App\Arguments $args = null) Logger::warning(API_LOG_PREFIX . 'not implemented', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString()]); throw new NotFoundException(); } catch (HTTPException $e) { + Logger::notice(API_LOG_PREFIX . 'got exception', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString(), 'error' => $e]); DI::apiResponse()->error($e->getCode(), $e->getDescription(), $e->getMessage(), $type); } } @@ -282,40 +283,6 @@ function api_unique_id_to_nurl($id) } } -/** - * return api-formatted array for item's author and owner - * - * @param App $a App - * @param array $item item from db - * @return array(array:author, array:owner) - * @throws BadRequestException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException - */ -function api_item_get_user(App $a, $item) -{ - if (empty($item['author-id'])) { - $item['author-id'] = Contact::getPublicIdByUserId(BaseApi::getCurrentUserID()); - } - $status_user = DI::twitterUser()->createFromContactId($item['author-id'], BaseApi::getCurrentUserID())->toArray(); - - $author_user = $status_user; - - $status_user["protected"] = isset($item['private']) && ($item['private'] == Item::PRIVATE); - - if (($item['thr-parent'] ?? '') == ($item['uri'] ?? '')) { - if (empty($item['owner-id'])) { - $item['owner-id'] = Contact::getPublicIdByUserId(BaseApi::getCurrentUserID()); - } - $owner_user = DI::twitterUser()->createFromContactId($item['owner-id'], BaseApi::getCurrentUserID())->toArray(); - } else { - $owner_user = $author_user; - } - - return ([$status_user, $author_user, $owner_user]); -} - /** * TWITTER API */ @@ -2298,14 +2265,12 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso } foreach ((array)$items as $item) { - [$status_user, $author_user, $owner_user] = api_item_get_user($a, $item); - // Look if the posts are matching if they should be filtered by user id - if ($filter_user && ($status_user["id"] != $user_info["id"])) { + if ($filter_user && ($item["author-id"] != $user_info["id"])) { continue; } - $status = api_format_item($item, $type, $status_user, $author_user, $owner_user); + $status = api_format_item($item, $type); $ret[] = $status; } @@ -2325,13 +2290,10 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso * @throws InternalServerErrorException * @throws UnauthorizedException */ -function api_format_item($item, $type = "json", $status_user = null, $author_user = null, $owner_user = null) +function api_format_item($item, $type = "json") { - $a = DI::app(); - - if (empty($status_user) || empty($author_user) || empty($owner_user)) { - [$status_user, $author_user, $owner_user] = api_item_get_user($a, $item); - } + $author_user = DI::twitterUser()->createFromContactId($item['author-id'], BaseApi::getCurrentUserID())->toArray(); + $owner_user = DI::twitterUser()->createFromContactId($item['owner-id'], BaseApi::getCurrentUserID())->toArray(); DI::contentItem()->localize($item); @@ -2359,7 +2321,7 @@ function api_format_item($item, $type = "json", $status_user = null, $author_use 'in_reply_to_screen_name' => $in_reply_to['screen_name'], $geo => null, 'favorited' => $item['starred'] ? true : false, - 'user' => $status_user, + 'user' => $author_user, 'friendica_author' => $author_user, 'friendica_owner' => $owner_user, 'friendica_private' => $item['private'] == Item::PRIVATE, diff --git a/src/Module/Api/Friendica/Index.php b/src/Module/Api/Friendica/Index.php index dbe45f545f..5806f2e481 100644 --- a/src/Module/Api/Friendica/Index.php +++ b/src/Module/Api/Friendica/Index.php @@ -23,6 +23,7 @@ namespace Friendica\Module\Api\Friendica; use Friendica\DI; use Friendica\Module\BaseApi; +require_once __DIR__ . '/../../../../include/api.php'; /** * api/friendica diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 13509bf343..490fac7dd5 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -763,29 +763,6 @@ class ApiTest extends FixtureTest self::assertSelfUser(DI::twitterUser()->createFromUserId(BaseApi::getCurrentUserID())->toArray()); } - /** - * Test the api_item_get_user() function. - * - * @return void - */ - public function testApiItemGetUser() - { - $users = api_item_get_user($this->app, []); - self::assertSelfUser($users[0]); - } - - /** - * Test the api_item_get_user() function with a different item parent. - * - * @return void - */ - public function testApiItemGetUserWithDifferentParent() - { - $users = api_item_get_user($this->app, ['thr-parent' => 'item_parent', 'uri' => 'item_uri']); - self::assertSelfUser($users[0]); - self::assertEquals($users[0], $users[1]); - } - /** * Test the Arrays::walkRecursive() function. *