]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/DirectMessagesEndpoint.php
Merge pull request #12854 from annando/api-edit
[friendica.git] / src / Module / Api / Twitter / DirectMessagesEndpoint.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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          * @return void
59          */
60         protected function getMessages(array $request, int $uid, array $condition)
61         {
62                 // params
63                 $count    = $this->getRequestValue($request, 'count', 20, 1, 100);
64                 $page     = $this->getRequestValue($request, 'page', 1, 1);
65                 $since_id = $this->getRequestValue($request, 'since_id', 0, 0);
66                 $max_id   = $this->getRequestValue($request, 'max_id', 0, 0);
67                 $min_id   = $this->getRequestValue($request, 'min_id', 0, 0);
68                 $verbose  = $this->getRequestValue($request, 'friendica_verbose', false);
69
70                 // pagination
71                 $start = max(0, ($page - 1) * $count);
72
73                 $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
74
75                 if (!empty($max_id)) {
76                         $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
77                 }
78
79                 if (!empty($since_id)) {
80                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
81                 }
82
83                 if (!empty($min_id)) {
84                         $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
85
86                         $params['order'] = ['id'];
87                 }
88
89                 $cid = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'screen_name', ''), $this->getRequestValue($request, 'profileurl', ''), $this->getRequestValue($request, 'user_id', 0), 0);
90                 if (!empty($cid)) {
91                         $cdata = Contact::getPublicAndUserContactID($cid, $uid);
92                         if (!empty($cdata['user'])) {
93                                 $condition = DBA::mergeConditions($condition, ["`contact-id` = ?", $cdata['user']]);
94                         }
95                 }
96
97                 $condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]);
98
99                 $mails = $this->dba->selectToArray('mail', ['id'], $condition, $params);
100                 if ($verbose && !DBA::isResult($mails)) {
101                         $answer = ['result' => 'error', 'message' => 'no mails available'];
102                         $this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null);
103                         return;
104                 }
105
106                 $ids = array_column($mails, 'id');
107
108                 if (!empty($min_id)) {
109                         $ids = array_reverse($ids);
110                 }
111
112                 $ret = [];
113                 foreach ($ids as $id) {
114                         $ret[] = $this->directMessage->createFromMailId($id, $uid, $this->getRequestValue($request, 'getText', ''));
115                 }
116
117                 self::setLinkHeader();
118
119                 $this->response->exit('direct-messages', ['direct_message' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
120         }
121 }