]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Search/Tweets.php
spelling: days
[friendica.git] / src / Module / Api / Twitter / Search / Tweets.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\Search;
23
24 use Friendica\Database\DBA;
25 use Friendica\Module\BaseApi;
26 use Friendica\DI;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Item;
29 use Friendica\Model\Post;
30 use Friendica\Network\HTTPException\BadRequestException;
31
32 /**
33  * Returns statuses that match a specified query.
34  *
35  * @see https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
36  */
37 class Tweets extends BaseApi
38 {
39         protected function rawContent(array $request = [])
40         {
41                 BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
42                 $uid = BaseApi::getCurrentUserID();
43
44                 if (empty($request['q'])) {
45                         throw new BadRequestException('q parameter is required.');
46                 }
47
48                 $searchTerm = trim(rawurldecode($request['q']));
49
50                 $data['status'] = [];
51
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);
59
60                 $start = max(0, ($page - 1) * $count);
61
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];
66
67                         $tags   = DBA::select('tag-search-view', ['uri-id'], $condition);
68                         $uriids = [];
69                         while ($tag = DBA::fetch($tags)) {
70                                 $uriids[] = $tag['uri-id'];
71                         }
72                         DBA::close($tags);
73
74                         if (empty($uriids)) {
75                                 $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
76                                 return;
77                         }
78
79                         $condition = ['uri-id' => $uriids];
80                         if ($exclude_replies) {
81                                 $condition['gravity'] = Item::GRAVITY_PARENT;
82                         }
83
84                         $params['group_by'] = ['uri-id'];
85                 } else {
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']];
91                         if ($max_id > 0) {
92                                 $condition[0] .= ' AND `uri-id` <= ?';
93                                 $condition[] = $max_id;
94                         }
95                 }
96
97                 $statuses = [];
98
99                 if (parse_url($searchTerm, PHP_URL_SCHEME) != '') {
100                         $id = Item::fetchByLink($searchTerm, $uid);
101                         if (!$id) {
102                                 // Public post
103                                 $id = Item::fetchByLink($searchTerm);
104                         }
105
106                         if (!empty($id)) {
107                                 $statuses = Post::select([], ['id' => $id]);
108                         }
109                 }
110
111                 $statuses = $statuses ?: Post::selectForUser($uid, [], $condition, $params);
112
113                 $ret = [];
114                 while ($status = DBA::fetch($statuses)) {
115                         $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray();
116                 }
117                 DBA::close($statuses);
118
119                 $this->response->exit('statuses', ['status' => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
120         }
121 }