]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Timelines/Tag.php
Catch not fetchable posts
[friendica.git] / src / Module / Api / Mastodon / Timelines / Tag.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\Mastodon\Timelines;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Friendica\Module\BaseApi;
32 use Friendica\Network\HTTPException;
33
34 /**
35  * @see https://docs.joinmastodon.org/methods/timelines/
36  */
37 class Tag extends BaseApi
38 {
39         /**
40          * @throws HTTPException\InternalServerErrorException
41          */
42         protected function rawContent(array $request = [])
43         {
44                 self::checkAllowedScope(self::SCOPE_READ);
45                 $uid = self::getCurrentUserID();
46
47                 if (empty($this->parameters['hashtag'])) {
48                         DI::mstdnError()->UnprocessableEntity();
49                 }
50
51                 /**
52                  * @todo Respect missing parameters
53                  * @see https://github.com/tootsuite/mastodon/blob/main/app/controllers/api/v1/timelines/tag_controller.rb
54                  *
55                  * There seem to be the parameters "any", "all", and "none".
56                  */
57
58                 $request = $this->getRequest([
59                         'local'           => false, // If true, return only local statuses. Defaults to false.
60                         'remote'          => false, // Show only remote statuses? Defaults to false.
61                         'only_media'      => false, // If true, return only statuses with media attachments. Defaults to false.
62                         'max_id'          => 0,     // Return results older than this ID.
63                         'since_id'        => 0,     // Return results newer than this ID.
64                         'min_id'          => 0,     // Return results immediately newer than this ID.
65                         'limit'           => 20,    // Maximum number of results to return. Defaults to 20.
66                         'with_muted'      => false, // Pleroma extension: return activities by muted (not by blocked!) users.
67                         'exclude_replies' => false, // Don't show comments
68                 ], $request);
69
70                 $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
71
72                 $condition = ["`name` = ? AND (`uid` = ? OR (`uid` = ? AND NOT `global`))
73                         AND (`network` IN (?, ?, ?, ?) OR (`uid` = ? AND `uid` != ?))",
74                         $this->parameters['hashtag'], 0, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, $uid, 0];
75
76                 if ($request['local']) {
77                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin`)"]);
78                 }
79
80                 if ($request['remote']) {
81                         $condition = DBA::mergeConditions($condition, ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `post-user`.`uri-id` = `tag-search-view`.`uri-id`)"]);
82                 }
83
84                 if ($request['only_media']) {
85                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
86                                 Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
87                 }
88
89                 if ($request['exclude_replies']) {
90                         $condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_PARENT]);
91                 }
92
93                 if (!empty($request['max_id'])) {
94                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
95                 }
96
97                 if (!empty($request['since_id'])) {
98                         $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
99                 }
100
101                 if (!empty($request['min_id'])) {
102                         $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
103
104                         $params['order'] = ['uri-id'];
105                 }
106
107                 $items = DBA::select('tag-search-view', ['uri-id'], $condition, $params);
108
109                 $display_quotes = self::appSupportsQuotes();
110
111                 $statuses = [];
112                 while ($item = Post::fetch($items)) {
113                         self::setBoundaries($item['uri-id']);
114                         try {
115                                 $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
116                         } catch (\Throwable $th) {
117                                 Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
118                         }
119                 }
120                 DBA::close($items);
121
122                 if (!empty($request['min_id'])) {
123                         $statuses = array_reverse($statuses);
124                 }
125
126                 self::setLinkHeader();
127                 System::jsonExit($statuses);
128         }
129 }