1
1
Fork 0

"DI" calls are replaced

This commit is contained in:
Michael 2022-01-16 07:07:46 +00:00
parent f68c94db04
commit c55c42b303
11 changed files with 194 additions and 63 deletions

View file

@ -262,30 +262,6 @@ abstract class DI
// "Factory" namespace instances
//
/**
* @return Factory\Api\Friendica\Activities
*/
public static function friendicaActivities()
{
return self::$dice->create(Factory\Api\Friendica\Activities::class);
}
/**
* @return Factory\Api\Friendica\Group
*/
public static function friendicaGroup()
{
return self::$dice->create(Factory\Api\Friendica\Group::class);
}
/**
* @return Factory\Api\Friendica\Photo
*/
public static function friendicaPhoto()
{
return self::$dice->create(Factory\Api\Friendica\Photo::class);
}
/**
* @return Factory\Api\Mastodon\Account
*/

View file

@ -21,7 +21,6 @@
namespace Friendica\Factory\Api\Friendica;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Database\DBA;
use Friendica\Model\Post;
@ -32,17 +31,14 @@ use Friendica\Factory\Api\Twitter\User as TwitterUser;
class Activities extends BaseFactory
{
/** @var BaseURL */
private $baseUrl;
/** @var twitterUser entity */
private $twitterUser;
public function __construct(LoggerInterface $logger, BaseURL $baseURL, TwitterUser $twitteruser)
public function __construct(LoggerInterface $logger, TwitterUser $twitteruser)
{
parent::__construct($logger);
$this->twitterUser = $twitteruser;
$this->baseUrl = $baseURL;
}
/**

View file

@ -21,13 +21,29 @@
namespace Friendica\Module\Api\Friendica;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Photo extends BaseApi
{
/** @var FriendicaPhoto */
private $friendicaPhoto;
public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->friendicaPhoto = $friendicaPhoto;
}
protected function post(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
@ -42,7 +58,7 @@ class Photo extends BaseApi
$photo_id = $_REQUEST['photo_id'];
// prepare json/xml output with data from database for the requested photo
$data = ['photo' => DI::friendicaPhoto()->createFromId($photo_id, $scale, $uid, $type)];
$data = ['photo' => $this->friendicaPhoto->createFromId($photo_id, $scale, $uid, $type)];
$this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
}

View file

@ -21,17 +21,33 @@
namespace Friendica\Module\Api\Friendica\Photo;
use Friendica\App;
use Friendica\Core\ACL;
use Friendica\DI;
use Friendica\Model\Photo;
use Friendica\Core\L10n;
use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
use Friendica\Module\BaseApi;
use Friendica\Model\Photo;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* API endpoint: /api/friendica/photo/create
*/
class Create extends BaseApi
{
/** @var FriendicaPhoto */
private $friendicaPhoto;
public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->friendicaPhoto = $friendicaPhoto;
}
protected function post(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
@ -72,7 +88,7 @@ class Create extends BaseApi
// return success of updating or error message
if (!empty($photo)) {
$data = ['photo' => DI::friendicaPhoto()->createFromId($photo['resource_id'], null, $uid, $type)];
$data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)];
$this->response->exit('photo_create', $data, $this->parameters['extension'] ?? null);
return;
} else {

View file

@ -22,10 +22,15 @@
namespace Friendica\Module\Api\Friendica\Photo;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Model\Photo;
use Friendica\Module\Api\ApiResponse;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Returns all lists the user subscribes to.
@ -34,6 +39,17 @@ use Friendica\Model\Photo;
*/
class Lists extends BaseApi
{
/** @var FriendicaPhoto */
private $friendicaPhoto;
public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->friendicaPhoto = $friendicaPhoto;
}
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
@ -46,7 +62,7 @@ class Lists extends BaseApi
$data = ['photo' => []];
if (DBA::isResult($photos)) {
foreach ($photos as $photo) {
$element = DI::friendicaPhoto()->createFromId($photo['resource-id'], null, $uid, 'json', false);
$element = $this->friendicaPhoto->createFromId($photo['resource-id'], null, $uid, 'json', false);
$element['thumb'] = end($element['link']);
unset($element['link']);

View file

@ -21,17 +21,33 @@
namespace Friendica\Module\Api\Friendica\Photo;
use Friendica\App;
use Friendica\Core\ACL;
use Friendica\DI;
use Friendica\Model\Photo;
use Friendica\Core\L10n;
use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
use Friendica\Module\BaseApi;
use Friendica\Model\Photo;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* API endpoint: /api/friendica/photo/update
*/
class Update extends BaseApi
{
/** @var FriendicaPhoto */
private $friendicaPhoto;
public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->friendicaPhoto = $friendicaPhoto;
}
protected function post(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
@ -111,7 +127,7 @@ class Update extends BaseApi
$nothingtodo = false;
$photo = Photo::upload($uid, $_FILES['media'], $album, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc, $photo_id);
if (!empty($photo)) {
$data = ['photo' => DI::friendicaPhoto()->createFromId($photo['resource_id'], null, $uid, $type)];
$data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)];
$this->response->exit('photo_update', $data, $this->parameters['extension'] ?? null);
return;
}

View file

@ -21,12 +21,17 @@
namespace Friendica\Module\Api\Twitter\Lists;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Factory\Api\Friendica\Group as FriendicaGroup;
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Model\Group;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Update information about a group.
@ -35,6 +40,20 @@ use Friendica\Network\HTTPException;
*/
class Create extends BaseApi
{
/** @var friendicaGroup */
private $friendicaGroup;
/** @var Database */
private $dba;
public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba;
$this->friendicaGroup = $friendicaGroup;
}
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
@ -48,7 +67,7 @@ class Create extends BaseApi
}
// error message if specified group name already exists
if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) {
if ($this->dba->exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) {
throw new HTTPException\BadRequestException('group name already exists');
}
@ -59,7 +78,7 @@ class Create extends BaseApi
throw new HTTPException\BadRequestException('other API error');
}
$grp = DI::friendicaGroup()->createFromId($gid);
$grp = $this->friendicaGroup->createFromId($gid);
$this->response->exit('statuses', ['lists' => ['lists' => $grp]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
}

View file

@ -21,12 +21,17 @@
namespace Friendica\Module\Api\Twitter\Lists;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Factory\Api\Friendica\Group as FriendicaGroup;
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Model\Group;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Delete a group.
@ -35,6 +40,20 @@ use Friendica\Network\HTTPException;
*/
class Destroy extends BaseApi
{
/** @var friendicaGroup */
private $friendicaGroup;
/** @var Database */
private $dba;
public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba;
$this->friendicaGroup = $friendicaGroup;
}
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
@ -49,13 +68,13 @@ class Destroy extends BaseApi
}
// get data of the specified group id
$group = DBA::selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
$group = $this->dba->selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
// error message if specified gid is not in database
if (!$group) {
throw new HTTPException\BadRequestException('gid not available');
}
$list = DI::friendicaGroup()->createFromId($gid);
$list = $this->friendicaGroup->createFromId($gid);
if (Group::remove($gid)) {
$this->response->exit('statuses', ['lists' => ['lists' => $list]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));

View file

@ -21,10 +21,15 @@
namespace Friendica\Module\Api\Twitter\Lists;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Factory\Api\Friendica\Group as FriendicaGroup;
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Module\Api\ApiResponse;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Returns all groups the user owns.
@ -33,17 +38,30 @@ use Friendica\Model\Contact;
*/
class Ownership extends BaseApi
{
/** @var friendicaGroup */
private $friendicaGroup;
/** @var Database */
private $dba;
public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba;
$this->friendicaGroup = $friendicaGroup;
}
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
$uid = BaseApi::getCurrentUserID();
$groups = DBA::select('group', [], ['deleted' => false, 'uid' => $uid]);
$groups = $this->dba->select('group', [], ['deleted' => false, 'uid' => $uid]);
// loop through all groups
$lists = [];
foreach ($groups as $group) {
$lists[] = DI::friendicaGroup()->createFromId($group['id']);
$lists[] = $this->friendicaGroup->createFromId($group['id']);
}
$this->response->exit('statuses', ['lists' => ['lists' => $lists]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));

View file

@ -21,12 +21,18 @@
namespace Friendica\Module\Api\Twitter\Lists;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Factory\Api\Twitter\Status as TwitterStatus;
use Friendica\Module\BaseApi;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Post;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Returns recent statuses from users in the specified group.
@ -35,13 +41,27 @@ use Friendica\Network\HTTPException\BadRequestException;
*/
class Statuses extends BaseApi
{
/** @var TwitterStatus */
private $twitterStatus;
/** @var Database */
private $dba;
public function __construct(Database $dba, TwitterStatus $twitterStatus, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba;
$this->twitterStatus = $twitterStatus;
}
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
$uid = BaseApi::getCurrentUserID();
if (empty($request['list_id'])) {
throw new BadRequestException('list_id not specified');
throw new HTTPException\BadRequestException('list_id not specified');
}
// params
@ -54,7 +74,7 @@ class Statuses extends BaseApi
$start = max(0, ($page - 1) * $count);
$groups = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $request['list_id']]);
$groups = $this->dba->selectToArray('group_member', ['contact-id'], ['gid' => $request['list_id']]);
$gids = array_column($groups, 'contact-id');
$condition = ['uid' => $uid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'contact-id' => $gids];
$condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
@ -78,10 +98,10 @@ class Statuses extends BaseApi
$include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true');
$items = [];
while ($status = DBA::fetch($statuses)) {
$items[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
while ($status = $this->dba->fetch($statuses)) {
$items[] = $this->twitterStatus->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
}
DBA::close($statuses);
$this->dba->close($statuses);
$this->response->exit('statuses', ['status' => $items], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
}

View file

@ -21,12 +21,17 @@
namespace Friendica\Module\Api\Twitter\Lists;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Factory\Api\Friendica\Group as FriendicaGroup;
use Friendica\Module\BaseApi;
use Friendica\Model\Contact;
use Friendica\Model\Group;
use Friendica\Module\Api\ApiResponse;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* Update information about a group.
@ -35,6 +40,20 @@ use Friendica\Network\HTTPException;
*/
class Update extends BaseApi
{
/** @var friendicaGroup */
private $friendicaGroup;
/** @var Database */
private $dba;
public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
{
parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->dba = $dba;
$this->friendicaGroup = $friendicaGroup;
}
protected function rawContent(array $request = [])
{
BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
@ -50,14 +69,14 @@ class Update extends BaseApi
}
// get data of the specified group id
$group = DBA::selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
$group = $this->dba->selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
// error message if specified gid is not in database
if (!$group) {
throw new HTTPException\BadRequestException('gid not available');
}
if (Group::update($gid, $name)) {
$list = DI::friendicaGroup()->createFromId($gid);
$list = $this->friendicaGroup->createFromId($gid);
$this->response->exit('statuses', ['lists' => ['lists' => $list]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
}