2021-05-09 11:35:51 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2023-01-01 15:36:24 +01:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2021-05-09 11:35:51 +02:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Module\Api\Mastodon\Accounts;
|
|
|
|
|
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Database\DBA;
|
|
|
|
use Friendica\DI;
|
|
|
|
use Friendica\Model\Contact;
|
|
|
|
use Friendica\Module\BaseApi;
|
2022-11-05 23:32:56 +01:00
|
|
|
use Friendica\Util\Network;
|
2021-05-09 14:00:08 +02:00
|
|
|
|
2021-05-09 11:35:51 +02:00
|
|
|
/**
|
2022-12-06 19:47:43 +01:00
|
|
|
* @see https://docs.joinmastodon.org/methods/accounts/#search
|
2021-05-09 11:35:51 +02:00
|
|
|
*/
|
|
|
|
class Search extends BaseApi
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
*/
|
2021-11-20 15:38:03 +01:00
|
|
|
protected function rawContent(array $request = [])
|
2021-05-09 11:35:51 +02:00
|
|
|
{
|
2023-10-11 15:30:42 +02:00
|
|
|
$this->checkAllowedScope(self::SCOPE_READ);
|
2021-05-09 11:35:51 +02:00
|
|
|
$uid = self::getCurrentUserID();
|
|
|
|
|
2021-11-28 13:22:27 +01:00
|
|
|
$request = $this->getRequest([
|
2021-05-18 21:26:46 +02:00
|
|
|
'q' => '', // What to search for
|
|
|
|
'limit' => 40, // Maximum number of results. Defaults to 40.
|
2022-12-06 19:47:43 +01:00
|
|
|
'offset' => 0, // Offset in search results. Used for pagination. Defaults to 0.
|
2021-05-18 21:26:46 +02:00
|
|
|
'resolve' => false, // Attempt WebFinger lookup. Defaults to false. Use this when q is an exact address.
|
|
|
|
'following' => false, // Only who the user is following. Defaults to false.
|
2021-11-28 00:30:41 +01:00
|
|
|
], $request);
|
2021-05-09 11:35:51 +02:00
|
|
|
|
|
|
|
$accounts = [];
|
|
|
|
|
2022-12-06 19:47:43 +01:00
|
|
|
if (($request['offset'] == 0) && (Network::isValidHttpUrl($request['q']) || (strrpos($request['q'], '@') > 0))) {
|
2022-11-06 06:43:34 +01:00
|
|
|
$id = Contact::getIdForURL($request['q'], 0, $request['resolve'] ? null : false);
|
2021-05-09 13:58:27 +02:00
|
|
|
|
2022-11-06 06:43:34 +01:00
|
|
|
if (!empty($id)) {
|
|
|
|
$accounts[] = DI::mstdnAccount()->createFromContactId($id, $uid);
|
2021-05-09 11:35:51 +02:00
|
|
|
}
|
2022-11-05 23:08:28 +01:00
|
|
|
}
|
2021-05-09 13:58:27 +02:00
|
|
|
|
2022-11-06 06:43:34 +01:00
|
|
|
if (empty($accounts)) {
|
2023-03-29 20:39:21 +02:00
|
|
|
$contacts = Contact::searchByName($request['q'], '', false, $request['following'] ? $uid : 0, $request['limit'], $request['offset']);
|
2021-05-09 11:35:51 +02:00
|
|
|
foreach ($contacts as $contact) {
|
|
|
|
$accounts[] = DI::mstdnAccount()->createFromContactId($contact['id'], $uid);
|
|
|
|
}
|
|
|
|
DBA::close($contacts);
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:16:17 +02:00
|
|
|
$this->jsonExit($accounts);
|
2021-05-09 11:35:51 +02:00
|
|
|
}
|
|
|
|
}
|