]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/DirectMessagesEndpoint.php
Merge remote-tracking branch 'upstream/2021.12-rc' into api-direct-messages
[friendica.git] / src / Module / Api / Twitter / DirectMessagesEndpoint.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Contact;
27 use Friendica\Module\BaseApi;
28
29 abstract class DirectMessagesEndpoint extends BaseApi
30 {
31         /**
32          */
33         protected function getMessages(array $request, int $uid, array $condition)
34         {
35                 // params
36                 $count    = filter_var($request['count'] ?? 20,                FILTER_VALIDATE_INT, ['options' => ['max_range' => 100]]);
37                 $page     = filter_var($request['page'] ?? 1,                  FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
38                 $since_id = filter_var($request['since_id'] ?? 0,              FILTER_VALIDATE_INT);
39                 $max_id   = filter_var($request['max_id'] ?? 0,                FILTER_VALIDATE_INT);
40                 $min_id   = filter_var($request['min_id'] ?? 0,                FILTER_VALIDATE_INT);
41                 $verbose  = filter_var($request['friendica_verbose'] ?? false, FILTER_VALIDATE_BOOLEAN);
42
43                 // pagination
44                 $start = max(0, ($page - 1) * $count);
45
46                 $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
47
48                 if (!empty($max_id)) {
49                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
50                 }
51
52                 if (!empty($since_id)) {
53                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
54                 }
55
56                 if (!empty($min_id)) {
57                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
58
59                         $params['order'] = ['id'];
60                 }
61
62                 $cid = BaseApi::getContactIDForSearchterm($_REQUEST['screen_name'] ?? '', $_REQUEST['profileurl'] ?? '', $_REQUEST['user_id'] ?? 0, 0);
63                 if (!empty($cid)) {
64                         $cdata = Contact::getPublicAndUserContactID($cid, $uid);
65                         if (!empty($cdata['user'])) {
66                                 $condition = DBA::mergeConditions($condition, ["`contact-id` = ?", $cdata['user']]);
67                         }
68                 }
69
70                 $condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]);
71
72                 $mails = DBA::selectToArray('mail', ['id'], $condition, $params);
73                 if ($verbose && !DBA::isResult($mails)) {
74                         $answer = ['result' => 'error', 'message' => 'no mails available'];
75                         $this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null);
76                         exit;
77                 }
78
79                 $ids = array_column($mails, 'id');
80
81                 if (!empty($min_id)) {
82                         $ids = array_reverse($ids);
83                 }
84
85                 $ret = [];
86                 foreach ($ids as $id) {
87                         $ret[] = DI::twitterDirectMessage()->createFromMailId($id, $uid, $request['getText'] ?? '');
88                 }
89
90                 self::setLinkHeader();
91
92                 $this->response->exit('direct-messages', ['direct_message' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
93         }
94 }