]> git.mxchange.org Git - friendica.git/commitdiff
Move Module\Search\Acl::contactAutocomplete to Core\Search::searchGlobalContact
authorHypolite Petovan <hypolite@mrpetovan.com>
Sat, 5 Oct 2019 02:19:54 +0000 (22:19 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 5 Oct 2019 02:19:54 +0000 (22:19 -0400)
- Replace broken reference to directory /lsearch module with /search module

src/Core/Search.php
src/Module/Search/Acl.php

index 90298971a6881c21af5d2515523d8ed16d81f91b..495d45d1b5f39d0c482b85d7a5b9f586843ba02f 100644 (file)
@@ -5,6 +5,7 @@ namespace Friendica\Core;
 use Friendica\BaseObject;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
+use Friendica\Model\GContact;
 use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
 use Friendica\Object\Search\ContactResult;
@@ -245,4 +246,48 @@ class Search extends BaseObject
 
                return $resultList;
        }
+
+
+
+       /**
+        * Searching for global contacts for autocompletion
+        *
+        * @brief Searching for global contacts for autocompletion
+        * @param string $search Name or part of a name or nick
+        * @param string $mode   Search mode (e.g. "community")
+        * @param int    $page   Page number (starts at 1)
+        * @return array with the search results
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public static function searchGlobalContact($search, $mode, int $page = 1)
+       {
+               if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
+                       return [];
+               }
+
+               // don't search if search term has less than 2 characters
+               if (!$search || mb_strlen($search) < 2) {
+                       return [];
+               }
+
+               if (substr($search, 0, 1) === '@') {
+                       $search = substr($search, 1);
+               }
+
+               // check if searching in the local global contact table is enabled
+               if (Config::get('system', 'poco_local_search')) {
+                       $return = GContact::searchByName($search, $mode);
+               } else {
+                       $p = $page > 1 ? 'p=' . $page : '';
+                       $curlResult = Network::curl(get_server() . '/search/people?' . $p . '&q=' . urlencode($search), false, ['accept_content' => 'application/json']);
+                       if ($curlResult->isSuccess()) {
+                               $searchResult = json_decode($curlResult->getBody(), true);
+                               if (!empty($searchResult['profiles'])) {
+                                       $return = $searchResult['profiles'];
+                               }
+                       }
+               }
+
+               return $return ?? [];
+       }
 }
index 07cc9cdac5516865492e2d1db4a7e1ebc17efec4..97a55eaf2ccdfccef56dc8be0e74e00a4c426903 100644 (file)
@@ -4,18 +4,15 @@ namespace Friendica\Module\Search;
 
 use Friendica\BaseModule;
 use Friendica\Content\Widget;
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
-use Friendica\Core\Session;
+use Friendica\Core\Search;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
-use Friendica\Model\GContact;
 use Friendica\Model\Item;
 use Friendica\Network\HTTPException;
-use Friendica\Util\Network;
 use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 
@@ -32,10 +29,53 @@ class Acl extends BaseModule
                        throw new HTTPException\UnauthorizedException(L10n::t('You must be logged in to use this module.'));
                }
 
+               $type = $_REQUEST['type'] ?? '';
+
+               if ($type === 'x') {
+                       $o = self::globalContactSearch();
+               } else {
+                       $o = self::regularContactSearch($type);
+               }
+
+               echo json_encode($o);
+               exit;
+       }
+
+       private static function globalContactSearch()
+       {
+               // autocomplete for global contact search (e.g. navbar search)
+               $search = Strings::escapeTags(trim($_REQUEST['search']));
+               $mode = $_REQUEST['smode'];
+               $page = $_REQUEST['page'] ?? 1;
+
+               $r = Search::searchGlobalContact($search, $mode, $page);
+
+               $contacts = [];
+               foreach ($r as $g) {
+                       $contacts[] = [
+                               'photo'   => ProxyUtils::proxifyUrl($g['photo'], false, ProxyUtils::SIZE_MICRO),
+                               'name'    => htmlspecialchars($g['name']),
+                               'nick'    => $g['addr'] ?: $g['url'],
+                               'network' => $g['network'],
+                               'link'    => $g['url'],
+                               'forum'   => !empty($g['community']) ? 1 : 0,
+                       ];
+               }
+
+               $o = [
+                       'start' => ($page - 1) * 20,
+                       'count' => 1000,
+                       'items' => $contacts,
+               ];
+
+               return $o;
+       }
+
+       private static function regularContactSearch(string $type)
+       {
                $start   = $_REQUEST['start']        ?? 0;
                $count   = $_REQUEST['count']        ?? 100;
                $search  = $_REQUEST['search']       ?? '';
-               $type    = $_REQUEST['type']         ?? '';
                $conv_id = $_REQUEST['conversation'] ?? null;
 
                // For use with jquery.textcomplete for private mail completion
@@ -194,32 +234,6 @@ class Acl extends BaseModule
                                ORDER BY `name`",
                                intval(local_user())
                        );
-               } elseif ($type == 'x') {
-                       // autocomplete for global contact search (e.g. navbar search)
-                       $search = Strings::escapeTags(trim($_REQUEST['search']));
-                       $mode = $_REQUEST['smode'];
-                       $page = $_REQUEST['page'] ?? 1;
-
-                       $r = self::contactAutocomplete($search, $mode, $page);
-
-                       $contacts = [];
-                       foreach ($r as $g) {
-                               $contacts[] = [
-                                       'photo'   => ProxyUtils::proxifyUrl($g['photo'], false, ProxyUtils::SIZE_MICRO),
-                                       'name'    => htmlspecialchars($g['name']),
-                                       'nick'    => $g['addr'] ?: $g['url'],
-                                       'network' => $g['network'],
-                                       'link'    => $g['url'],
-                                       'forum'   => !empty($g['community']) ? 1 : 0,
-                               ];
-                       }
-                       $o = [
-                               'start' => $start,
-                               'count' => $count,
-                               'items' => $contacts,
-                       ];
-                       echo json_encode($o);
-                       exit;
                }
 
                if (DBA::isResult($r)) {
@@ -324,51 +338,6 @@ class Acl extends BaseModule
                        'items' => $results['items'],
                ];
 
-               echo json_encode($o);
-               exit;
-       }
-
-
-       /**
-        * Searching for global contacts for autocompletion
-        *
-        * @brief Searching for global contacts for autocompletion
-        * @param string $search Name or part of a name or nick
-        * @param string $mode   Search mode (e.g. "community")
-        * @param int    $page   Page number (starts at 1)
-        * @return array with the search results
-        * @throws HTTPException\InternalServerErrorException
-        */
-       private static function contactAutocomplete($search, $mode, int $page = 1)
-       {
-               if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
-                       return [];
-               }
-
-               // don't search if search term has less than 2 characters
-               if (!$search || mb_strlen($search) < 2) {
-                       return [];
-               }
-
-               if (substr($search, 0, 1) === '@') {
-                       $search = substr($search, 1);
-               }
-
-               // check if searching in the local global contact table is enabled
-               if (Config::get('system', 'poco_local_search')) {
-                       $return = GContact::searchByName($search, $mode);
-               } else {
-                       $p = $page > 1 ? 'p=' . $page : '';
-
-                       $curlResult = Network::curl(get_server() . '/lsearch?' . $p . '&search=' . urlencode($search));
-                       if ($curlResult->isSuccess()) {
-                               $lsearch = json_decode($curlResult->getBody(), true);
-                               if (!empty($lsearch['results'])) {
-                                       $return = $lsearch['results'];
-                               }
-                       }
-               }
-
-               return $return ?? [];
+               return $o;
        }
 }