Issue 12096: Improve account search

This commit is contained in:
Michael 2022-11-05 22:08:28 +00:00
parent c107c5ff0d
commit 70d0f087b7
3 changed files with 24 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

@ -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);

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