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\Search;
24 use Friendica\Database\DBA;
25 use Friendica\Module\BaseApi;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Item;
29 use Friendica\Model\Post;
30 use Friendica\Network\HTTPException\BadRequestException;
33 * Returns statuses that match a specified query.
35 * @see https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
37 class Tweets extends BaseApi
39 protected function rawContent(array $request = [])
41 BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
42 $uid = BaseApi::getCurrentUserID();
44 if (empty($request['q'])) {
45 throw new BadRequestException('q parameter is required.');
48 $searchTerm = trim(rawurldecode($request['q']));
52 $count = $this->getRequestValue($request, 'count', 20, 1, 100);
53 $count = $this->getRequestValue($request, 'rpp', $count);
54 $since_id = $this->getRequestValue($request, 'since_id', 0, 0);
55 $max_id = $this->getRequestValue($request, 'max_id', 0, 0);
56 $page = $this->getRequestValue($request, 'page', 1, 1);
57 $include_entities = $this->getRequestValue($request, 'include_entities', false);
58 $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false);
60 $start = max(0, ($page - 1) * $count);
62 $params = ['order' => ['uri-id' => true], 'limit' => [$start, $count]];
63 if (preg_match('/^#(\w+)$/', $searchTerm, $matches) === 1 && isset($matches[1])) {
64 $searchTerm = $matches[1];
65 $condition = ["`uri-id` > ? AND `name` = ? AND (NOT `private` OR (`private` AND `uid` = ?))", $since_id, $searchTerm, $uid];
67 $tags = DBA::select('tag-search-view', ['uri-id'], $condition);
69 while ($tag = DBA::fetch($tags)) {
70 $uriids[] = $tag['uri-id'];
75 $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
79 $condition = ['uri-id' => $uriids];
80 if ($exclude_replies) {
81 $condition['gravity'] = Item::GRAVITY_PARENT;
84 $params['group_by'] = ['uri-id'];
86 $condition = ["`uri-id` > ?
87 " . ($exclude_replies ? " AND `gravity` = " . Item::GRAVITY_PARENT : ' ') . "
88 AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
89 AND `body` LIKE CONCAT('%',?,'%')",
90 $since_id, $uid, $_REQUEST['q']];
92 $condition[0] .= ' AND `uri-id` <= ?';
93 $condition[] = $max_id;
99 if (parse_url($searchTerm, PHP_URL_SCHEME) != '') {
100 $id = Item::fetchByLink($searchTerm, $uid);
103 $id = Item::fetchByLink($searchTerm);
107 $statuses = Post::select([], ['id' => $id]);
111 $statuses = $statuses ?: Post::selectForUser($uid, [], $condition, $params);
114 while ($status = DBA::fetch($statuses)) {
115 $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
117 DBA::close($statuses);
119 $this->response->exit('statuses', ['status' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));