3 * @copyright Copyright (C) 2010-2021, 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\Twitter;
25 use Friendica\Core\L10n;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Module\Api\ApiResponse;
30 use Friendica\Module\BaseApi;
31 use Friendica\Util\Profiler;
32 use Friendica\Factory\Api\Twitter\DirectMessage;
33 use Psr\Log\LoggerInterface;
35 abstract class DirectMessagesEndpoint extends BaseApi
39 protected function getMessages(array $request, int $uid, array $condition)
42 $count = filter_var($request['count'] ?? 20, FILTER_VALIDATE_INT, ['options' => ['max_range' => 100]]);
43 $page = filter_var($request['page'] ?? 1, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
44 $since_id = filter_var($request['since_id'] ?? 0, FILTER_VALIDATE_INT);
45 $max_id = filter_var($request['max_id'] ?? 0, FILTER_VALIDATE_INT);
46 $min_id = filter_var($request['min_id'] ?? 0, FILTER_VALIDATE_INT);
47 $verbose = filter_var($request['friendica_verbose'] ?? false, FILTER_VALIDATE_BOOLEAN);
50 $start = max(0, ($page - 1) * $count);
52 $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
54 if (!empty($max_id)) {
55 $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
58 if (!empty($since_id)) {
59 $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
62 if (!empty($min_id)) {
63 $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
65 $params['order'] = ['id'];
68 $cid = BaseApi::getContactIDForSearchterm($_REQUEST['screen_name'] ?? '', $_REQUEST['profileurl'] ?? '', $_REQUEST['user_id'] ?? 0, 0);
70 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
71 if (!empty($cdata['user'])) {
72 $condition = DBA::mergeConditions($condition, ["`contact-id` = ?", $cdata['user']]);
76 $condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]);
78 $mails = DBA::selectToArray('mail', ['id'], $condition, $params);
79 if ($verbose && !DBA::isResult($mails)) {
80 $answer = ['result' => 'error', 'message' => 'no mails available'];
81 $this->response->exit('direct-messages', ['direct_message' => $answer], $this->parameters['extension'] ?? null);
85 $ids = array_column($mails, 'id');
87 if (!empty($min_id)) {
88 $ids = array_reverse($ids);
92 foreach ($ids as $id) {
93 $ret[] = DI::twitterDirectMessage()->createFromMailId($id, $uid, $request['getText'] ?? '');
96 self::setLinkHeader();
98 $this->response->exit('direct-messages', ['direct_message' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));