Merge pull request #4079 from MrPetovan/task/4069-add-pagination-to-statuses-api

Add pagination to API statuses/friends and statuses/followers
This commit is contained in:
Michael Vogel 2017-12-17 11:31:01 +01:00 committed by GitHub
commit de56a3a824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 12 deletions

View File

@ -486,9 +486,8 @@ function api_unique_id_to_url($id)
* *
* @param object $a App * @param object $a App
* @param int|string $contact_id Contact ID or URL * @param int|string $contact_id Contact ID or URL
* @param string $type Return type (for errors)
*/ */
function api_get_user(App $a, $contact_id = null, $type = "json") function api_get_user(App $a, $contact_id = null)
{ {
global $called_api; global $called_api;
@ -2912,11 +2911,16 @@ function api_lists_list($type)
api_register_func('api/lists/list', 'api_lists_list', true); api_register_func('api/lists/list', 'api_lists_list', true);
/** /**
* https://dev.twitter.com/docs/api/1/get/statuses/friends * @brief Returns either the friends of the follower list
* This function is deprecated by Twitter *
* returns: json, xml * Note: Considers friends and followers lists to be private and won't return
* anything if any user_id parameter is passed.
*
* @param string $qtype Either "friends" or "followers"
* @return boolean|array
* @throws ForbiddenException
*/ */
function api_statuses_f($type, $qtype) function api_statuses_f($qtype)
{ {
$a = get_app(); $a = get_app();
@ -2924,9 +2928,17 @@ function api_statuses_f($type, $qtype)
throw new ForbiddenException(); throw new ForbiddenException();
} }
// pagination
$count = x($_GET, 'count') ? $_GET['count'] : 20;
$page = x($_GET, 'page') ? $_GET['page'] : 1;
if ($page < 1) {
$page = 1;
}
$start = ($page - 1) * $count;
$user_info = api_get_user($a); $user_info = api_get_user($a);
if (x($_GET, 'cursor') && $_GET['cursor']=='undefined') { if (x($_GET, 'cursor') && $_GET['cursor'] == 'undefined') {
/* this is to stop Hotot to load friends multiple times /* this is to stop Hotot to load friends multiple times
* I'm not sure if I'm missing return something or * I'm not sure if I'm missing return something or
* is a bug in hotot. Workaround, meantime * is a bug in hotot. Workaround, meantime
@ -2950,8 +2962,17 @@ function api_statuses_f($type, $qtype)
} }
$r = q( $r = q(
"SELECT `nurl` FROM `contact` WHERE `uid` = %d AND NOT `self` AND (NOT `blocked` OR `pending`) $sql_extra ORDER BY `nick`", "SELECT `nurl`
intval(api_user()) FROM `contact`
WHERE `uid` = %d
AND NOT `self`
AND (NOT `blocked` OR `pending`)
$sql_extra
ORDER BY `nick`
LIMIT %d, %d",
intval(api_user()),
intval($start),
intval($count)
); );
$ret = array(); $ret = array();
@ -2967,21 +2988,37 @@ function api_statuses_f($type, $qtype)
} }
return array('user' => $ret); return array('user' => $ret);
} }
/**
* @brief Returns the list of friends of the provided user
*
* @deprecated By Twitter API in favor of friends/list
*
* @param string $type Either "json" or "xml"
* @return boolean|string|array
*/
function api_statuses_friends($type) function api_statuses_friends($type)
{ {
$data = api_statuses_f($type, "friends"); $data = api_statuses_f("friends");
if ($data === false) { if ($data === false) {
return false; return false;
} }
return api_format_data("users", $type, $data); return api_format_data("users", $type, $data);
} }
/**
* @brief Returns the list of friends of the provided user
*
* @deprecated By Twitter API in favor of friends/list
*
* @param string $type Either "json" or "xml"
* @return boolean|string|array
*/
function api_statuses_followers($type) function api_statuses_followers($type)
{ {
$data = api_statuses_f($type, "followers"); $data = api_statuses_f("followers");
if ($data === false) { if ($data === false) {
return false; return false;
} }