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