Removed "api_get_user"

This commit is contained in:
Michael 2021-11-18 22:20:19 +00:00
parent 1f79d25ffc
commit 8fd738f550
3 changed files with 20 additions and 155 deletions

View file

@ -133,24 +133,6 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY
]; ];
} }
/**
* Check HTTP method of called API
*
* API endpoints can define which HTTP method to accept when called.
* This function check the current HTTP method agains endpoint
* registered method.
*
* @param string $method Required methods, uppercase, separated by comma
* @return bool
*/
function api_check_method($method)
{
if ($method == "*") {
return true;
}
return (stripos($method, $_SERVER['REQUEST_METHOD'] ?? 'GET') !== false);
}
/** /**
* Main API entry point * Main API entry point
* *
@ -186,10 +168,6 @@ function api_call(App $a, App\Arguments $args = null)
try { try {
foreach ($API as $p => $info) { foreach ($API as $p => $info) {
if (strpos($args->getCommand(), $p) === 0) { if (strpos($args->getCommand(), $p) === 0) {
if (!api_check_method($info['method'])) {
throw new MethodNotAllowedException();
}
if (!empty($info['auth']) && BaseApi::getCurrentUserID() === false) { if (!empty($info['auth']) && BaseApi::getCurrentUserID() === false) {
BasicAuth::getCurrentUserID(true); BasicAuth::getCurrentUserID(true);
Logger::info(API_LOG_PREFIX . 'nickname {nickname}', ['module' => 'api', 'action' => 'call', 'nickname' => $a->getLoggedInUserNickname()]); Logger::info(API_LOG_PREFIX . 'nickname {nickname}', ['module' => 'api', 'action' => 'call', 'nickname' => $a->getLoggedInUserNickname()]);
@ -304,85 +282,6 @@ function api_unique_id_to_nurl($id)
} }
} }
/**
* Get user info array.
*
* @param App $a App
* @param int|string $contact_id Contact ID or URL
* @return array|bool
* @throws BadRequestException
* @throws ImagickException
* @throws InternalServerErrorException
* @throws UnauthorizedException
*/
function api_get_user($contact_id = null)
{
$user = null;
$extra_query = "";
$url = "";
Logger::info(API_LOG_PREFIX . 'Fetching data for user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $contact_id]);
// Searching for contact URL
if (intval($contact_id) == 0) {
$user = Strings::normaliseLink($contact_id);
$url = $user;
$extra_query = "AND `contact`.`nurl` = ? ";
if (!empty(BaseApi::getCurrentUserID())) {
$extra_query .= "AND `contact`.`uid`=" . intval(BaseApi::getCurrentUserID());
}
}
// Searching for contact id with uid = 0
if (intval($contact_id) != 0) {
$user = api_unique_id_to_nurl(intval($contact_id));
if ($user == "") {
throw new BadRequestException("User ID ".$contact_id." not found.");
}
$url = $user;
$extra_query = "AND `contact`.`nurl` = ? ";
if (!empty(BaseApi::getCurrentUserID())) {
$extra_query .= "AND `contact`.`uid`=" . intval(BaseApi::getCurrentUserID());
}
}
Logger::info(API_LOG_PREFIX . 'getting user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $user]);
if (!$user) {
return false;
}
Logger::info(API_LOG_PREFIX . 'found user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $user, 'extra_query' => $extra_query]);
// user info
$uinfo = DBA::toArray(DBA::p(
"SELECT *, `contact`.`id` AS `cid` FROM `contact`
WHERE 1
$extra_query",
$user
));
if (DBA::isResult($uinfo)) {
// Selecting the id by priority, friendica first
api_best_nickname($uinfo);
return DI::twitterUser()->createFromContactId($uinfo[0]['cid'], $uinfo[0]['uid'])->toArray();
}
if ($url == "") {
throw new BadRequestException("User not found.");
}
$cid = Contact::getIdForURL($url, 0, false);
if (!empty($cid)) {
return DI::twitterUser()->createFromContactId($cid, 0)->toArray();
} else {
throw new BadRequestException("User ".$url." not found.");
}
}
/** /**
* return api-formatted array for item's author and owner * return api-formatted array for item's author and owner
* *
@ -981,7 +880,10 @@ function api_users_lookup($type)
if (!empty($_REQUEST['user_id'])) { if (!empty($_REQUEST['user_id'])) {
foreach (explode(',', $_REQUEST['user_id']) as $id) { foreach (explode(',', $_REQUEST['user_id']) as $id) {
if (!empty($id)) { if (!empty($id)) {
$users[] = api_get_user($id); $cid = BaseApi::getContactIDForSearchterm($id);
if (!empty($cid)) {
$users[] = DI::twitterUser()->createFromContactId($cid, BaseApi::getCurrentUserID())->toArray();
}
} }
} }
} }
@ -2940,7 +2842,10 @@ function api_direct_messages_new($type)
$recipient = DI::twitterUser()->createFromContactId($contacts[0]['id'], $uid)->toArray(); $recipient = DI::twitterUser()->createFromContactId($contacts[0]['id'], $uid)->toArray();
} }
} else { } else {
$recipient = api_get_user($_POST['user_id']); $cid = BaseApi::getContactIDForSearchterm($_POST['user_id']);
if (!empty($cid)) {
$recipient = DI::twitterUser()->createFromContactId($cid, $uid)->toArray();
}
} }
if (empty($recipient)) { if (empty($recipient)) {

View file

@ -25,6 +25,7 @@ use Friendica\BaseModule;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Post; use Friendica\Model\Post;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Security\BasicAuth; use Friendica\Security\BasicAuth;
@ -290,4 +291,15 @@ class BaseApi extends BaseModule
} }
} }
} }
public static function getContactIDForSearchterm($searchterm)
{
if (intval($searchterm) == 0) {
$cid = Contact::getIdForURL($searchterm, 0, false);
} else {
$cid = intval($searchterm);
}
return $cid;
}
} }

View file

@ -378,37 +378,6 @@ class ApiTest extends FixtureTest
BasicAuth::getCurrentUserID(true); BasicAuth::getCurrentUserID(true);
} }
/**
* Test the api_check_method() function.
*
* @return void
*/
public function testApiCheckMethod()
{
self::assertFalse(api_check_method('method'));
}
/**
* Test the api_check_method() function with a correct method.
*
* @return void
*/
public function testApiCheckMethodWithCorrectMethod()
{
$_SERVER['REQUEST_METHOD'] = 'method';
self::assertTrue(api_check_method('method'));
}
/**
* Test the api_check_method() function with a wildcard.
*
* @return void
*/
public function testApiCheckMethodWithWildcard()
{
self::assertTrue(api_check_method('*'));
}
/** /**
* Test the api_call() function. * Test the api_call() function.
* *
@ -784,27 +753,6 @@ class ApiTest extends FixtureTest
// self::assertSelfUser(api_get_user()); // self::assertSelfUser(api_get_user());
} }
/**
* Test the api_get_user() function with a valid user.
*
* @return void
*/
public function testApiGetUserWithCorrectUser()
{
self::assertOtherUser(api_get_user($this->otherUser['id']));
}
/**
* Test the api_get_user() function with a wrong user ID.
*
* @return void
*/
public function testApiGetUserWithWrongUser()
{
$this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
self::assertOtherUser(api_get_user($this->wrongUserId));
}
/** /**
* Test the api_get_user() function with a 0 user ID. * Test the api_get_user() function with a 0 user ID.
* *