3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Api\Friendica\DirectMessages;
25 use Friendica\Core\L10n;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
28 use Friendica\Factory\Api\Twitter\DirectMessage;
29 use Friendica\Module\Api\ApiResponse;
30 use Friendica\Module\BaseApi;
31 use Friendica\Util\Profiler;
32 use Psr\Log\LoggerInterface;
35 * search for direct_messages containing a searchstring through api
37 * API endpoint: api/friendica/direct_messages_search
39 class Search extends BaseApi
44 /** @var DirectMessage */
45 private $directMessage;
47 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 = [])
49 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52 $this->directMessage = $directMessage;
55 protected function rawContent(array $request = [])
57 self::checkAllowedScope(self::SCOPE_READ);
58 $uid = self::getCurrentUserID();
60 $request = $this->getRequest([
64 // error if no searchstring specified
65 if ($request['searchstring'] == '') {
66 $answer = ['result' => 'error', 'message' => 'searchstring not specified'];
67 $this->response->exit('direct_message_search', ['$result' => $answer], $this->parameters['extension'] ?? null);
71 // get data for the specified searchstring
72 $mails = $this->dba->selectToArray('mail', ['id'], ["`uid` = ? AND `body` LIKE ?", $uid, '%' . $request['searchstring'] . '%'], ['order' => ['id' => true]]);
74 // message if nothing was found
75 if (!DBA::isResult($mails)) {
76 $success = ['success' => false, 'search_results' => 'nothing found'];
79 foreach ($mails as $mail) {
80 $ret[] = $this->directMessage->createFromMailId($mail['id'], $uid, $this->getRequestValue($request, 'getText', ''));
82 $success = ['success' => true, 'search_results' => $ret];
85 $this->response->exit('direct_message_search', ['$result' => $success], $this->parameters['extension'] ?? null);