]> git.mxchange.org Git - friendica.git/commitdiff
Add support for max_id parameter in new Twitter contact API endpoints
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 10 May 2020 14:24:35 +0000 (10:24 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 9 Jun 2020 12:36:53 +0000 (08:36 -0400)
- Use filter_input instead of manual type casting

doc/API-Twitter.md
src/Module/Api/Twitter/ContactEndpoint.php
src/Module/Api/Twitter/FollowersIds.php
src/Module/Api/Twitter/FollowersList.php
src/Module/Api/Twitter/FriendsIds.php
src/Module/Api/Twitter/FriendsList.php

index 86a317875242a56bd4e5e31b28d55af352494563..fab26ae5b5d85dfc05de52808f8dceef647cd944 100644 (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/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)
-    - Additional parameter:
-        - `since_id`: Same behavior as `cursor`, use the `next_cursor` value to load the next page.
+    - Additional parameters:
+        - `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:
         - `skip_status`: No status is returned even if it isn't set to true.
     - 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.
         - `screen_name` must be associated with a local user account.
         - `screen_name` trumps `user_id` if both are provided (undocumented Twitter behavior).
index 0e45488e1eead38bcbf8782c0e4beb4b228b94f4..116f8eea2da4bb64afdbf9b72b64e809c746a8f2 100644 (file)
@@ -114,8 +114,6 @@ abstract class ContactEndpoint extends BaseApi
                        'total_count' => $return['total_count'],
                ];
 
-
-
                return $return;
        }
 
@@ -140,28 +138,33 @@ abstract class ContactEndpoint extends BaseApi
                        $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 = [];
                $next_cursor = 0;
                $previous_cursor = 0;
                $total_count = 0;
                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);
 
+                       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']]);
 
                        // 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
                        if (count($ids)) {
+                               $previous_cursor = -$ids[0];
                                $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
                        array_walk($ids, function (&$contactId) use ($uid, $stringify_ids) {
                                $cdata = Contact::getPublicAndUserContacID($contactId, $uid);
@@ -181,11 +207,6 @@ abstract class ContactEndpoint extends BaseApi
                                        $contactId = (int)$cdata['public'];
                                }
                        });
-
-                       // No next page
-                       if ($total_count <= count($contacts)) {
-                               $next_cursor = 0;
-                       }
                }
 
                $return = [
index 8e0a7da98b3e8e0bd19bdccf61a14cc9f359ca2f..7b0bc84e073c1748d10c77a89b0a749799dcd380 100644 (file)
@@ -32,16 +32,25 @@ class FollowersIds extends ContactEndpoint
        public static function rawContent(array $parameters = [])
        {
                // Expected value for user_id parameter: public/user contact id
-               $contact_id    = $_GET['user_id'] ?? null;
-               $screen_name   = $_GET['screen_name'] ?? null;
-               $cursor        = $_GET['cursor'] ?? $_GET['since_id'] ?? -1;
-               $stringify_ids = ($_GET['stringify_ids'] ?? 'false') != 'false';
-               $count         = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT);
+               $contact_id    = filter_input(INPUT_GET, 'user_id'      , FILTER_VALIDATE_INT);
+               $screen_name   = filter_input(INPUT_GET, 'screen_name');
+               $cursor        = filter_input(INPUT_GET, 'cursor'       , FILTER_VALIDATE_INT);
+               $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN);
+               $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(
                        [Contact::FOLLOWER, Contact::FRIEND],
                        self::getUid($contact_id, $screen_name),
-                       $cursor,
+                       $cursor ?? $since_id ?? - $max_id,
                        $count,
                        $stringify_ids
                ));
index 867272bfba84f73643185bb5385ea3f66517c5a0..7559d8327a995e2ebb69d57fb1cc40f0bb8b612a 100644 (file)
@@ -32,17 +32,28 @@ class FollowersList extends ContactEndpoint
        public static function rawContent(array $parameters = [])
        {
                // Expected value for user_id parameter: public/user contact id
-               $contact_id  = $_GET['user_id'] ?? null;
-               $screen_name = $_GET['screen_name'] ?? null;
-               $cursor      = $_GET['cursor'] ?? $_GET['since_id'] ?? -1;
-               $count       = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT);
-               $skip_status = in_array(($_GET['skip_status'] ?? false), [true, 'true', 't', 1, '1']);
-               $include_user_entities = ($_GET['include_user_entities'] ?? 'true') != 'false';
+               $contact_id    = filter_input(INPUT_GET, 'user_id'      , FILTER_VALIDATE_INT);
+               $screen_name   = filter_input(INPUT_GET, 'screen_name');
+               $cursor        = filter_input(INPUT_GET, 'cursor'       , FILTER_VALIDATE_INT);
+               $count         = filter_input(INPUT_GET, 'count'        , FILTER_VALIDATE_INT, ['options' => [
+                       'default' => self::DEFAULT_COUNT,
+                       '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(
                        [Contact::FOLLOWER, Contact::FRIEND],
                        self::getUid($contact_id, $screen_name),
-                       $cursor,
+                       $cursor ?? $since_id ?? - $max_id,
                        $count,
                        $skip_status,
                        $include_user_entities
index 4086b17a503d3649d09a67ed69dcd138eb2a00f4..1a303bfa7546b0bf6b96b907dee82fc0387eca56 100644 (file)
@@ -32,16 +32,25 @@ class FriendsIds extends ContactEndpoint
        public static function rawContent(array $parameters = [])
        {
                // Expected value for user_id parameter: public/user contact id
-               $contact_id    = $_GET['user_id'] ?? null;
-               $screen_name   = $_GET['screen_name'] ?? null;
-               $cursor        = $_GET['cursor'] ?? $_GET['since_id'] ?? -1;
-               $stringify_ids = ($_GET['stringify_ids'] ?? 'false') != 'false';
-               $count         = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT);
+               $contact_id    = filter_input(INPUT_GET, 'user_id'      , FILTER_VALIDATE_INT);
+               $screen_name   = filter_input(INPUT_GET, 'screen_name');
+               $cursor        = filter_input(INPUT_GET, 'cursor'       , FILTER_VALIDATE_INT);
+               $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN);
+               $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(
                        [Contact::SHARING, Contact::FRIEND],
                        self::getUid($contact_id, $screen_name),
-                       $cursor,
+                       $cursor ?? $since_id ?? - $max_id,
                        $count,
                        $stringify_ids
                ));
index e07f558f1dc47028f014600207b903bd99d10bf6..1a45f0791751af2a753e14d083c9c6645cc524ed 100644 (file)
@@ -32,17 +32,27 @@ class FriendsList extends ContactEndpoint
        public static function rawContent(array $parameters = [])
        {
                // Expected value for user_id parameter: public/user contact id
-               $contact_id  = $_GET['user_id'] ?? null;
-               $screen_name = $_GET['screen_name'] ?? null;
-               $cursor      = $_GET['cursor'] ?? $_GET['since_id'] ?? -1;
-               $count       = min((int) ($_GET['count'] ?? self::DEFAULT_COUNT), self::MAX_COUNT);
-               $skip_status = in_array(($_GET['skip_status'] ?? false), [true, 'true', 't', 1, '1']);
-               $include_user_entities = ($_GET['include_user_entities'] ?? 'true') != 'false';
+               $contact_id    = filter_input(INPUT_GET, 'user_id'      , FILTER_VALIDATE_INT);
+               $screen_name   = filter_input(INPUT_GET, 'screen_name');
+               $cursor        = filter_input(INPUT_GET, 'cursor'       , FILTER_VALIDATE_INT);
+               $count         = filter_input(INPUT_GET, 'count'        , FILTER_VALIDATE_INT, ['options' => [
+                       'default' => self::DEFAULT_COUNT,
+                       '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(
                        [Contact::SHARING, Contact::FRIEND],
                        self::getUid($contact_id, $screen_name),
-                       $cursor,
+                       $cursor ?? $since_id ?? - $max_id,
                        $count,
                        $skip_status,
                        $include_user_entities