From 70d0f087b74d4a9e808a49913e0578c6bd1a5647 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 5 Nov 2022 22:08:28 +0000 Subject: [PATCH] Issue 12096: Improve account search --- src/Model/Contact.php | 16 ++++++++++++-- src/Module/Api/Mastodon/Accounts/Search.php | 24 +++++---------------- src/Module/Api/Mastodon/Search.php | 23 +++++--------------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 7c83e03511..4e2b0e5a24 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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; } diff --git a/src/Module/Api/Mastodon/Accounts/Search.php b/src/Module/Api/Mastodon/Accounts/Search.php index 60db5b057d..71f867d48b 100644 --- a/src/Module/Api/Mastodon/Accounts/Search.php +++ b/src/Module/Api/Mastodon/Accounts/Search.php @@ -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); diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index 27f99f00bf..2acfb90efa 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -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); -- 2.39.5