Add support for max_id parameter in new Twitter contact API endpoints

- Use filter_input instead of manual type casting
This commit is contained in:
Hypolite Petovan 2020-05-10 10:24:35 -04:00
parent 7a5afc10bb
commit 8111ede2e5
6 changed files with 112 additions and 50 deletions

View file

@ -157,11 +157,13 @@ These endpoints use the [Friendica API entities](help/API-Entities).
- [GET api/followers/list](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list) - [GET api/followers/list](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list)
- [GET api/friends/ids](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids) - [GET api/friends/ids](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids)
- [GET api/friends/list](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-list) - [GET api/friends/list](https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-list)
- Additional parameter: - Additional parameters:
- `since_id`: Same behavior as `cursor`, use the `next_cursor` value to load the next page. - `since_id`: You can use the `next_cursor` value to load the next page.
- `max_id`: You can use the inverse of the `previous_cursor` value to load the previous page.
- Unsupported parameter: - Unsupported parameter:
- `skip_status`: No status is returned even if it isn't set to true. - `skip_status`: No status is returned even if it isn't set to true.
- Caveats: - Caveats:
- `cursor` trumps `since_id` trumps `max_id` if any combination is provided.
- `user_id` must be the ID of a contact associated with a local user account. - `user_id` must be the ID of a contact associated with a local user account.
- `screen_name` must be associated with a local user account. - `screen_name` must be associated with a local user account.
- `screen_name` trumps `user_id` if both are provided (undocumented Twitter behavior). - `screen_name` trumps `user_id` if both are provided (undocumented Twitter behavior).

View file

@ -114,8 +114,6 @@ abstract class ContactEndpoint extends BaseApi
'total_count' => $return['total_count'], 'total_count' => $return['total_count'],
]; ];
return $return; return $return;
} }
@ -140,28 +138,33 @@ abstract class ContactEndpoint extends BaseApi
$hide_friends = (bool)$profile['hide-friends']; $hide_friends = (bool)$profile['hide-friends'];
} }
$condition = DBA::collapseCondition([
'rel' => $rel,
'uid' => $uid,
'self' => false,
'deleted' => false,
'hidden' => false,
'archive' => false,
'pending' => false
]);
if ($cursor !== -1) {
$condition[0] .= " AND `id` > ?";
$condition[] = $cursor;
}
$ids = []; $ids = [];
$next_cursor = 0; $next_cursor = 0;
$previous_cursor = 0; $previous_cursor = 0;
$total_count = 0; $total_count = 0;
if (!$hide_friends) { if (!$hide_friends) {
$condition = DBA::collapseCondition([
'rel' => $rel,
'uid' => $uid,
'self' => false,
'deleted' => false,
'hidden' => false,
'archive' => false,
'pending' => false
]);
$total_count = DBA::count('contact', $condition); $total_count = DBA::count('contact', $condition);
if ($cursor !== -1) {
if ($cursor > 0) {
$condition[0] .= " AND `id` > ?";
$condition[] = $cursor;
} else {
$condition[0] .= " AND `id` < ?";
$condition[] = -$cursor;
}
}
$contacts = Contact::selectToArray(['id'], $condition, ['limit' => $count, 'order' => ['id']]); $contacts = Contact::selectToArray(['id'], $condition, ['limit' => $count, 'order' => ['id']]);
// Contains user-specific contact ids // Contains user-specific contact ids
@ -169,9 +172,32 @@ abstract class ContactEndpoint extends BaseApi
// Cursor is on the user-specific contact id since it's the sort field // Cursor is on the user-specific contact id since it's the sort field
if (count($ids)) { if (count($ids)) {
$previous_cursor = -$ids[0];
$next_cursor = $ids[count($ids) -1]; $next_cursor = $ids[count($ids) -1];
} }
// No next page
if ($total_count <= count($contacts) || count($contacts) < $count) {
$next_cursor = 0;
}
// End of results
if ($cursor < 0 && count($contacts) === 0) {
$next_cursor = -1;
}
// No previous page
if ($cursor === -1) {
$previous_cursor = 0;
}
if ($cursor > 0 && count($contacts) === 0) {
$previous_cursor = -$cursor;
}
if ($cursor < 0 && count($contacts) === 0) {
$next_cursor = -1;
}
// Conversion to public contact ids // Conversion to public contact ids
array_walk($ids, function (&$contactId) use ($uid, $stringify_ids) { array_walk($ids, function (&$contactId) use ($uid, $stringify_ids) {
$cdata = Contact::getPublicAndUserContacID($contactId, $uid); $cdata = Contact::getPublicAndUserContacID($contactId, $uid);
@ -181,11 +207,6 @@ abstract class ContactEndpoint extends BaseApi
$contactId = (int)$cdata['public']; $contactId = (int)$cdata['public'];
} }
}); });
// No next page
if ($total_count <= count($contacts)) {
$next_cursor = 0;
}
} }
$return = [ $return = [

View file

@ -32,16 +32,25 @@ class FollowersIds extends ContactEndpoint
public static function rawContent(array $parameters = []) public static function rawContent(array $parameters = [])
{ {
// Expected value for user_id parameter: public/user contact id // Expected value for user_id parameter: public/user contact id
$contact_id = $_GET['user_id'] ?? null; $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT);
$screen_name = $_GET['screen_name'] ?? null; $screen_name = filter_input(INPUT_GET, 'screen_name');
$cursor = $_GET['cursor'] ?? $_GET['since_id'] ?? -1; $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT);
$stringify_ids = ($_GET['stringify_ids'] ?? 'false') != 'false'; $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN);
$count = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT); $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [
'default' => self::DEFAULT_COUNT,
'min_range' => 1,
'max_range' => self::MAX_COUNT,
]]);
// Friendica-specific
$since_id = filter_input(INPUT_GET, 'since_id' , FILTER_VALIDATE_INT);
$max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT, ['options' => [
'default' => 1,
]]);
System::jsonExit(self::ids( System::jsonExit(self::ids(
[Contact::FOLLOWER, Contact::FRIEND], [Contact::FOLLOWER, Contact::FRIEND],
self::getUid($contact_id, $screen_name), self::getUid($contact_id, $screen_name),
$cursor, $cursor ?? $since_id ?? - $max_id,
$count, $count,
$stringify_ids $stringify_ids
)); ));

View file

@ -32,17 +32,28 @@ class FollowersList extends ContactEndpoint
public static function rawContent(array $parameters = []) public static function rawContent(array $parameters = [])
{ {
// Expected value for user_id parameter: public/user contact id // Expected value for user_id parameter: public/user contact id
$contact_id = $_GET['user_id'] ?? null; $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT);
$screen_name = $_GET['screen_name'] ?? null; $screen_name = filter_input(INPUT_GET, 'screen_name');
$cursor = $_GET['cursor'] ?? $_GET['since_id'] ?? -1; $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT);
$count = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT); $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [
$skip_status = in_array(($_GET['skip_status'] ?? false), [true, 'true', 't', 1, '1']); 'default' => self::DEFAULT_COUNT,
$include_user_entities = ($_GET['include_user_entities'] ?? 'true') != 'false'; 'min_range' => 1,
'max_range' => self::MAX_COUNT,
]]);
$skip_status = filter_input(INPUT_GET, 'skip_status' , FILTER_VALIDATE_BOOLEAN);
$include_user_entities = filter_input(INPUT_GET, 'include_user_entities', FILTER_VALIDATE_BOOLEAN);
// Friendica-specific
$since_id = filter_input(INPUT_GET, 'since_id' , FILTER_VALIDATE_INT);
$max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT, ['options' => [
'default' => 1,
]]);
System::jsonExit(self::list( System::jsonExit(self::list(
[Contact::FOLLOWER, Contact::FRIEND], [Contact::FOLLOWER, Contact::FRIEND],
self::getUid($contact_id, $screen_name), self::getUid($contact_id, $screen_name),
$cursor, $cursor ?? $since_id ?? - $max_id,
$count, $count,
$skip_status, $skip_status,
$include_user_entities $include_user_entities

View file

@ -32,16 +32,25 @@ class FriendsIds extends ContactEndpoint
public static function rawContent(array $parameters = []) public static function rawContent(array $parameters = [])
{ {
// Expected value for user_id parameter: public/user contact id // Expected value for user_id parameter: public/user contact id
$contact_id = $_GET['user_id'] ?? null; $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT);
$screen_name = $_GET['screen_name'] ?? null; $screen_name = filter_input(INPUT_GET, 'screen_name');
$cursor = $_GET['cursor'] ?? $_GET['since_id'] ?? -1; $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT);
$stringify_ids = ($_GET['stringify_ids'] ?? 'false') != 'false'; $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN);
$count = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT); $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [
'default' => self::DEFAULT_COUNT,
'min_range' => 1,
'max_range' => self::MAX_COUNT,
]]);
// Friendica-specific
$since_id = filter_input(INPUT_GET, 'since_id' , FILTER_VALIDATE_INT);
$max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT, ['options' => [
'default' => 1,
]]);
System::jsonExit(self::ids( System::jsonExit(self::ids(
[Contact::SHARING, Contact::FRIEND], [Contact::SHARING, Contact::FRIEND],
self::getUid($contact_id, $screen_name), self::getUid($contact_id, $screen_name),
$cursor, $cursor ?? $since_id ?? - $max_id,
$count, $count,
$stringify_ids $stringify_ids
)); ));

View file

@ -32,17 +32,27 @@ class FriendsList extends ContactEndpoint
public static function rawContent(array $parameters = []) public static function rawContent(array $parameters = [])
{ {
// Expected value for user_id parameter: public/user contact id // Expected value for user_id parameter: public/user contact id
$contact_id = $_GET['user_id'] ?? null; $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT);
$screen_name = $_GET['screen_name'] ?? null; $screen_name = filter_input(INPUT_GET, 'screen_name');
$cursor = $_GET['cursor'] ?? $_GET['since_id'] ?? -1; $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT);
$count = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT); $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [
$skip_status = in_array(($_GET['skip_status'] ?? false), [true, 'true', 't', 1, '1']); 'default' => self::DEFAULT_COUNT,
$include_user_entities = ($_GET['include_user_entities'] ?? 'true') != 'false'; 'min_range' => 1,
'max_range' => self::MAX_COUNT,
]]);
$skip_status = filter_input(INPUT_GET, 'skip_status' , FILTER_VALIDATE_BOOLEAN);
$include_user_entities = filter_input(INPUT_GET, 'include_user_entities', FILTER_VALIDATE_BOOLEAN);
// Friendica-specific
$since_id = filter_input(INPUT_GET, 'since_id' , FILTER_VALIDATE_INT);
$max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT, ['options' => [
'default' => 1,
]]);
System::jsonExit(self::list( System::jsonExit(self::list(
[Contact::SHARING, Contact::FRIEND], [Contact::SHARING, Contact::FRIEND],
self::getUid($contact_id, $screen_name), self::getUid($contact_id, $screen_name),
$cursor, $cursor ?? $since_id ?? - $max_id,
$count, $count,
$skip_status, $skip_status,
$include_user_entities $include_user_entities