]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Search.php
17adb54851915a0aa3378c64bf1b37917cd93091
[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  * @see https://docs.joinmastodon.org/methods/accounts/
33  */
34 class Search extends BaseApi
35 {
36         /**
37          * @param array $parameters
38          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39          */
40         public static function rawContent(array $parameters = [])
41         {
42                 self::login();
43                 $uid = self::getCurrentUserID();
44
45                 // What to search for
46                 $q = (int)!isset($_REQUEST['q']) ? 0 : $_REQUEST['q'];
47                 // Maximum number of results. Defaults to 40.
48                 $limit = (int)!isset($_REQUEST['limit']) ? 40 : $_REQUEST['limit'];
49                 // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
50                 $resolve = (int)!isset($_REQUEST['resolve']) ? 0 : $_REQUEST['resolve'];
51                 // Only who the user is following. Defaults to false.
52                 $following = (int)!isset($_REQUEST['following']) ? 0 : $_REQUEST['following'];
53
54                 $accounts = [];
55
56                 if (!$following) {
57                         if ((strrpos($q, '@') > 0) && $resolve) {
58                                 $results = CoreSearch::getContactsFromProbe($q);
59                         }
60
61                         if (empty($results)) {
62                                 if (DI::config()->get('system', 'poco_local_search')) {
63                                         $results = CoreSearch::getContactsFromLocalDirectory($q, CoreSearch::TYPE_ALL, 0, $limit);
64                                 } elseif (!empty(DI::config()->get('system', 'directory'))) {
65                                         $results = CoreSearch::getContactsFromGlobalDirectory($q, CoreSearch::TYPE_ALL, 1);
66                                 }
67                         }
68
69                         if (!empty($results)) {
70                                 $counter = 0;
71                                 foreach ($results->getResults() as $result) {
72                                         if (++$counter > $limit) {
73                                                 continue;
74                                         }
75                                         if ($result instanceof ContactResult) {
76                                                 $id = Contact::getIdForURL($result->getUrl(), 0, false);
77                                                 $accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid);       
78                                         }
79                                 }
80                         }
81                 } else {
82                         $contacts = Contact::searchByName($q, '', $uid);
83                         $counter = 0;
84                         foreach ($contacts as $contact) {
85                                 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
86                                         continue;
87                                 }
88                                 if (++$counter > $limit) {
89                                         continue;
90                                 }
91                                 $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
92                         }
93                         DBA::close($contacts);
94                 }
95
96                 System::jsonExit($accounts);
97         }
98 }