Only probr when needed, search local if nothing was found

This commit is contained in:
Michael 2023-05-17 02:23:56 +00:00
parent 97456ff205
commit e23a7383f8
2 changed files with 31 additions and 27 deletions

View File

@ -55,19 +55,25 @@ class Search
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public static function getContactsFromProbe(string $user): ?ResultList public static function getContactsFromProbe(string $user, $only_forum = false): ResultList
{ {
$emptyResultList = new ResultList();
if (empty(parse_url($user, PHP_URL_SCHEME)) && !(filter_var($user, FILTER_VALIDATE_EMAIL) || Network::isEmailDomainValid($user))) { if (empty(parse_url($user, PHP_URL_SCHEME)) && !(filter_var($user, FILTER_VALIDATE_EMAIL) || Network::isEmailDomainValid($user))) {
return null; return $emptyResultList;
} }
$user_data = Contact::getByURL($user); $user_data = Contact::getByURL($user);
if (empty($user_data)) { if (empty($user_data)) {
return null; return $emptyResultList;
}
if ($only_forum && ($user_data['contact-type'] != Contact::TYPE_COMMUNITY)) {
return $emptyResultList;
} }
if (!Protocol::supportsProbe($user_data['network'])) { if (!Protocol::supportsProbe($user_data['network'])) {
return null; return $emptyResultList;
} }
$contactDetails = Contact::getByURLForUser($user_data['url'], DI::userSession()->getLocalUserId()); $contactDetails = Contact::getByURLForUser($user_data['url'], DI::userSession()->getLocalUserId());
@ -125,8 +131,8 @@ class Search
$resultList = new ResultList( $resultList = new ResultList(
($results['page'] ?? 0) ?: 1, ($results['page'] ?? 0) ?: 1,
$results['count'] ?? 0, ($results['itemsperpage'] ?? 0) ?: 30,
($results['itemsperpage'] ?? 0) ?: 30 $results['count'] ?? 0
); );
$profiles = $results['profiles'] ?? []; $profiles = $results['profiles'] ?? [];
@ -170,7 +176,7 @@ class Search
$contacts = Contact::searchByName($search, $type == self::TYPE_FORUM ? 'community' : '', true); $contacts = Contact::searchByName($search, $type == self::TYPE_FORUM ? 'community' : '', true);
$resultList = new ResultList($start, $itemPage, count($contacts)); $resultList = new ResultList($start, count($contacts), $itemPage);
foreach ($contacts as $contact) { foreach ($contacts as $contact) {
$result = new ContactResult( $result = new ContactResult(
@ -280,4 +286,4 @@ class Search
return 'search?q=' . urlencode($search); return 'search?q=' . urlencode($search);
} }
} }
} }

View File

@ -23,6 +23,7 @@ namespace Friendica\Module;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Core\Logger;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\Core\Search; use Friendica\Core\Search;
use Friendica\DI; use Friendica\DI;
@ -62,18 +63,13 @@ class BaseSearch extends BaseModule
} }
$header = ''; $header = '';
$results = new ResultList();
if (strpos($search, '@') === 0) { if (strpos($search, '@') === 0) {
$search = trim(substr($search, 1)); $search = trim(substr($search, 1));
$type = Search::TYPE_PEOPLE; $type = Search::TYPE_PEOPLE;
$header = DI::l10n()->t('People Search - %s', $search); $header = DI::l10n()->t('People Search - %s', $search);
} elseif (strpos($search, '!') === 0) {
if (strrpos($search, '@') > 0) {
$results = Search::getContactsFromProbe(Network::convertToIdn($search));
}
}
if (strpos($search, '!') === 0) {
$search = trim(substr($search, 1)); $search = trim(substr($search, 1));
$type = Search::TYPE_FORUM; $type = Search::TYPE_FORUM;
$header = DI::l10n()->t('Forum Search - %s', $search); $header = DI::l10n()->t('Forum Search - %s', $search);
@ -91,16 +87,18 @@ class BaseSearch extends BaseModule
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage); $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
if (empty($results)) { if (!$results->getTotal() && !$localSearch && Search::getGlobalDirectory()) {
if ($localSearch) { $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
$pager->setItemsPerPage(80); $pager->setItemsPerPage($results->getItemsPage());
$results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage()); }
} elseif (Search::getGlobalDirectory()) {
$results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage()); if (!$results->getTotal()) {
$pager->setItemsPerPage($results->getItemsPage()); $pager->setItemsPerPage(80);
} else { $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
$results = new ResultList(); }
}
if (!$results->getTotal()) {
$results = Search::getContactsFromProbe(Network::convertToIdn($search), $type == Search::TYPE_FORUM);
} }
return self::printResult($results, $pager, $header); return self::printResult($results, $pager, $header);
@ -153,4 +151,4 @@ class BaseSearch extends BaseModule
'$paginate' => $pager->renderFull($results->getTotal()), '$paginate' => $pager->renderFull($results->getTotal()),
]); ]);
} }
} }