From aa6192258ef77a85e92d66477a517efb7dfd85b5 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 2 Mar 2019 16:10:57 -0500 Subject: [PATCH 1/7] Extract API status retrieval from api_status_show() - Remove 'raw' type from api_status_show() - Add new api_get_status() and api_get_last_status() functions - Use api_get_last_status() in api_account_verify_credentials() and api_show_user() --- include/api.php | 217 +++++++++++++++++++++--------------------------- 1 file changed, 96 insertions(+), 121 deletions(-) diff --git a/include/api.php b/include/api.php index 239417ea9..766f97c54 100644 --- a/include/api.php +++ b/include/api.php @@ -932,7 +932,6 @@ function api_format_data($root_element, $type, $data) */ function api_account_verify_credentials($type) { - $a = \get_app(); if (api_user() === false) { @@ -954,13 +953,9 @@ function api_account_verify_credentials($type) // - Adding last status if (!$skip_status) { - $user_info["status"] = api_status_show("raw"); - if (isset($user_info["status"])) { - if (!is_array($user_info["status"]) || !count($user_info["status"])) { - unset($user_info["status"]); - } else { - unset($user_info["status"]["user"]); - } + $last_status = api_get_last_status($user_info['pid'], api_user()); + if ($last_status) { + $user_info['status'] = $last_status; } } @@ -1244,105 +1239,126 @@ function api_media_upload() api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST); /** - * * @param string $type Return type (atom, rss, xml, json) - * * @param int $item_id - * @return array|string + * @return string * @throws BadRequestException * @throws ImagickException * @throws InternalServerErrorException * @throws UnauthorizedException */ -function api_status_show($type, $item_id = 0) +function api_status_show($type, $item_id) { - $a = \get_app(); + Logger::info(API_LOG_PREFIX . 'Start', ['action' => 'status_show', 'type' => $type, 'item_id' => $item_id]); + + $a = \Friendica\BaseObject::getApp(); $user_info = api_get_user($a); - Logger::log('api_status_show: user_info: '.print_r($user_info, true), Logger::DEBUG); - - if (!empty($item_id)) { - // Get the item with the given id - $condition = ['id' => $item_id]; - } else { - // get last public wall message - $condition = ['owner-id' => $user_info['pid'], 'uid' => api_user(), - 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]]; - } - - if ($type == "raw") { - $condition['private'] = false; - } - - $lastwall = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]); - - if (DBA::isResult($lastwall)) { - $in_reply_to = api_in_reply_to($lastwall); - - $converted = api_convert_item($lastwall); - - if ($type == "xml") { - $geo = "georss:point"; - } else { - $geo = "geo"; + $status_info = api_get_status(['id' => $item_id]); + if ($status_info) { + if ($type == 'xml') { + $status_info['georss:point'] = $status_info['geo']; + unset($status_info['geo']); } + if ($status_info) { + $status_info['user'] = $user_info; + + // "uid" and "self" are only needed for some internal stuff, so remove it from here + unset($status_info['user']['uid']); + unset($status_info['user']['self']); + } + } + + Logger::info(API_LOG_PREFIX . 'End', ['action' => 'get_status', 'status_info' => $status_info]); + + return api_format_data('statuses', $type, ['status' => $status_info]); +} + +/** + * Retrieves the last public status of the provided user info + * + * @param int $ownerId Public contact Id + * @param int $uid User Id + * @return array + * @throws InternalServerErrorException + */ +function api_get_last_status($ownerId, $uid) +{ + $condition = [ + 'owner-id' => $ownerId, + 'uid' => $uid, + 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], + 'private' => false + ]; + + $status_info = api_get_status($condition); + + return $status_info; +} + +/** + * Retrieves a single item record based on the provided condition and converts it for API use. + * + * @param array $condition Item table condition array + * @return array + * @throws InternalServerErrorException + */ +function api_get_status(array $condition) +{ + $status_info = []; + + $item = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]); + if (DBA::isResult($item)) { + $in_reply_to = api_in_reply_to($item); + + $converted = api_convert_item($item); + $status_info = [ - 'created_at' => api_date($lastwall['created']), - 'id' => intval($lastwall['id']), - 'id_str' => (string) $lastwall['id'], - 'text' => $converted["text"], - 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'), + 'created_at' => api_date($item['created']), + 'id' => intval($item['id']), + 'id_str' => (string) $item['id'], + 'text' => $converted['text'], + 'source' => $item['app'] ?: 'web', 'truncated' => false, 'in_reply_to_status_id' => $in_reply_to['status_id'], 'in_reply_to_status_id_str' => $in_reply_to['status_id_str'], 'in_reply_to_user_id' => $in_reply_to['user_id'], 'in_reply_to_user_id_str' => $in_reply_to['user_id_str'], 'in_reply_to_screen_name' => $in_reply_to['screen_name'], - 'user' => $user_info, - $geo => null, + 'geo' => null, 'coordinates' => '', 'place' => '', 'contributors' => '', 'is_quote_status' => false, 'retweet_count' => 0, 'favorite_count' => 0, - 'favorited' => $lastwall['starred'] ? true : false, + 'favorited' => $item['starred'] ? true : false, 'retweeted' => false, 'possibly_sensitive' => false, 'lang' => '', - 'statusnet_html' => $converted["html"], - 'statusnet_conversation_id' => $lastwall['parent'], - 'external_url' => System::baseUrl() . '/display/' . $lastwall['guid'], + 'statusnet_html' => $converted['html'], + 'statusnet_conversation_id' => $item['parent'], + 'external_url' => System::baseUrl() . '/display/' . $item['guid'], ]; - if (count($converted["attachments"]) > 0) { - $status_info["attachments"] = $converted["attachments"]; + if (count($converted['attachments']) > 0) { + $status_info['attachments'] = $converted['attachments']; } - if (count($converted["entities"]) > 0) { - $status_info["entities"] = $converted["entities"]; + if (count($converted['entities']) > 0) { + $status_info['entities'] = $converted['entities']; } - if ($status_info["source"] == 'web') { - $status_info["source"] = ContactSelector::networkToName($lastwall['network'], $lastwall['author-link']); - } elseif (ContactSelector::networkToName($lastwall['network'], $lastwall['author-link']) != $status_info["source"]) { - $status_info["source"] = trim($status_info["source"].' ('.ContactSelector::networkToName($lastwall['network'], $lastwall['author-link']).')'); + if ($status_info['source'] == 'web') { + $status_info['source'] = ContactSelector::networkToName($item['network'], $item['author-link']); + } elseif (ContactSelector::networkToName($item['network'], $item['author-link']) != $status_info['source']) { + $status_info['source'] = trim($status_info['source'] . ' (' . ContactSelector::networkToName($item['network'], $item['author-link']) . ')'); } - - // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($status_info["user"]["uid"]); - unset($status_info["user"]["self"]); - - Logger::log('status_info: '.print_r($status_info, true), Logger::DEBUG); - - if ($type == "raw") { - return $status_info; - } - - return api_format_data("statuses", $type, ['status' => $status_info]); } + + return $status_info; } /** @@ -1359,66 +1375,25 @@ function api_status_show($type, $item_id = 0) */ function api_users_show($type) { - $a = \get_app(); + $a = \Friendica\BaseObject::getApp(); $user_info = api_get_user($a); - $condition = ['owner-id' => $user_info['pid'], 'uid' => api_user(), - 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => false]; - $lastwall = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]); - - if (DBA::isResult($lastwall)) { - $in_reply_to = api_in_reply_to($lastwall); - - $converted = api_convert_item($lastwall); - - if ($type == "xml") { - $geo = "georss:point"; - } else { - $geo = "geo"; + $lastItem = api_get_last_status($user_info['pid'], $user_info['uid']); + if ($lastItem) { + if ($type == 'xml') { + $lastItem['georss:point'] = $lastItem['geo']; + unset($lastItem['geo']); } - $user_info['status'] = [ - 'text' => $converted["text"], - 'truncated' => false, - 'created_at' => api_date($lastwall['created']), - 'in_reply_to_status_id' => $in_reply_to['status_id'], - 'in_reply_to_status_id_str' => $in_reply_to['status_id_str'], - 'source' => (($lastwall['app']) ? $lastwall['app'] : 'web'), - 'id' => intval($lastwall['contact-id']), - 'id_str' => (string) $lastwall['contact-id'], - 'in_reply_to_user_id' => $in_reply_to['user_id'], - 'in_reply_to_user_id_str' => $in_reply_to['user_id_str'], - 'in_reply_to_screen_name' => $in_reply_to['screen_name'], - $geo => null, - 'favorited' => $lastwall['starred'] ? true : false, - 'statusnet_html' => $converted["html"], - 'statusnet_conversation_id' => $lastwall['parent'], - 'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'], - ]; - - if (count($converted["attachments"]) > 0) { - $user_info["status"]["attachments"] = $converted["attachments"]; - } - - if (count($converted["entities"]) > 0) { - $user_info["status"]["entities"] = $converted["entities"]; - } - - if ($user_info["status"]["source"] == 'web') { - $user_info["status"]["source"] = ContactSelector::networkToName($lastwall['network'], $lastwall['author-link']); - } - - if (ContactSelector::networkToName($lastwall['network'], $user_info['url']) != $user_info["status"]["source"]) { - $user_info["status"]["source"] = trim($user_info["status"]["source"] . ' (' . ContactSelector::networkToName($lastwall['network'], $lastwall['author-link']) . ')'); - } + $user_info['status'] = $lastItem; } // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($user_info["uid"]); - unset($user_info["self"]); + unset($user_info['uid']); + unset($user_info['self']); - return api_format_data("user", $type, ['user' => $user_info]); + return api_format_data('user', $type, ['user' => $user_info]); } /// @TODO move to top of file or somewhere better From bb78768fb234b56086ce71400dab91f6e76d9b9f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 2 Mar 2019 16:11:56 -0500 Subject: [PATCH 2/7] Update tests about api_show_status() - Remove testApiStatusShowWithRaw() - Add testApiGetLastStatus() - Mark testApiStatusShowWithJson() and testApiStatusShowWithXml() as skipped as they require an item id --- tests/include/ApiTest.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 3d7e5bcfb..39f4ef79c 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -1274,8 +1274,10 @@ class ApiTest extends DatabaseTest * Test the api_status_show() function. * @return void */ - public function testApiStatusShow() + public function testApiStatusShowWithJson() { + $this->markTestSkipped('This test requires an item ID'); + $result = api_status_show('json'); $this->assertStatus($result['status']); } @@ -1286,6 +1288,8 @@ class ApiTest extends DatabaseTest */ public function testApiStatusShowWithXml() { + $this->markTestSkipped('This test requires an item ID'); + $result = api_status_show('xml'); $this->assertXml($result, 'statuses'); } @@ -1294,9 +1298,11 @@ class ApiTest extends DatabaseTest * Test the api_status_show() function with a raw result. * @return void */ - public function testApiStatusShowWithRaw() + public function testApiGetLastStatus() { - $this->assertStatus(api_status_show('raw')); + $user_info = api_get_user($this->app); + + $this->assertStatus(api_get_last_status($user_info['pid'], $user_info['uid'])); } /** From 9693a7472ee7eff93f5f04d5b7eef7722006f08f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 2 Mar 2019 17:03:13 -0500 Subject: [PATCH 3/7] Extract API status formatting from api_get_status() - Rename api_get_status() to api_get_item() - Ad new api_format_item() function for single item formatting --- include/api.php | 323 +++++++++++++++++++++--------------------------- 1 file changed, 141 insertions(+), 182 deletions(-) diff --git a/include/api.php b/include/api.php index 766f97c54..292068b51 100644 --- a/include/api.php +++ b/include/api.php @@ -953,7 +953,7 @@ function api_account_verify_credentials($type) // - Adding last status if (!$skip_status) { - $last_status = api_get_last_status($user_info['pid'], api_user()); + $last_status = api_get_last_status($user_info['pid'], $user_info['uid'], $type); if ($last_status) { $user_info['status'] = $last_status; } @@ -1239,36 +1239,20 @@ function api_media_upload() api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST); /** - * @param string $type Return type (atom, rss, xml, json) + * @param string $type Return format (atom, rss, xml, json) * @param int $item_id * @return string - * @throws BadRequestException - * @throws ImagickException - * @throws InternalServerErrorException - * @throws UnauthorizedException + * @throws Exception */ function api_status_show($type, $item_id) { Logger::info(API_LOG_PREFIX . 'Start', ['action' => 'status_show', 'type' => $type, 'item_id' => $item_id]); - $a = \Friendica\BaseObject::getApp(); + $status_info = []; - $user_info = api_get_user($a); - - $status_info = api_get_status(['id' => $item_id]); - if ($status_info) { - if ($type == 'xml') { - $status_info['georss:point'] = $status_info['geo']; - unset($status_info['geo']); - } - - if ($status_info) { - $status_info['user'] = $user_info; - - // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($status_info['user']['uid']); - unset($status_info['user']['self']); - } + $item = api_get_item(['id' => $item_id]); + if ($item) { + $status_info = api_format_item($item, $type); } Logger::info(API_LOG_PREFIX . 'End', ['action' => 'get_status', 'status_info' => $status_info]); @@ -1279,12 +1263,13 @@ function api_status_show($type, $item_id) /** * Retrieves the last public status of the provided user info * - * @param int $ownerId Public contact Id - * @param int $uid User Id + * @param int $ownerId Public contact Id + * @param int $uid User Id + * @param string $type Return format (atom, rss, xml, json) * @return array - * @throws InternalServerErrorException + * @throws Exception */ -function api_get_last_status($ownerId, $uid) +function api_get_last_status($ownerId, $uid, $type = 'json') { $condition = [ 'owner-id' => $ownerId, @@ -1293,7 +1278,9 @@ function api_get_last_status($ownerId, $uid) 'private' => false ]; - $status_info = api_get_status($condition); + $item = api_get_item($condition); + + $status_info = api_format_item($item, $type); return $status_info; } @@ -1303,62 +1290,13 @@ function api_get_last_status($ownerId, $uid) * * @param array $condition Item table condition array * @return array - * @throws InternalServerErrorException + * @throws Exception */ -function api_get_status(array $condition) +function api_get_item(array $condition) { - $status_info = []; - $item = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]); - if (DBA::isResult($item)) { - $in_reply_to = api_in_reply_to($item); - $converted = api_convert_item($item); - - $status_info = [ - 'created_at' => api_date($item['created']), - 'id' => intval($item['id']), - 'id_str' => (string) $item['id'], - 'text' => $converted['text'], - 'source' => $item['app'] ?: 'web', - 'truncated' => false, - 'in_reply_to_status_id' => $in_reply_to['status_id'], - 'in_reply_to_status_id_str' => $in_reply_to['status_id_str'], - 'in_reply_to_user_id' => $in_reply_to['user_id'], - 'in_reply_to_user_id_str' => $in_reply_to['user_id_str'], - 'in_reply_to_screen_name' => $in_reply_to['screen_name'], - 'geo' => null, - 'coordinates' => '', - 'place' => '', - 'contributors' => '', - 'is_quote_status' => false, - 'retweet_count' => 0, - 'favorite_count' => 0, - 'favorited' => $item['starred'] ? true : false, - 'retweeted' => false, - 'possibly_sensitive' => false, - 'lang' => '', - 'statusnet_html' => $converted['html'], - 'statusnet_conversation_id' => $item['parent'], - 'external_url' => System::baseUrl() . '/display/' . $item['guid'], - ]; - - if (count($converted['attachments']) > 0) { - $status_info['attachments'] = $converted['attachments']; - } - - if (count($converted['entities']) > 0) { - $status_info['entities'] = $converted['entities']; - } - - if ($status_info['source'] == 'web') { - $status_info['source'] = ContactSelector::networkToName($item['network'], $item['author-link']); - } elseif (ContactSelector::networkToName($item['network'], $item['author-link']) != $status_info['source']) { - $status_info['source'] = trim($status_info['source'] . ' (' . ContactSelector::networkToName($item['network'], $item['author-link']) . ')'); - } - } - - return $status_info; + return $item; } /** @@ -1379,14 +1317,9 @@ function api_users_show($type) $user_info = api_get_user($a); - $lastItem = api_get_last_status($user_info['pid'], $user_info['uid']); - if ($lastItem) { - if ($type == 'xml') { - $lastItem['georss:point'] = $lastItem['geo']; - unset($lastItem['geo']); - } - - $user_info['status'] = $lastItem; + $lastStatus = api_get_last_status($user_info['pid'], $user_info['uid'], $type); + if ($lastStatus) { + $user_info['status'] = $lastStatus; } // "uid" and "self" are only needed for some internal stuff, so remove it from here @@ -2947,7 +2880,7 @@ function api_format_items_profiles($profile_row) /** * @brief format items to be returned by api * - * @param array $r array of items + * @param array $items array of items * @param array $user_info * @param bool $filter_user filter items by $user_info * @param string $type Return type (atom, rss, xml, json) @@ -2957,14 +2890,13 @@ function api_format_items_profiles($profile_row) * @throws InternalServerErrorException * @throws UnauthorizedException */ -function api_format_items($r, $user_info, $filter_user = false, $type = "json") +function api_format_items($items, $user_info, $filter_user = false, $type = "json") { - $a = \get_app(); + $a = \Friendica\BaseObject::getApp(); $ret = []; - foreach ((array)$r as $item) { - localize_item($item); + foreach ((array)$items as $item) { list($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 @@ -2972,100 +2904,127 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json") continue; } - $in_reply_to = api_in_reply_to($item); + $status = api_format_item($item, $type, $status_user, $owner_user); - $converted = api_convert_item($item); - - if ($type == "xml") { - $geo = "georss:point"; - } else { - $geo = "geo"; - } - - $status = [ - 'text' => $converted["text"], - 'truncated' => false, - 'created_at'=> api_date($item['created']), - 'in_reply_to_status_id' => $in_reply_to['status_id'], - 'in_reply_to_status_id_str' => $in_reply_to['status_id_str'], - 'source' => (($item['app']) ? $item['app'] : 'web'), - 'id' => intval($item['id']), - 'id_str' => (string) intval($item['id']), - 'in_reply_to_user_id' => $in_reply_to['user_id'], - 'in_reply_to_user_id_str' => $in_reply_to['user_id_str'], - 'in_reply_to_screen_name' => $in_reply_to['screen_name'], - $geo => null, - 'favorited' => $item['starred'] ? true : false, - 'user' => $status_user, - 'friendica_author' => $author_user, - 'friendica_owner' => $owner_user, - 'friendica_private' => $item['private'] == 1, - //'entities' => NULL, - 'statusnet_html' => $converted["html"], - 'statusnet_conversation_id' => $item['parent'], - 'external_url' => System::baseUrl() . "/display/" . $item['guid'], - 'friendica_activities' => api_format_items_activities($item, $type), - ]; - - if (count($converted["attachments"]) > 0) { - $status["attachments"] = $converted["attachments"]; - } - - if (count($converted["entities"]) > 0) { - $status["entities"] = $converted["entities"]; - } - - if ($status["source"] == 'web') { - $status["source"] = ContactSelector::networkToName($item['network'], $item['author-link']); - } elseif (ContactSelector::networkToName($item['network'], $item['author-link']) != $status["source"]) { - $status["source"] = trim($status["source"].' ('.ContactSelector::networkToName($item['network'], $item['author-link']).')'); - } - - if ($item["id"] == $item["parent"]) { - $retweeted_item = api_share_as_retweet($item); - if ($retweeted_item !== false) { - $retweeted_status = $status; - $status['user'] = $status['friendica_owner']; - try { - $retweeted_status["user"] = api_get_user($a, $retweeted_item["author-id"]); - } catch (BadRequestException $e) { - // user not found. should be found? - /// @todo check if the user should be always found - $retweeted_status["user"] = []; - } - - $rt_converted = api_convert_item($retweeted_item); - - $retweeted_status['text'] = $rt_converted["text"]; - $retweeted_status['statusnet_html'] = $rt_converted["html"]; - $retweeted_status['friendica_activities'] = api_format_items_activities($retweeted_item, $type); - $retweeted_status['created_at'] = api_date($retweeted_item['created']); - $status['retweeted_status'] = $retweeted_status; - $status['friendica_author'] = $retweeted_status['friendica_author']; - } - } - - // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($status["user"]["uid"]); - unset($status["user"]["self"]); - - if ($item["coord"] != "") { - $coords = explode(' ', $item["coord"]); - if (count($coords) == 2) { - if ($type == "json") { - $status["geo"] = ['type' => 'Point', - 'coordinates' => [(float) $coords[0], - (float) $coords[1]]]; - } else {// Not sure if this is the official format - if someone founds a documentation we can check - $status["georss:point"] = $item["coord"]; - } - } - } $ret[] = $status; - }; + } + return $ret; } +/** + * @param array $item Item record + * @param string $type Return format (atom, rss, xml, json) + * @param array $status_user User record of the item author, can be provided by api_item_get_user() + * @param array $owner_user User record of the item owner, can be provided by api_item_get_user() + * @return array API-formatted status + * @throws BadRequestException + * @throws ImagickException + * @throws InternalServerErrorException + * @throws UnauthorizedException + */ +function api_format_item($item, $type = "json", $status_user = null, $owner_user = null) +{ + $a = \Friendica\BaseObject::getApp(); + + if (empty($status_user) || empty($owner_user)) { + list($status_user, $owner_user) = api_item_get_user($a, $item); + } + + localize_item($item); + + $in_reply_to = api_in_reply_to($item); + + $converted = api_convert_item($item); + + if ($type == "xml") { + $geo = "georss:point"; + } else { + $geo = "geo"; + } + + $status = [ + 'text' => $converted["text"], + 'truncated' => false, + 'created_at'=> api_date($item['created']), + 'in_reply_to_status_id' => $in_reply_to['status_id'], + 'in_reply_to_status_id_str' => $in_reply_to['status_id_str'], + 'source' => (($item['app']) ? $item['app'] : 'web'), + 'id' => intval($item['id']), + 'id_str' => (string) intval($item['id']), + 'in_reply_to_user_id' => $in_reply_to['user_id'], + 'in_reply_to_user_id_str' => $in_reply_to['user_id_str'], + 'in_reply_to_screen_name' => $in_reply_to['screen_name'], + $geo => null, + 'favorited' => $item['starred'] ? true : false, + 'user' => $status_user, + 'friendica_author' => $author_user, + 'friendica_owner' => $owner_user, + 'friendica_private' => $item['private'] == 1, + //'entities' => NULL, + 'statusnet_html' => $converted["html"], + 'statusnet_conversation_id' => $item['parent'], + 'external_url' => System::baseUrl() . "/display/" . $item['guid'], + 'friendica_activities' => api_format_items_activities($item, $type), + ]; + + if (count($converted["attachments"]) > 0) { + $status["attachments"] = $converted["attachments"]; + } + + if (count($converted["entities"]) > 0) { + $status["entities"] = $converted["entities"]; + } + + if ($status["source"] == 'web') { + $status["source"] = ContactSelector::networkToName($item['network'], $item['author-link']); + } elseif (ContactSelector::networkToName($item['network'], $item['author-link']) != $status["source"]) { + $status["source"] = trim($status["source"].' ('.ContactSelector::networkToName($item['network'], $item['author-link']).')'); + } + + if ($item["id"] == $item["parent"]) { + $retweeted_item = api_share_as_retweet($item); + if ($retweeted_item !== false) { + $retweeted_status = $status; + $status['user'] = $status['friendica_owner']; + try { + $retweeted_status["user"] = api_get_user($a, $retweeted_item["author-id"]); + } catch (BadRequestException $e) { + // user not found. should be found? + /// @todo check if the user should be always found + $retweeted_status["user"] = []; + } + + $rt_converted = api_convert_item($retweeted_item); + + $retweeted_status['text'] = $rt_converted["text"]; + $retweeted_status['statusnet_html'] = $rt_converted["html"]; + $retweeted_status['friendica_activities'] = api_format_items_activities($retweeted_item, $type); + $retweeted_status['created_at'] = api_date($retweeted_item['created']); + $status['retweeted_status'] = $retweeted_status; + $status['friendica_author'] = $retweeted_status['friendica_author'];} + } + + // "uid" and "self" are only needed for some internal stuff, so remove it from here + unset($status["user"]["uid"]); + unset($status["user"]["self"]); + + if ($item["coord"] != "") { + $coords = explode(' ', $item["coord"]); + if (count($coords) == 2) { + if ($type == "json") { + $status["geo"] = ['type' => 'Point', + 'coordinates' => [(float) $coords[0], + (float) $coords[1]]]; + } else {// Not sure if this is the official format - if someone founds a documentation we can check + $status["georss:point"] = $item["coord"]; + } + } + } + + return $status; +} + /** * Returns the remaining number of API requests available to the user before the API limit is reached. * From 8d28f199a639851d0dbc6725b105b87ecb50c19f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Mon, 4 Mar 2019 05:59:53 -0500 Subject: [PATCH 4/7] Test fix: Add expected author-network field to api_get_item() result --- include/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/api.php b/include/api.php index 292068b51..841ed1a71 100644 --- a/include/api.php +++ b/include/api.php @@ -1294,7 +1294,7 @@ function api_get_last_status($ownerId, $uid, $type = 'json') */ function api_get_item(array $condition) { - $item = Item::selectFirst(Item::ITEM_FIELDLIST, $condition, ['order' => ['id' => true]]); + $item = Item::selectFirst(Item::DISPLAY_FIELDLIST, $condition, ['order' => ['id' => true]]); return $item; } From e60e7a56ac61d2aa6d55abccfa6898807e16809d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 23 Mar 2019 09:59:52 -0400 Subject: [PATCH 5/7] Add author_user support to api_format_item --- include/api.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/api.php b/include/api.php index 841ed1a71..1bfc3f9d0 100644 --- a/include/api.php +++ b/include/api.php @@ -2904,7 +2904,7 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso continue; } - $status = api_format_item($item, $type, $status_user, $owner_user); + $status = api_format_item($item, $type, $status_user, $author_user, $owner_user); $ret[] = $status; } @@ -2916,6 +2916,7 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso * @param array $item Item record * @param string $type Return format (atom, rss, xml, json) * @param array $status_user User record of the item author, can be provided by api_item_get_user() + * @param array $author_user User record of the item author, can be provided by api_item_get_user() * @param array $owner_user User record of the item owner, can be provided by api_item_get_user() * @return array API-formatted status * @throws BadRequestException @@ -2923,12 +2924,12 @@ 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, $owner_user = null) +function api_format_item($item, $type = "json", $status_user = null, $author_user = null, $owner_user = null) { $a = \Friendica\BaseObject::getApp(); - if (empty($status_user) || empty($owner_user)) { - list($status_user, $owner_user) = api_item_get_user($a, $item); + if (empty($status_user) || empty($author_user) || empty($owner_user)) { + list($status_user, $author_user, $owner_user) = api_item_get_user($a, $item); } localize_item($item); @@ -2959,7 +2960,7 @@ function api_format_item($item, $type = "json", $status_user = null, $owner_user 'favorited' => $item['starred'] ? true : false, 'user' => $status_user, 'friendica_author' => $author_user, - 'friendica_owner' => $owner_user, + 'friendica_owner' => $owner_user, 'friendica_private' => $item['private'] == 1, //'entities' => NULL, 'statusnet_html' => $converted["html"], From ab7ec357cb932c62a8ab613e774425ce0cdf580d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 23 Mar 2019 10:01:25 -0400 Subject: [PATCH 6/7] Remove type parameter from api_get_last_status function prototype --- include/api.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/include/api.php b/include/api.php index 1bfc3f9d0..7bdab84d2 100644 --- a/include/api.php +++ b/include/api.php @@ -953,9 +953,9 @@ function api_account_verify_credentials($type) // - Adding last status if (!$skip_status) { - $last_status = api_get_last_status($user_info['pid'], $user_info['uid'], $type); - if ($last_status) { - $user_info['status'] = $last_status; + $item = api_get_last_status($user_info['pid'], $user_info['uid']); + if ($item) { + $user_info['status'] = api_format_item($item, $type); } } @@ -1265,11 +1265,10 @@ function api_status_show($type, $item_id) * * @param int $ownerId Public contact Id * @param int $uid User Id - * @param string $type Return format (atom, rss, xml, json) * @return array * @throws Exception */ -function api_get_last_status($ownerId, $uid, $type = 'json') +function api_get_last_status($ownerId, $uid) { $condition = [ 'owner-id' => $ownerId, @@ -1280,9 +1279,7 @@ function api_get_last_status($ownerId, $uid, $type = 'json') $item = api_get_item($condition); - $status_info = api_format_item($item, $type); - - return $status_info; + return $item; } /** @@ -1317,9 +1314,9 @@ function api_users_show($type) $user_info = api_get_user($a); - $lastStatus = api_get_last_status($user_info['pid'], $user_info['uid'], $type); - if ($lastStatus) { - $user_info['status'] = $lastStatus; + $item = api_get_last_status($user_info['pid'], $user_info['uid']); + if ($item) { + $user_info['status'] = api_format_item($item, $type); } // "uid" and "self" are only needed for some internal stuff, so remove it from here From fb13d0c31bd656975273909cb9c421642a81764a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 23 Mar 2019 10:02:32 -0400 Subject: [PATCH 7/7] Add tests for api_status_show - Update test for api_get_last_status() - Add test class properties declaration --- tests/include/ApiTest.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index 39f4ef79c..df3a7e940 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -31,6 +31,18 @@ class ApiTest extends DatabaseTest */ protected $logOutput; + /** @var App */ + protected $app; + + /** @var array */ + protected $selfUser; + /** @var array */ + protected $friendUser; + /** @var array */ + protected $otherUser; + + protected $wrongUserId; + /** * Create variables used by tests. */ @@ -1272,37 +1284,30 @@ class ApiTest extends DatabaseTest /** * Test the api_status_show() function. - * @return void */ public function testApiStatusShowWithJson() { - $this->markTestSkipped('This test requires an item ID'); - - $result = api_status_show('json'); + $result = api_status_show('json', 1); $this->assertStatus($result['status']); } /** * Test the api_status_show() function with an XML result. - * @return void */ public function testApiStatusShowWithXml() { - $this->markTestSkipped('This test requires an item ID'); - - $result = api_status_show('xml'); + $result = api_status_show('xml', 1); $this->assertXml($result, 'statuses'); } /** - * Test the api_status_show() function with a raw result. - * @return void + * Test the api_get_last_status() function */ public function testApiGetLastStatus() { - $user_info = api_get_user($this->app); + $item = api_get_last_status($this->selfUser['id'], $this->selfUser['id']); - $this->assertStatus(api_get_last_status($user_info['pid'], $user_info['uid'])); + $this->assertNotNull($item); } /**