Use dynamic functions

This commit is contained in:
Michael 2021-12-19 08:22:16 +00:00
parent 23f8d37bcd
commit 465ee3718d
2 changed files with 49 additions and 9 deletions

View file

@ -21,9 +21,15 @@
namespace Friendica\Module\Api\Friendica\DirectMessages; namespace Friendica\Module\Api\Friendica\DirectMessages;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\Factory\Api\Twitter\DirectMessage;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/** /**
* search for direct_messages containing a searchstring through api * search for direct_messages containing a searchstring through api
@ -32,6 +38,20 @@ use Friendica\Module\BaseApi;
*/ */
class Search extends 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 = []) protected function rawContent(array $request = [])
{ {
self::checkAllowedScope(self::SCOPE_READ); self::checkAllowedScope(self::SCOPE_READ);
@ -45,11 +65,11 @@ class Search extends BaseApi
if ($request['searchstring'] == '') { if ($request['searchstring'] == '') {
$answer = ['result' => 'error', 'message' => 'searchstring not specified']; $answer = ['result' => 'error', 'message' => 'searchstring not specified'];
$this->response->exit('direct_message_search', ['$result' => $answer], $this->parameters['extension'] ?? null); $this->response->exit('direct_message_search', ['$result' => $answer], $this->parameters['extension'] ?? null);
exit; return;
} }
// get data for the specified searchstring // 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 // message if nothing was found
if (!DBA::isResult($mails)) { if (!DBA::isResult($mails)) {
@ -59,7 +79,7 @@ class Search extends BaseApi
} else { } else {
$ret = []; $ret = [];
foreach ($mails as $mail) { 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]; $success = ['success' => true, 'search_results' => $ret];
} }

View file

@ -21,13 +21,33 @@
namespace Friendica\Module\Api\Twitter; namespace Friendica\Module\Api\Twitter;
use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\Factory\Api\Twitter\DirectMessage;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\BaseApi; use Friendica\Module\BaseApi;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
abstract class DirectMessagesEndpoint extends BaseApi 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 * Handles a direct messages endpoint with the given condition
* *
@ -64,7 +84,7 @@ abstract class DirectMessagesEndpoint extends BaseApi
$params['order'] = ['id']; $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)) { if (!empty($cid)) {
$cdata = Contact::getPublicAndUserContactID($cid, $uid); $cdata = Contact::getPublicAndUserContactID($cid, $uid);
if (!empty($cdata['user'])) { if (!empty($cdata['user'])) {
@ -74,11 +94,11 @@ abstract class DirectMessagesEndpoint extends BaseApi
$condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]); $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)) { if ($verbose && !DBA::isResult($mails)) {
$answer = ['result' => 'error', 'message' => 'no mails available']; $answer = ['result' => 'error', 'message' => 'no mails available'];
$this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null); $this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null);
exit; return;
} }
$ids = array_column($mails, 'id'); $ids = array_column($mails, 'id');
@ -89,7 +109,7 @@ abstract class DirectMessagesEndpoint extends BaseApi
$ret = []; $ret = [];
foreach ($ids as $id) { foreach ($ids as $id) {
$ret[] = DI::twitterDirectMessage()->createFromMailId($id, $uid, $request['getText'] ?? ''); $ret[] = $this->directMessage->createFromMailId($id, $uid, $request['getText'] ?? '');
} }
self::setLinkHeader(); self::setLinkHeader();