]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Search.php
Merge pull request #10380 from annando/api-oauth-basicauth
[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::checkAllowedScope(self::SCOPE_READ);
44                 $uid = self::getCurrentUserID();
45
46                 $request = self::getRequest([
47                         'q'         => '',    // What to search for
48                         'limit'     => 40,    // Maximum number of results. Defaults to 40.
49                         'resolve'   => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
50                         'following' => false, // Only who the user is following. Defaults to false.
51                 ]);
52
53                 $accounts = [];
54
55                 if (!$request['following']) {
56                         if ((strrpos($request['q'], '@') > 0) && $request['resolve']) {
57                                 $results = CoreSearch::getContactsFromProbe($request['q']);
58                         }
59
60                         if (empty($results)) {
61                                 if (DI::config()->get('system', 'poco_local_search')) {
62                                         $results = CoreSearch::getContactsFromLocalDirectory($request['q'], CoreSearch::TYPE_ALL, 0, $request['limit']);
63                                 } elseif (!empty(DI::config()->get('system', 'directory'))) {
64                                         $results = CoreSearch::getContactsFromGlobalDirectory($request['q'], CoreSearch::TYPE_ALL, 1);
65                                 }
66                         }
67
68                         if (!empty($results)) {
69                                 $counter = 0;
70                                 foreach ($results->getResults() as $result) {
71                                         if (++$counter > $request['limit']) {
72                                                 continue;
73                                         }
74                                         if ($result instanceof ContactResult) {
75                                                 $id = Contact::getIdForURL($result->getUrl(), 0, false);
76
77                                                 $accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid);
78                                         }
79                                 }
80                         }
81                 } else {
82                         $contacts = Contact::searchByName($request['q'], '', $uid);
83
84                         $counter = 0;
85                         foreach ($contacts as $contact) {
86                                 if (!in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
87                                         continue;
88                                 }
89                                 if (++$counter > $request['limit']) {
90                                         continue;
91                                 }
92                                 $accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
93                         }
94                         DBA::close($contacts);
95                 }
96
97                 System::jsonExit($accounts);
98         }
99 }