]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Search.php
Merge pull request #10224 from nupplaphil/feat/drone
[friendica.git] / src / Module / Api / Mastodon / Accounts / Search.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Mastodon\Accounts;
23
24 use Friendica\Core\Search as CoreSearch;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Module\BaseApi;
30 use Friendica\Object\Search\ContactResult;
31
32 /**
33  * @see https://docs.joinmastodon.org/methods/accounts/
34  */
35 class Search extends BaseApi
36 {
37         /**
38          * @param array $parameters
39          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
40          */
41         public static function rawContent(array $parameters = [])
42         {
43                 self::login();
44                 $uid = self::getCurrentUserID();
45
46                 // What to search for
47                 $q = (int)!isset($_REQUEST['q']) ? 0 : $_REQUEST['q'];
48                 // Maximum number of results. Defaults to 40.
49                 $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
50                 // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
51                 $resolve = (int)!isset($_REQUEST['resolve']) ? 0 : $_REQUEST['resolve'];
52                 // Only who the user is following. Defaults to false.
53                 $following = (int)!isset($_REQUEST['following']) ? 0 : $_REQUEST['following'];
54
55                 $accounts = [];
56
57                 if (!$following) {
58                         if ((strrpos($q, '@') > 0) && $resolve) {
59                                 $results = CoreSearch::getContactsFromProbe($q);
60                         }
61
62                         if (empty($results)) {
63                                 if (DI::config()->get('system', 'poco_local_search')) {
64                                         $results = CoreSearch::getContactsFromLocalDirectory($q, CoreSearch::TYPE_ALL, 0, $limit);
65                                 } elseif (!empty(DI::config()->get('system', 'directory'))) {
66                                         $results = CoreSearch::getContactsFromGlobalDirectory($q, CoreSearch::TYPE_ALL, 1);
67                                 }
68                         }
69
70                         if (!empty($results)) {
71                                 $counter = 0;
72                                 foreach ($results->getResults() as $result) {
73                                         if (++$counter > $limit) {
74                                                 continue;
75                                         }
76                                         if ($result instanceof ContactResult) {
77                                                 $id = Contact::getIdForURL($result->getUrl(), 0, false);
78
79                                                 $accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid);
80                                         }
81                                 }
82                         }
83                 } else {
84                         $contacts = Contact::searchByName($q, '', $uid);
85
86                         $counter = 0;
87                         foreach ($contacts as $contact) {
88                                 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
89                                         continue;
90                                 }
91                                 if (++$counter > $limit) {
92                                         continue;
93                                 }
94                                 $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
95                         }
96                         DBA::close($contacts);
97                 }
98
99                 System::jsonExit($accounts);
100         }
101 }