From ecde6e456da2b30ca4cf0413e8162719ac512439 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 16 Dec 2017 10:16:25 -0500 Subject: [PATCH 1/3] Add pagination to API statuses/friends and statuses/followers - Add doc blocks for both API calls - Remove unused parameters --- include/api.php | 61 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/include/api.php b/include/api.php index 52910d4727..9918651952 100644 --- a/include/api.php +++ b/include/api.php @@ -486,9 +486,8 @@ function api_unique_id_to_url($id) * * @param object $a App * @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; @@ -2901,11 +2900,16 @@ function api_lists_list($type) api_register_func('api/lists/list', 'api_lists_list', true); /** - * https://dev.twitter.com/docs/api/1/get/statuses/friends - * This function is deprecated by Twitter - * returns: json, xml + * @brief Returns either the friends of the follower list + * + * 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(); @@ -2913,9 +2917,17 @@ function api_statuses_f($type, $qtype) 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); - if (x($_GET, 'cursor') && $_GET['cursor']=='undefined') { + if (x($_GET, 'cursor') && $_GET['cursor'] == 'undefined') { /* this is to stop Hotot to load friends multiple times * I'm not sure if I'm missing return something or * is a bug in hotot. Workaround, meantime @@ -2939,8 +2951,17 @@ function api_statuses_f($type, $qtype) } $r = q( - "SELECT `nurl` FROM `contact` WHERE `uid` = %d AND NOT `self` AND (NOT `blocked` OR `pending`) $sql_extra ORDER BY `nick`", - intval(api_user()) + "SELECT `nurl` + 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(); @@ -2956,21 +2977,37 @@ function api_statuses_f($type, $qtype) } 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) { - $data = api_statuses_f($type, "friends"); + $data = api_statuses_f("friends"); if ($data === false) { return false; } 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) { - $data = api_statuses_f($type, "followers"); + $data = api_statuses_f("followers"); if ($data === false) { return false; } From 78c053c6b9289ca67decea7cc56d49c072b7c9b6 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 16 Dec 2017 11:34:44 -0500 Subject: [PATCH 2/3] Add max_id support for statuses/user_timeline API - Add documentation - Improve formatting --- include/api.php | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/include/api.php b/include/api.php index 52910d4727..2501fd2375 100644 --- a/include/api.php +++ b/include/api.php @@ -2019,6 +2019,13 @@ function api_statuses_mentions($type) api_register_func('api/statuses/mentions', 'api_statuses_mentions', true); api_register_func('api/statuses/replies', 'api_statuses_mentions', true); +/** + * @brief Returns a user's public timeline + * + * @param string $type Either "json" or "xml" + * @return string|array + * @throws ForbiddenException + */ function api_statuses_user_timeline($type) { $a = get_app(); @@ -2028,7 +2035,6 @@ function api_statuses_user_timeline($type) } $user_info = api_get_user($a); - // get last network messages logger( "api_statuses_user_timeline: api_user: ". api_user() . @@ -2037,18 +2043,18 @@ function api_statuses_user_timeline($type) LOGGER_DEBUG ); - // params - $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20); - $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] -1 : 0); - if ($page < 0) { - $page = 0; - } - $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0); - //$since_id = 0;//$since_id = (x($_REQUEST, 'since_id')?$_REQUEST['since_id'] : 0); - $exclude_replies = (x($_REQUEST, 'exclude_replies') ? 1 : 0); - $conversation_id = (x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0); + $since_id = x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0; + $max_id = x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0; + $exclude_replies = x($_REQUEST, 'exclude_replies') ? 1 : 0; + $conversation_id = x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0; - $start = $page * $count; + // pagination + $count = x($_REQUEST, 'count') ? $_REQUEST['count'] : 20; + $page = x($_REQUEST, 'page') ? $_REQUEST['page'] : 1; + if ($page < 1) { + $page = 1; + } + $start = ($page - 1) * $count; $sql_extra = ''; if ($user_info['self'] == 1) { @@ -2058,10 +2064,15 @@ function api_statuses_user_timeline($type) if ($exclude_replies > 0) { $sql_extra .= ' AND `item`.`parent` = `item`.`id`'; } + if ($conversation_id > 0) { $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id); } + if ($max_id > 0) { + $sql_extra .= ' AND `item`.`id` <= ' . intval($max_id); + } + $r = q( "SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, @@ -2074,7 +2085,7 @@ function api_statuses_user_timeline($type) AND `item`.`contact-id` = %d AND `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted` $sql_extra - AND `item`.`id`>%d + AND `item`.`id` > %d ORDER BY `item`.`id` DESC LIMIT %d ,%d ", intval(api_user()), dbesc(ACTIVITY_POST), From 0dbf7a2611a7c2471617b614903411876b9f3c06 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 16 Dec 2017 11:50:58 -0500 Subject: [PATCH 3/3] Remove lingering reference to group_member.uid --- src/Model/Group.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Model/Group.php b/src/Model/Group.php index 5428271510..26bab7f45b 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -57,15 +57,12 @@ class Group extends BaseObject /** * @brief Get a list of group ids a contact belongs to * - * @todo Get rid of $uid, the contact id already bears the information - * - * @param int $uid * @param int $cid * @return array */ - private static function getByContactIdForUserId($uid, $cid) + private static function getIdsByContactId($cid) { - $condition = ['uid' => $uid, 'contact-id' => $cid]; + $condition = ['contact-id' => $cid]; $stmt = dba::select('group_member', ['gid'], $condition); $return = []; @@ -369,7 +366,7 @@ class Group extends BaseObject $member_of = array(); if ($cid) { - $member_of = self::getByContactIdForUserId(local_user(), $cid); + $member_of = self::getIdsByContactId($cid); } while ($group = dba::fetch($stmt)) {