Merge pull request #10987 from annando/api4
API: moved classes / unified user array creation
This commit is contained in:
@@ -52,4 +52,9 @@ class User extends BaseFactory
|
||||
|
||||
return new \Friendica\Object\Api\Twitter\User($publicContact, $apcontact, $userContact, $skip_status, $include_user_entities);
|
||||
}
|
||||
|
||||
public function createFromUserId(int $uid, $skip_status = false, $include_user_entities = true)
|
||||
{
|
||||
return $this->createFromContactId(Contact::getPublicIdByUserId($uid), $uid, $skip_status, $include_user_entities);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ namespace Friendica\Module\Api\Friendica\Profile;
|
||||
use Friendica\Profile\ProfileField\Collection\ProfileFields;
|
||||
use Friendica\Content\Text\BBCode;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Profile;
|
||||
use Friendica\Module\BaseApi;
|
||||
use Friendica\Network\HTTPException;
|
||||
@@ -55,13 +54,10 @@ class Show extends BaseApi
|
||||
$profiles[] = $profile;
|
||||
}
|
||||
|
||||
// return settings, authenticated user and profiles data
|
||||
$self = Contact::selectFirst(['nurl'], ['uid' => $uid, 'self' => true]);
|
||||
|
||||
$result = [
|
||||
'multi_profiles' => false,
|
||||
'global_dir' => $directory,
|
||||
'friendica_owner' => self::getUser($self['nurl']),
|
||||
'friendica_owner' => DI::twitterUser()->createFromUserId($uid),
|
||||
'profiles' => $profiles
|
||||
];
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ class RateLimitStatus extends BaseApi
|
||||
'reset_time_in_seconds' => strtotime('now + 1 hour'),
|
||||
'remaining_hits' => '150',
|
||||
'hourly_limit' => '150',
|
||||
'reset_time' => api_date(DateTimeFormat::utc('now + 1 hour', DateTimeFormat::ATOM)),
|
||||
'reset_time' => DateTimeFormat::utc('now + 1 hour', DateTimeFormat::API),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Module\Api\Twitter;
|
||||
namespace Friendica\Module\Api\Twitter\Followers;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
|
||||
/**
|
||||
* @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids
|
||||
@@ -47,6 +48,8 @@ class FollowersIds extends ContactEndpoint
|
||||
'default' => 1,
|
||||
]]);
|
||||
|
||||
// @todo Use Model\Contact\Relation::listFollowers($cid, $condition, $count);
|
||||
|
||||
System::jsonExit(self::ids(
|
||||
[Contact::FOLLOWER, Contact::FRIEND],
|
||||
self::getUid($contact_id, $screen_name),
|
||||
@@ -19,10 +19,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Module\Api\Twitter;
|
||||
namespace Friendica\Module\Api\Twitter\Followers;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
|
||||
/**
|
||||
* @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-list
|
||||
@@ -49,6 +50,7 @@ class FollowersList extends ContactEndpoint
|
||||
'default' => 1,
|
||||
]]);
|
||||
|
||||
// @todo Use Model\Contact\Relation::listFollowers($cid, $condition, $count);
|
||||
|
||||
System::jsonExit(self::list(
|
||||
[Contact::FOLLOWER, Contact::FRIEND],
|
||||
@@ -19,15 +19,16 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Module\Api\Twitter;
|
||||
namespace Friendica\Module\Api\Twitter\Friends;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
|
||||
/**
|
||||
* @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids
|
||||
*/
|
||||
class FriendsIds extends ContactEndpoint
|
||||
class Ids extends ContactEndpoint
|
||||
{
|
||||
public function rawContent()
|
||||
{
|
||||
@@ -47,6 +48,8 @@ class FriendsIds extends ContactEndpoint
|
||||
'default' => 1,
|
||||
]]);
|
||||
|
||||
// @todo Use Model\Contact\Relation::listFollows($cid, $condition, $count);
|
||||
|
||||
System::jsonExit(self::ids(
|
||||
[Contact::SHARING, Contact::FRIEND],
|
||||
self::getUid($contact_id, $screen_name),
|
||||
@@ -19,15 +19,16 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Module\Api\Twitter;
|
||||
namespace Friendica\Module\Api\Twitter\Friends;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Module\Api\Twitter\ContactEndpoint;
|
||||
|
||||
/**
|
||||
* @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-list
|
||||
*/
|
||||
class FriendsList extends ContactEndpoint
|
||||
class Lists extends ContactEndpoint
|
||||
{
|
||||
public function rawContent()
|
||||
{
|
||||
@@ -49,6 +50,8 @@ class FriendsList extends ContactEndpoint
|
||||
'default' => 1,
|
||||
]]);
|
||||
|
||||
// @todo Use Model\Contact\Relation::listFollows($cid, $condition, $count);
|
||||
|
||||
System::jsonExit(self::list(
|
||||
[Contact::SHARING, Contact::FRIEND],
|
||||
self::getUid($contact_id, $screen_name),
|
||||
@@ -25,16 +25,13 @@ use Friendica\BaseModule;
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Core\System;
|
||||
use Friendica\DI;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Post;
|
||||
use Friendica\Network\HTTPException;
|
||||
use Friendica\Security\BasicAuth;
|
||||
use Friendica\Security\OAuth;
|
||||
use Friendica\Util\Arrays;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\HTTPInputData;
|
||||
use Friendica\Util\XML;
|
||||
|
||||
require_once __DIR__ . '/../../include/api.php';
|
||||
|
||||
class BaseApi extends BaseModule
|
||||
{
|
||||
@@ -295,18 +292,14 @@ class BaseApi extends BaseModule
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user info array.
|
||||
*
|
||||
* @param int|string $contact_id Contact ID or URL
|
||||
* @return array|bool
|
||||
* @throws HTTPException\BadRequestException
|
||||
* @throws HTTPException\InternalServerErrorException
|
||||
* @throws HTTPException\UnauthorizedException
|
||||
* @throws \ImagickException
|
||||
*/
|
||||
protected static function getUser($contact_id = null)
|
||||
public static function getContactIDForSearchterm($searchterm)
|
||||
{
|
||||
return api_get_user($contact_id);
|
||||
if (intval($searchterm) == 0) {
|
||||
$cid = Contact::getIdForURL($searchterm, 0, false);
|
||||
} else {
|
||||
$cid = intval($searchterm);
|
||||
}
|
||||
|
||||
return $cid;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ namespace Friendica\Object\Api\Twitter;
|
||||
use Friendica\BaseDataTransferObject;
|
||||
use Friendica\Content\ContactSelector;
|
||||
use Friendica\Content\Text\BBCode;
|
||||
use Friendica\Core\Protocol;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\Proxy;
|
||||
|
||||
/**
|
||||
* @see https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object
|
||||
@@ -79,6 +83,14 @@ class User extends BaseDataTransferObject
|
||||
/** @var string */
|
||||
protected $withheld_scope;
|
||||
|
||||
/**
|
||||
* Missing fields:
|
||||
*
|
||||
* - profile_sidebar_fill_color
|
||||
* - profile_link_color
|
||||
* - profile_background_color
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param array $publicContact Full contact table record with uid = 0
|
||||
* @param array $apcontact Optional full apcontact table record
|
||||
@@ -89,9 +101,11 @@ class User extends BaseDataTransferObject
|
||||
*/
|
||||
public function __construct(array $publicContact, array $apcontact = [], array $userContact = [], $skip_status = false, $include_user_entities = true)
|
||||
{
|
||||
$uid = $userContact['uid'] ?? 0;
|
||||
|
||||
$this->id = (int)$publicContact['id'];
|
||||
$this->id_str = (string) $publicContact['id'];
|
||||
$this->name = $publicContact['name'];
|
||||
$this->name = $publicContact['name'] ?: $publicContact['nick'];
|
||||
$this->screen_name = $publicContact['nick'] ?: $publicContact['name'];
|
||||
$this->location = $publicContact['location'] ?:
|
||||
ContactSelector::networkToName($publicContact['network'], $publicContact['url'], $publicContact['protocol']);
|
||||
@@ -106,16 +120,16 @@ class User extends BaseDataTransferObject
|
||||
unset($this->entities);
|
||||
}
|
||||
$this->description = BBCode::toPlaintext($publicContact['about']);
|
||||
$this->profile_image_url_https = $userContact['avatar'] ?? $publicContact['avatar'];
|
||||
$this->profile_image_url_https = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_MICRO);
|
||||
$this->protected = false;
|
||||
$this->followers_count = $apcontact['followers_count'] ?? 0;
|
||||
$this->friends_count = $apcontact['following_count'] ?? 0;
|
||||
$this->listed_count = 0;
|
||||
$this->created_at = api_date($publicContact['created']);
|
||||
$this->created_at = DateTimeFormat::utc($publicContact['created'], DateTimeFormat::API);
|
||||
$this->favourites_count = 0;
|
||||
$this->verified = false;
|
||||
$this->verified = $uid != 0;
|
||||
$this->statuses_count = $apcontact['statuses_count'] ?? 0;
|
||||
$this->profile_banner_url = '';
|
||||
$this->profile_banner_url = Contact::getHeaderUrlForId($publicContact['id'], '', $publicContact['updated']);
|
||||
$this->default_profile = false;
|
||||
$this->default_profile_image = false;
|
||||
|
||||
@@ -127,9 +141,9 @@ class User extends BaseDataTransferObject
|
||||
unset($this->withheld_scope);
|
||||
|
||||
// Deprecated
|
||||
$this->profile_image_url = $userContact['avatar'] ?? $publicContact['avatar'];
|
||||
$this->profile_image_url_profile_size = $publicContact['thumb'];
|
||||
$this->profile_image_url_large = $publicContact['photo'];
|
||||
$this->profile_image_url = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_MICRO);
|
||||
$this->profile_image_url_profile_size = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_THUMB);
|
||||
$this->profile_image_url_large = Contact::getAvatarUrlForUrl($publicContact['url'], $uid, Proxy::SIZE_LARGE);
|
||||
$this->utc_offset = 0;
|
||||
$this->time_zone = 'UTC';
|
||||
$this->geo_enabled = false;
|
||||
@@ -137,17 +151,17 @@ class User extends BaseDataTransferObject
|
||||
$this->contributors_enabled = false;
|
||||
$this->is_translator = false;
|
||||
$this->is_translation_enabled = false;
|
||||
$this->following = false;
|
||||
$this->following = in_array($userContact['rel'] ?? Contact::NOTHING, [Contact::FOLLOWER, Contact::FRIEND]);
|
||||
$this->follow_request_sent = false;
|
||||
$this->statusnet_blocking = false;
|
||||
$this->notifications = false;
|
||||
|
||||
// Friendica-specific
|
||||
$this->uid = (int)$userContact['uid'] ?? 0;
|
||||
$this->cid = (int)$userContact['id'] ?? 0;
|
||||
$this->uid = (int)$uid;
|
||||
$this->cid = (int)($userContact['id'] ?? 0);
|
||||
$this->pid = (int)$publicContact['id'];
|
||||
$this->self = (boolean)$userContact['self'] ?? false;
|
||||
$this->network = $publicContact['network'];
|
||||
$this->self = (boolean)($userContact['self'] ?? false);
|
||||
$this->network = $publicContact['network'] ?: Protocol::DFRN;
|
||||
$this->statusnet_profile_url = $publicContact['url'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,11 @@ class BasicAuth
|
||||
return (int)self::$current_user_id;
|
||||
}
|
||||
|
||||
public static function setCurrentUserID(int $uid = null)
|
||||
{
|
||||
self::$current_user_id = $uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a dummy application token
|
||||
*
|
||||
@@ -118,7 +123,6 @@ class BasicAuth
|
||||
private static function getUserIdByAuth(bool $do_login = true):int
|
||||
{
|
||||
$a = DI::app();
|
||||
Session::set('allow_api', false);
|
||||
self::$current_user_id = 0;
|
||||
|
||||
// workaround for HTTP-auth in CGI mode
|
||||
@@ -173,7 +177,10 @@ class BasicAuth
|
||||
return 0;
|
||||
}
|
||||
Logger::debug('Access denied', ['parameters' => $_SERVER]);
|
||||
header('WWW-Authenticate: Basic realm="Friendica"');
|
||||
// Checking for commandline for the tests, we have to avoid to send a header
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
header('WWW-Authenticate: Basic realm="Friendica"');
|
||||
}
|
||||
throw new UnauthorizedException("This API requires login");
|
||||
}
|
||||
|
||||
@@ -182,15 +189,10 @@ class BasicAuth
|
||||
|
||||
DI::auth()->setForUser($a, $record, false, false, $login_refresh);
|
||||
|
||||
Session::set('allow_api', true);
|
||||
|
||||
Hook::callAll('logged_in', $record);
|
||||
|
||||
if (Session::get('allow_api')) {
|
||||
self::$current_user_id = local_user();
|
||||
} else {
|
||||
self::$current_user_id = 0;
|
||||
}
|
||||
self::$current_user_id = local_user();
|
||||
|
||||
return self::$current_user_id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ class DateTimeFormat
|
||||
const MYSQL = 'Y-m-d H:i:s';
|
||||
const HTTP = 'D, d M Y H:i:s \G\M\T';
|
||||
const JSON = 'Y-m-d\TH:i:s.v\Z';
|
||||
const API = 'D M d H:i:s +0000 Y';
|
||||
|
||||
static $localTimezone = 'UTC';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user