* @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 [];
if ($uid == 0) {
$condition['blocked'] = false;
+ } else {
+ $condition['rel'] = [Contact::SHARING, Contact::FRIEND];
}
// check if we search only communities or every 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;
}
$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) {
}
}
}
- } 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);
{
$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) {
}
}
}
- } 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);