]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Statuses.php
Replace `$parameters` argument per method with `static::$parameters`
[friendica.git] / src / Module / Api / Mastodon / Accounts / Statuses.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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         public static function rawContent()
43         {
44                 $uid = self::getCurrentUserID();
45
46                 if (empty(static::$parameters['id'])) {
47                         DI::mstdnError()->UnprocessableEntity();
48                 }
49
50                 $id = static::$parameters['id'];
51                 if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
52                         DI::mstdnError()->RecordNotFound();
53                 }
54
55                 $request = self::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                 ]);
67
68                 $params = ['order' => ['uri-id' => true], 'limit' => $request['limit']];
69
70                 if (!$uid) {
71                         $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED],
72                                 'uid' => 0, 'network' => Protocol::FEDERATED];
73                 } else {
74                         $condition = ["`author-id` = ? AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))", $id, $uid];
75                 }
76
77                 $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ?))",
78                         GRAVITY_PARENT, GRAVITY_COMMENT, GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE)]);
79
80                 if ($request['only_media']) {
81                         $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `type` IN (?, ?, ?))",
82                                 Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]);
83                 }
84         
85                 if (!empty($request['max_id'])) {
86                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
87                 }
88
89                 if (!empty($request['since_id'])) {
90                         $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
91                 }
92
93                 if (!empty($request['min_id'])) {
94                         $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
95                         $params['order'] = ['uri-id'];
96                 }
97
98                 if ($request['pinned']) {
99                         $condition = DBA::mergeConditions($condition, ['pinned' => true]);
100                 }
101
102                 if ($request['exclude_replies']) {
103                         $condition = DBA::mergeConditions($condition, ['gravity' => GRAVITY_PARENT]);
104                 }
105
106                 $items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
107
108                 $statuses = [];
109                 while ($item = Post::fetch($items)) {
110                         self::setBoundaries($item['uri-id']);
111                         $statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid);
112                 }
113                 DBA::close($items);
114
115                 if (!empty($request['min_id'])) {
116                         array_reverse($statuses);
117                 }
118
119                 self::setLinkHeader();
120                 System::jsonExit($statuses);
121         }
122 }