Fix legacy API

This commit is contained in:
Michael 2021-11-19 20:15:12 +00:00
parent 942001b04d
commit d576e920d5
3 changed files with 8 additions and 68 deletions

View File

@ -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,

View File

@ -23,6 +23,7 @@ namespace Friendica\Module\Api\Friendica;
use Friendica\DI;
use Friendica\Module\BaseApi;
require_once __DIR__ . '/../../../../include/api.php';
/**
* api/friendica

View File

@ -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.
*