]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/DirectMessagesEndpoint.php
Issue 11309: improved check for wanted posts
[friendica.git] / src / Module / Api / Twitter / DirectMessagesEndpoint.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\Twitter;
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\Model\Contact;
30 use Friendica\Module\Api\ApiResponse;
31 use Friendica\Module\BaseApi;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
34
35 abstract class DirectMessagesEndpoint extends BaseApi
36 {
37         /** @var Database */
38         private $dba;
39
40         /** @var DirectMessage */
41         private $directMessage;
42
43         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 = [])
44         {
45                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
46
47                 $this->dba           = $dba;
48                 $this->directMessage = $directMessage;
49         }
50
51         /**
52          * Handles a direct messages endpoint with the given condition
53          *
54          * @param array $request
55          * @param int   $uid
56          * @param array $condition
57          */
58         protected function getMessages(array $request, int $uid, array $condition)
59         {
60                 // params
61                 $count    = $this->getRequestValue($request, 'count', 20, 1, 100);
62                 $page     = $this->getRequestValue($request, 'page', 1, 1);
63                 $since_id = $this->getRequestValue($request, 'since_id', 0, 0);
64                 $max_id   = $this->getRequestValue($request, 'max_id', 0, 0);
65                 $min_id   = $this->getRequestValue($request, 'min_id', 0, 0);
66                 $verbose  = $this->getRequestValue($request, 'friendica_verbose', false);
67
68                 // pagination
69                 $start = max(0, ($page - 1) * $count);
70
71                 $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
72
73                 if (!empty($max_id)) {
74                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
75                 }
76
77                 if (!empty($since_id)) {
78                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
79                 }
80
81                 if (!empty($min_id)) {
82                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
83
84                         $params['order'] = ['id'];
85                 }
86
87                 $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, 0);
88                 if (!empty($cid)) {
89                         $cdata = Contact::getPublicAndUserContactID($cid, $uid);
90                         if (!empty($cdata['user'])) {
91                                 $condition = DBA::mergeConditions($condition, ["`contact-id` = ?", $cdata['user']]);
92                         }
93                 }
94
95                 $condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]);
96
97                 $mails = $this->dba->selectToArray('mail', ['id'], $condition, $params);
98                 if ($verbose && !DBA::isResult($mails)) {
99                         $answer = ['result' => 'error', 'message' => 'no mails available'];
100                         $this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null);
101                         return;
102                 }
103
104                 $ids = array_column($mails, 'id');
105
106                 if (!empty($min_id)) {
107                         $ids = array_reverse($ids);
108                 }
109
110                 $ret = [];
111                 foreach ($ids as $id) {
112                         $ret[] = $this->directMessage->createFromMailId($id, $uid, $request['getText'] ?? '');
113                 }
114
115                 self::setLinkHeader();
116
117                 $this->response->exit('direct-messages', ['direct_message' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
118         }
119 }