]> git.mxchange.org Git - friendica.git/commitdiff
Issue 12096: Improve account search
authorMichael <heluecht@pirati.ca>
Sat, 5 Nov 2022 22:08:28 +0000 (22:08 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 5 Nov 2022 22:08:28 +0000 (22:08 +0000)
src/Model/Contact.php
src/Module/Api/Mastodon/Accounts/Search.php
src/Module/Api/Mastodon/Search.php

index 7c83e035119faa0eb41f77a6bbc9512ffa5495c4..4e2b0e5a24b8c1ff3b1bad4b3151c870ccdafc87 100644 (file)
@@ -3373,11 +3373,13 @@ class Contact
         * @param string $search Name or nick
         * @param string $mode   Search mode (e.g. "community")
         * @param int    $uid    User ID
+        * @param int    $limit  Maximum amount of returned values
+        * @param int    $offset Limit offset
         *
         * @return array with search results
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function searchByName(string $search, string $mode = '', int $uid = 0): array
+       public static function searchByName(string $search, string $mode = '', int $uid = 0, int $limit = 0, int $offset = 0): array
        {
                if (empty($search)) {
                        return [];
@@ -3397,6 +3399,8 @@ class Contact
 
                if ($uid == 0) {
                        $condition['blocked'] = false;
+               } else {
+                       $condition['rel'] = [Contact::SHARING, Contact::FRIEND];
                }
 
                // check if we search only communities or every contact
@@ -3406,11 +3410,19 @@ class Contact
 
                $search .= '%';
 
+               $params = [];
+
+               if (!empty($limit) && !empty($offset)) {
+                       $params['limit'] = [$offset, $limit];
+               } elseif (!empty($limit)) {
+                       $params['limit'] = $limit;
+               }
+
                $condition = DBA::mergeConditions($condition,
                        ["(NOT `unsearchable` OR `nurl` IN (SELECT `nurl` FROM `owner-view` WHERE `publish` OR `net-publish`))
                        AND (`addr` LIKE ? OR `name` LIKE ? OR `nick` LIKE ?)", $search, $search, $search]);
 
-               $contacts = self::selectToArray([], $condition);
+               $contacts = self::selectToArray([], $condition, $params);
                return $contacts;
        }
 
index 60db5b057d03b756d356924d6c62d122338dc3f7..71f867d48bf35308484c9da1d5f3f8f89491d491 100644 (file)
@@ -51,19 +51,11 @@ class Search extends BaseApi
 
                $accounts = [];
 
-               if (!$request['following']) {
-                       if ((strrpos($request['q'], '@') > 0) && $request['resolve']) {
+               if ($request['resolve']) {
+                       if ((strrpos($request['q'], '@') > 0)) {
                                $results = CoreSearch::getContactsFromProbe($request['q']);
                        }
 
-                       if (empty($results)) {
-                               if (DI::config()->get('system', 'poco_local_search')) {
-                                       $results = CoreSearch::getContactsFromLocalDirectory($request['q'], CoreSearch::TYPE_ALL, 0, $request['limit']);
-                               } elseif (CoreSearch::getGlobalDirectory()) {
-                                       $results = CoreSearch::getContactsFromGlobalDirectory($request['q'], CoreSearch::TYPE_ALL, 1);
-                               }
-                       }
-
                        if (!empty($results)) {
                                $counter = 0;
                                foreach ($results->getResults() as $result) {
@@ -77,17 +69,11 @@ class Search extends BaseApi
                                        }
                                }
                        }
-               } else {
-                       $contacts = Contact::searchByName($request['q'], '', $uid);
+               }
 
-                       $counter = 0;
+               if (count($accounts) < $request['limit']) {
+                       $contacts = Contact::searchByName($request['q'], '', $request['following'] ? $uid : 0, $request['limit']);
                        foreach ($contacts as $contact) {
-                               if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
-                                       continue;
-                               }
-                               if (++$counter > $request['limit']) {
-                                       continue;
-                               }
                                $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
                        }
                        DBA::close($contacts);
index 27f99f00bf031119f8c1b0f86f9f3160c5ee24f1..2acfb90efa1a05fb6a4b125b2fef1a54fdcbbb6e 100644 (file)
@@ -83,18 +83,11 @@ class Search extends BaseApi
        {
                $accounts = [];
 
-               if (!$following) {
-                       if ((strrpos($q, '@') > 0) && $resolve) {
+               if ($resolve) {
+                       if ((strrpos($q, '@') > 0)) {
                                $results = CoreSearch::getContactsFromProbe($q);
                        }
 
-                       if (empty($results)) {
-                               if (DI::config()->get('system', 'poco_local_search')) {
-                                       $results = CoreSearch::getContactsFromLocalDirectory($q, CoreSearch::TYPE_ALL, 0, $limit);
-                               } elseif (CoreSearch::getGlobalDirectory()) {
-                                       $results = CoreSearch::getContactsFromGlobalDirectory($q, CoreSearch::TYPE_ALL, 1);
-                               }
-                       }
                        if (!empty($results)) {
                                $counter = 0;
                                foreach ($results->getResults() as $result) {
@@ -108,17 +101,11 @@ class Search extends BaseApi
                                        }
                                }
                        }
-               } else {
-                       $contacts = Contact::searchByName($q, '', $uid);
+               }
 
-                       $counter = 0;
+               if (count($accounts) < $limit) {
+                       $contacts = Contact::searchByName($q, '', $following ? $uid : 0, $limit - count($accounts), $offset);
                        foreach ($contacts as $contact) {
-                               if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
-                                       continue;
-                               }
-                               if (++$counter > $limit) {
-                                       continue;
-                               }
                                $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
                        }
                        DBA::close($contacts);