]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Statuses.php
API: Improve the performance for fetching pinned posts
[friendica.git] / src / Module / Api / Mastodon / Accounts / Statuses.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Accounts;
23
24 use Friendica\Core\Protocol;
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\Model\Verb;
31 use Friendica\Module\BaseApi;
32 use Friendica\Protocol\Activity;
33
34 /**
35  * @see https://docs.joinmastodon.org/methods/accounts/
36  */
37 class Statuses extends BaseApi
38 {
39         /**
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          */
42         protected function rawContent(array $request = [])
43         {
44                 $uid = self::getCurrentUserID();
45
46                 if (empty($this->parameters['id'])) {
47                         DI::mstdnError()->UnprocessableEntity();
48                 }
49
50                 $id = $this->parameters['id'];
51                 if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
52                         DI::mstdnError()->RecordNotFound();
53                 }
54
55                 $request = $this->getRequest([
56                         'only_media'      => false, // Show only statuses with media attached? Defaults to false.
57                         'max_id'          => 0,     // Return results older than this id
58                         'since_id'        => 0,     // Return results newer than this id
59                         'min_id'          => 0,     // Return results immediately newer than this id
60                         'limit'           => 20,    // Maximum number of results to return. Defaults to 20.
61                         'pinned'          => false, // Only pinned posts
62                         'exclude_replies' => false, // Don't show comments
63                         'with_muted'      => false, // Pleroma extension: return activities by muted (not by blocked!) users.
64                         'exclude_reblogs' => false, // Undocumented parameter
65                         'tagged'          => false, // Undocumented parameter
66                 ], $request);
67
68                 $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
69
70                 if ($request['pinned']) {
71                         $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED]];
72                 } elseif (!$uid) {
73                         $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED],
74                                 'uid' => 0, 'network' => Protocol::FEDERATED];
75                 } else {
76                         $condition = ["`author-id` = ? AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))", $id, $uid];
77                 }
78
79                 if (!$request['pinned']) {
80                         $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))",
81                                 GRAVITY_PARENT, GRAVITY_COMMENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE)]);
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 (!empty($request['max_id'])) {
90                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
91                 }
92
93                 if (!empty($request['since_id'])) {
94                         $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
95                 }
96
97                 if (!empty($request['min_id'])) {
98                         $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
99                         $params['order'] = ['uri-id'];
100                 }
101
102                 if ($request['exclude_replies']) {
103                         $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
104                 }
105
106                 if ($request['pinned']) {
107                         $items = DBA::select('collection-view', ['uri-id'], $condition, $params);
108                 } else {
109                         $items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
110                 }
111
112                 $statuses = [];
113                 while ($item = Post::fetch($items)) {
114                         self::setBoundaries($item['uri-id']);
115                         $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
116                 }
117                 DBA::close($items);
118
119                 if (!empty($request['min_id'])) {
120                         $statuses = array_reverse($statuses);
121                 }
122
123                 self::setLinkHeader();
124                 System::jsonExit($statuses);
125         }
126 }