]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/Statuses.php
Merge pull request #13176 from MrPetovan/bug/warnings
[friendica.git] / src / Module / Api / Mastodon / Accounts / Statuses.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\Accounts;
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\Conversation;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Model\Verb;
33 use Friendica\Module\BaseApi;
34 use Friendica\Object\Api\Mastodon\TimelineOrderByTypes;
35 use Friendica\Protocol\Activity;
36
37 /**
38  * @see https://docs.joinmastodon.org/methods/accounts/
39  */
40 class Statuses extends BaseApi
41 {
42         /**
43          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
44          */
45         protected function rawContent(array $request = [])
46         {
47                 $uid = self::getCurrentUserID();
48
49                 if (empty($this->parameters['id'])) {
50                         DI::mstdnError()->UnprocessableEntity();
51                 }
52
53                 $id = $this->parameters['id'];
54                 if (!DBA::exists('contact', ['id' => $id, 'uid' => 0])) {
55                         DI::mstdnError()->RecordNotFound();
56                 }
57
58                 $request = $this->getRequest([
59                         'only_media'      => false, // Show only statuses with media attached? Defaults to false.
60                         'max_id'          => null,     // Return results older than this id
61                         'since_id'        => null,     // Return results newer than this id
62                         'min_id'          => null,     // Return results immediately newer than this id
63                         'limit'           => 20,    // Maximum number of results to return. Defaults to 20.
64                         'pinned'          => false, // Only pinned posts
65                         'exclude_replies' => false, // Don't show comments
66                         'with_muted'      => false, // Pleroma extension: return activities by muted (not by blocked!) users.
67                         'exclude_reblogs' => false, // Undocumented parameter
68                         'tagged'          => false, // Undocumented parameter
69                         'friendica_order' => TimelineOrderByTypes::ID, // order options (defaults to ID)
70                 ], $request);
71
72                 if ($request['pinned']) {
73                         $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'type' => Post\Collection::FEATURED];
74                 } elseif ($request['only_media']) {
75                         $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED], 'type' => [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]];
76                 } elseif (!$uid) {
77                         $condition = ['author-id' => $id, 'private' => [Item::PUBLIC, Item::UNLISTED],
78                                 'uid' => 0, 'network' => Protocol::FEDERATED];
79                 } else {
80                         $condition = ["`author-id` = ? AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))", $id, $uid];
81                 }
82
83                 $condition = $this->addPagingConditions($request, $condition);
84                 $params = $this->buildOrderAndLimitParams($request);
85
86                 if (!$request['pinned'] && !$request['only_media']) {
87                         if ($request['exclude_replies']) {
88                                 $condition = DBA::mergeConditions($condition, ["(`gravity` = ? OR (`gravity` = ? AND `vid` = ? AND `protocol` != ?))",
89                                         Item::GRAVITY_PARENT, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA]);
90                         } else {
91                                 $condition = DBA::mergeConditions($condition, ["(`gravity` IN (?, ?) OR (`gravity` = ? AND `vid` = ? AND `protocol` != ?))",
92                                         Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT, Item::GRAVITY_ACTIVITY, Verb::getID(Activity::ANNOUNCE), Conversation::PARCEL_DIASPORA]);
93                         }
94                 } elseif ($request['exclude_replies']) {
95                         $condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_PARENT]);
96                 }
97
98                 if ($request['pinned']) {
99                         $items = DBA::select('collection-view', ['uri-id'], $condition, $params);
100                 } elseif ($request['only_media']) {
101                         $items = DBA::select('media-view', ['uri-id'], $condition, $params);
102                 } else {
103                         $items = Post::selectForUser($uid, ['uri-id'], $condition, $params);
104                 }
105
106                 $display_quotes = self::appSupportsQuotes();
107
108                 $statuses = [];
109                 while ($item = Post::fetch($items)) {
110                         try {
111                                 $status =  DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
112                                 $this->updateBoundaries($status, $item, $request['friendica_order']);
113                                 $statuses[] = $status;
114                         } catch (\Throwable $th) {
115                                 Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
116                         }
117                 }
118                 DBA::close($items);
119
120                 if (!empty($request['min_id'])) {
121                         $statuses = array_reverse($statuses);
122                 }
123
124                 self::setLinkHeader($request['friendica_order'] != TimelineOrderByTypes::ID);
125                 System::jsonExit($statuses);
126         }
127 }