From 465ee3718d489d96f32a6821566025941594856b Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 Dec 2021 08:22:16 +0000 Subject: [PATCH] Use dynamic functions --- .../Api/Friendica/DirectMessages/Search.php | 28 ++++++++++++++--- .../Api/Twitter/DirectMessagesEndpoint.php | 30 +++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/Module/Api/Friendica/DirectMessages/Search.php b/src/Module/Api/Friendica/DirectMessages/Search.php index 37cf7779b..c0322a95e 100644 --- a/src/Module/Api/Friendica/DirectMessages/Search.php +++ b/src/Module/Api/Friendica/DirectMessages/Search.php @@ -21,9 +21,15 @@ namespace Friendica\Module\Api\Friendica\DirectMessages; +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; use Friendica\Database\DBA; -use Friendica\DI; +use Friendica\Factory\Api\Twitter\DirectMessage; +use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; /** * search for direct_messages containing a searchstring through api @@ -32,6 +38,20 @@ use Friendica\Module\BaseApi; */ class Search extends BaseApi { + /** @var Database */ + private $dba; + + /** @var DirectMessage */ + private $directMessage; + + public function __construct(DirectMessage $directMessage, Database $dba, 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->directMessage = $directMessage; + } + protected function rawContent(array $request = []) { self::checkAllowedScope(self::SCOPE_READ); @@ -45,11 +65,11 @@ class Search extends BaseApi if ($request['searchstring'] == '') { $answer = ['result' => 'error', 'message' => 'searchstring not specified']; $this->response->exit('direct_message_search', ['$result' => $answer], $this->parameters['extension'] ?? null); - exit; + return; } // get data for the specified searchstring - $mails = DBA::selectToArray('mail', ['id'], ["`uid` = ? AND `body` LIKE ?", $uid, '%' . $request['searchstring'] . '%'], ['order' => ['id' => true]]); + $mails = $this->dba->selectToArray('mail', ['id'], ["`uid` = ? AND `body` LIKE ?", $uid, '%' . $request['searchstring'] . '%'], ['order' => ['id' => true]]); // message if nothing was found if (!DBA::isResult($mails)) { @@ -59,7 +79,7 @@ class Search extends BaseApi } else { $ret = []; foreach ($mails as $mail) { - $ret[] = DI::twitterDirectMessage()->createFromMailId($mail['id'], $uid, $request['getText'] ?? ''); + $ret[] = $this->directMessage->createFromMailId($mail['id'], $uid, $request['getText'] ?? ''); } $success = ['success' => true, 'search_results' => $ret]; } diff --git a/src/Module/Api/Twitter/DirectMessagesEndpoint.php b/src/Module/Api/Twitter/DirectMessagesEndpoint.php index ba45e0ab4..0a51b0cbf 100644 --- a/src/Module/Api/Twitter/DirectMessagesEndpoint.php +++ b/src/Module/Api/Twitter/DirectMessagesEndpoint.php @@ -21,13 +21,33 @@ namespace Friendica\Module\Api\Twitter; +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; use Friendica\Database\DBA; -use Friendica\DI; +use Friendica\Factory\Api\Twitter\DirectMessage; use Friendica\Model\Contact; +use Friendica\Module\Api\ApiResponse; use Friendica\Module\BaseApi; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; abstract class DirectMessagesEndpoint extends BaseApi { + /** @var Database */ + private $dba; + + /** @var DirectMessage */ + private $directMessage; + + public function __construct(DirectMessage $directMessage, Database $dba, 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->directMessage = $directMessage; + } + /** * Handles a direct messages endpoint with the given condition * @@ -64,7 +84,7 @@ abstract class DirectMessagesEndpoint extends BaseApi $params['order'] = ['id']; } - $cid = BaseApi::getContactIDForSearchterm($_REQUEST['screen_name'] ?? '', $_REQUEST['profileurl'] ?? '', $_REQUEST['user_id'] ?? 0, 0); + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $_REQUEST['user_id'] ?? 0, 0); if (!empty($cid)) { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (!empty($cdata['user'])) { @@ -74,11 +94,11 @@ abstract class DirectMessagesEndpoint extends BaseApi $condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]); - $mails = DBA::selectToArray('mail', ['id'], $condition, $params); + $mails = $this->dba->selectToArray('mail', ['id'], $condition, $params); if ($verbose && !DBA::isResult($mails)) { $answer = ['result' => 'error', 'message' => 'no mails available']; $this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null); - exit; + return; } $ids = array_column($mails, 'id'); @@ -89,7 +109,7 @@ abstract class DirectMessagesEndpoint extends BaseApi $ret = []; foreach ($ids as $id) { - $ret[] = DI::twitterDirectMessage()->createFromMailId($id, $uid, $request['getText'] ?? ''); + $ret[] = $this->directMessage->createFromMailId($id, $uid, $request['getText'] ?? ''); } self::setLinkHeader();