3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Api\Mastodon\Accounts;
24 use Friendica\Core\Search as CoreSearch;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Module\BaseApi;
30 use Friendica\Object\Search\ContactResult;
33 * @see https://docs.joinmastodon.org/methods/accounts/
35 class Search extends BaseApi
38 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40 public static function rawContent()
42 self::checkAllowedScope(self::SCOPE_READ);
43 $uid = self::getCurrentUserID();
45 $request = self::getRequest([
46 'q' => '', // What to search for
47 'limit' => 40, // Maximum number of results. Defaults to 40.
48 'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
49 'following' => false, // Only who the user is following. Defaults to false.
54 if (!$request['following']) {
55 if ((strrpos($request['q'], '@') > 0) && $request['resolve']) {
56 $results = CoreSearch::getContactsFromProbe($request['q']);
59 if (empty($results)) {
60 if (DI::config()->get('system', 'poco_local_search')) {
61 $results = CoreSearch::getContactsFromLocalDirectory($request['q'], CoreSearch::TYPE_ALL, 0, $request['limit']);
62 } elseif (!empty(DI::config()->get('system', 'directory'))) {
63 $results = CoreSearch::getContactsFromGlobalDirectory($request['q'], CoreSearch::TYPE_ALL, 1);
67 if (!empty($results)) {
69 foreach ($results->getResults() as $result) {
70 if (++$counter > $request['limit']) {
73 if ($result instanceof ContactResult) {
74 $id = Contact::getIdForURL($result->getUrl(), 0, false);
76 $accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid);
81 $contacts = Contact::searchByName($request['q'], '', $uid);
84 foreach ($contacts as $contact) {
85 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
88 if (++$counter > $request['limit']) {
91 $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
93 DBA::close($contacts);
96 System::jsonExit($accounts);