1
1
Fork 0

Merge pull request #12115 from annando/issue-12096

Issue 12096: Improve account search
This commit is contained in:
Hypolite Petovan 2022-11-05 18:46:06 -04:00 committed by GitHub
commit 5bc0480fc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 39 deletions

View 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;
}

View file

@ -28,6 +28,7 @@ use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Module\BaseApi;
use Friendica\Object\Search\ContactResult;
use Friendica\Util\Network;
/**
* @see https://docs.joinmastodon.org/methods/accounts/
@ -51,19 +52,11 @@ class Search extends BaseApi
$accounts = [];
if (!$request['following']) {
if ((strrpos($request['q'], '@') > 0) && $request['resolve']) {
if ($request['resolve']) {
if ((strrpos($request['q'], '@') > 0) || Network::isValidHttpUrl($request['q'])) {
$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 +70,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);

View file

@ -31,6 +31,7 @@ use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Module\BaseApi;
use Friendica\Object\Search\ContactResult;
use Friendica\Util\Network;
/**
* @see https://docs.joinmastodon.org/methods/search/
@ -83,18 +84,11 @@ class Search extends BaseApi
{
$accounts = [];
if (!$following) {
if ((strrpos($q, '@') > 0) && $resolve) {
if ($resolve) {
if ((strrpos($q, '@') > 0) || Network::isValidHttpUrl($q)) {
$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 +102,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);