3 * @copyright Copyright (C) 2010-2022, 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\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;
35 abstract class DirectMessagesEndpoint extends BaseApi
40 /** @var DirectMessage */
41 private $directMessage;
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 = [])
45 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48 $this->directMessage = $directMessage;
52 * Handles a direct messages endpoint with the given condition
54 * @param array $request
56 * @param array $condition
60 protected function getMessages(array $request, int $uid, array $condition)
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);
71 $start = max(0, ($page - 1) * $count);
73 $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
75 if (!empty($max_id)) {
76 $condition = DBA::mergeConditions($condition, ["`id` < ?", $max_id]);
79 if (!empty($since_id)) {
80 $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]);
83 if (!empty($min_id)) {
84 $condition = DBA::mergeConditions($condition, ["`id` > ?", $min_id]);
86 $params['order'] = ['id'];
89 $cid = BaseApi::getContactIDForSearchterm($this->getRequestValue($request, 'screen_name', ''), $this->getRequestValue($request, 'profileurl', ''), $this->getRequestValue($request, 'user_id', 0), 0);
91 $cdata = Contact::getPublicAndUserContactID($cid, $uid);
92 if (!empty($cdata['user'])) {
93 $condition = DBA::mergeConditions($condition, ["`contact-id` = ?", $cdata['user']]);
97 $condition = DBA::mergeConditions($condition, ["`uid` = ?", $uid]);
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);
106 $ids = array_column($mails, 'id');
108 if (!empty($min_id)) {
109 $ids = array_reverse($ids);
113 foreach ($ids as $id) {
114 $ret[] = $this->directMessage->createFromMailId($id, $uid, $this->getRequestValue($request, 'getText', ''));
117 self::setLinkHeader();
119 $this->response->exit('direct-messages', ['direct_message' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));