]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/DirectMessages/Search.php
Merge pull request #11141 from urbalazs/language-names
[friendica.git] / src / Module / Api / Friendica / DirectMessages / Search.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\DirectMessages;
23
24 use Friendica\App;
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;
33
34 /**
35  * search for direct_messages containing a searchstring through api
36  *
37  * API endpoint: api/friendica/direct_messages_search
38  */
39 class Search extends BaseApi
40 {
41         /** @var Database */
42         private $dba;
43
44         /** @var DirectMessage */
45         private $directMessage;
46
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 = [])
48         {
49                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->dba           = $dba;
52                 $this->directMessage = $directMessage;
53         }
54
55         protected function rawContent(array $request = [])
56         {
57                 self::checkAllowedScope(self::SCOPE_READ);
58                 $uid = self::getCurrentUserID();
59
60                 $request = $this->getRequest([
61                         'searchstring' => '',
62                 ], $request);
63
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);
68                         return;
69                 }
70
71                 // get data for the specified searchstring
72                 $mails = $this->dba->selectToArray('mail', ['id'], ["`uid` = ? AND `body` LIKE ?", $uid, '%' . $request['searchstring'] . '%'], ['order' => ['id' => true]]);
73
74                 // message if nothing was found
75                 if (!DBA::isResult($mails)) {
76                         $success = ['success' => false, 'search_results' => 'nothing found'];
77                 } else {
78                         $ret = [];
79                         foreach ($mails as $mail) {
80                                 $ret[] = $this->directMessage->createFromMailId($mail['id'], $uid, $request['getText'] ?? '');
81                         }
82                         $success = ['success' => true, 'search_results' => $ret];
83                 }
84
85                 $this->response->exit('direct_message_search', ['$result' => $success], $this->parameters['extension'] ?? null);
86         }
87 }