]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Api/Twitter/ContactEndpoint.php
Update src/Module/Api/Twitter/Statuses/Update.php
[friendica.git] / src / Module / Api / Twitter / ContactEndpoint.php
index 0e45488e1eead38bcbf8782c0e4beb4b228b94f4..ea2edb30e9fa99aae9cf8c038bbc821d5dced3c7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Module\Api\Twitter;
 
-use Friendica\Database\DBA;
+use Friendica\App;
+use Friendica\Core\L10n;
 use Friendica\DI;
-use Friendica\Model\Profile;
 use Friendica\Model\User;
+use Friendica\Module\Api\ApiResponse;
 use Friendica\Module\BaseApi;
 use Friendica\Model\Contact;
 use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
 use Friendica\Util\Strings;
+use Psr\Log\LoggerInterface;
 
 abstract class ContactEndpoint extends BaseApi
 {
        const DEFAULT_COUNT = 20;
        const MAX_COUNT = 200;
 
-       public static function init(array $parameters = [])
+       public function __construct(App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
        {
-               parent::init($parameters);
+               parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
 
-               if (!self::login()) {
-                       throw new HTTPException\UnauthorizedException();
-               }
+               self::checkAllowedScope(self::SCOPE_READ);
        }
 
        /**
@@ -54,7 +55,7 @@ abstract class ContactEndpoint extends BaseApi
         */
        protected static function getUid(int $contact_id = null, string $screen_name = null)
        {
-               $uid = self::$current_user_id;
+               $uid = self::getCurrentUserID();
 
                if ($contact_id || $screen_name) {
                        // screen_name trumps user_id when both are provided
@@ -73,7 +74,7 @@ abstract class ContactEndpoint extends BaseApi
                                throw new HTTPException\NotFoundException(DI::l10n()->t('User not found'));
                        }
 
-                       $uid = $user['uid'];
+                       $uid = (int)$user['uid'];
                }
 
                return $uid;
@@ -82,8 +83,9 @@ abstract class ContactEndpoint extends BaseApi
        /**
         * This methods expands the contact ids into full user objects in an existing result set.
         *
-        * @param mixed $rel A relationship constant or a list of them
-        * @param int   $uid The local user id we query the contacts from
+        * @param array $ids           List of contact ids
+        * @param int   $total_count   Total list of contacts
+        * @param int   $uid           The local user id we query the contacts from
         * @param int   $cursor
         * @param int   $count
         * @param bool  $skip_status
@@ -93,9 +95,9 @@ abstract class ContactEndpoint extends BaseApi
         * @throws HTTPException\NotFoundException
         * @throws \ImagickException
         */
-       protected static function list($rel, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $skip_status = false, bool $include_user_entities = true)
+       protected static function list(array $ids, int $total_count, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $skip_status = false, bool $include_user_entities = true): array
        {
-               $return = self::ids($rel, $uid, $cursor, $count);
+               $return = self::ids($ids, $total_count, $cursor, $count, false);
 
                $users = [];
                foreach ($return['ids'] as $contactId) {
@@ -114,78 +116,55 @@ abstract class ContactEndpoint extends BaseApi
                        'total_count' => $return['total_count'],
                ];
 
-
-
                return $return;
        }
 
        /**
-        * @param mixed $rel A relationship constant or a list of them
-        * @param int   $uid The local user id we query the contacts from
+        * @param array $ids           List of contact ids
+        * @param int   $total_count   Total list of contacts
         * @param int   $cursor
-        * @param int   $count
-        * @param bool  $stringify_ids
+        * @param int   $count         Number of elements to return
+        * @param bool  $stringify_ids if "true" then the id is converted to a string
         * @return array
         * @throws HTTPException\NotFoundException
         */
-       protected static function ids($rel, int $uid, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $stringify_ids = false)
+       protected static function ids(array $ids, int $total_count, int $cursor = -1, int $count = self::DEFAULT_COUNT, bool $stringify_ids = false): array
        {
-               $hide_friends = false;
-               if ($uid != self::$current_user_id) {
-                       $profile = Profile::getByUID($uid);
-                       if (empty($profile)) {
-                               throw new HTTPException\NotFoundException(DI::l10n()->t('Profile not found'));
-                       }
+               $next_cursor = 0;
+               $previous_cursor = 0;
 
-                       $hide_friends = (bool)$profile['hide-friends'];
+               // Cursor is on the user-specific contact id since it's the sort field
+               if (count($ids)) {
+                       $previous_cursor = -$ids[0];
+                       $next_cursor = (int)$ids[count($ids) -1];
                }
 
-               $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;
+               // No next page
+               if ($total_count <= count($ids) || count($ids) < $count) {
+                       $next_cursor = 0;
+               }
+               // End of results
+               if ($cursor < 0 && count($ids) === 0) {
+                       $next_cursor = -1;
                }
 
-               $ids = [];
-               $next_cursor = 0;
-               $previous_cursor = 0;
-               $total_count = 0;
-               if (!$hide_friends) {
-                       $total_count = DBA::count('contact', $condition);
-
-                       $contacts = Contact::selectToArray(['id'], $condition, ['limit' => $count, 'order' => ['id']]);
+               // No previous page
+               if ($cursor === -1) {
+                       $previous_cursor = 0;
+               }
 
-                       // Contains user-specific contact ids
-                       $ids = array_column($contacts, 'id');
+               if ($cursor > 0 && count($ids) === 0) {
+                       $previous_cursor = -$cursor;
+               }
 
-                       // Cursor is on the user-specific contact id since it's the sort field
-                       if (count($ids)) {
-                               $next_cursor = $ids[count($ids) -1];
-                       }
+               if ($cursor < 0 && count($ids) === 0) {
+                       $next_cursor = -1;
+               }
 
-                       // Conversion to public contact ids
-                       array_walk($ids, function (&$contactId) use ($uid, $stringify_ids) {
-                               $cdata = Contact::getPublicAndUserContacID($contactId, $uid);
-                               if ($stringify_ids) {
-                                       $contactId = (string)$cdata['public'];
-                               } else {
-                                       $contactId = (int)$cdata['public'];
-                               }
+               if ($stringify_ids) {
+                       array_walk($ids, function (&$contactId) {
+                               $contactId = (string)$contactId;
                        });
-
-                       // No next page
-                       if ($total_count <= count($contacts)) {
-                               $next_cursor = 0;
-                       }
                }
 
                $return = [