]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Statuses/Context.php
Move jsonError out of Factory\Api\Mastodon\Error->RecordNotFound
[friendica.git] / src / Module / Api / Mastodon / Statuses / Context.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\Statuses;
23
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Item;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
30
31 /**
32  * @see https://docs.joinmastodon.org/methods/statuses/
33  */
34 class Context extends BaseApi
35 {
36         /**
37          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38          */
39         protected function rawContent(array $request = [])
40         {
41                 $uid = self::getCurrentUserID();
42
43                 if (empty($this->parameters['id'])) {
44                         DI::mstdnError()->UnprocessableEntity();
45                 }
46
47                 $request = $this->getRequest([
48                         'max_id'   => 0,     // Return results older than this id
49                         'since_id' => 0,     // Return results newer than this id
50                         'min_id'   => 0,     // Return results immediately newer than this id
51                         'limit'    => 40,    // Maximum number of results to return. Defaults to 40.
52                         'show_all' => false, // shows posts for all users including blocked and ignored users
53                 ], $request);
54
55                 $id = $this->parameters['id'];
56
57                 $parents  = [];
58                 $children = [];
59                 $deleted  = [];
60
61                 $parent = Post::selectOriginal(['uri-id', 'parent-uri-id'], ['uri-id' => $id]);
62                 if (DBA::isResult($parent)) {
63                         $id = $parent['uri-id'];
64                         $params    = ['order' => ['uri-id' => true]];
65                         $condition = ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]];
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                                 $params['order'] = ['uri-id'];
78                         }
79
80                         if (!empty($uid) && !$request['show_all']) {
81                                 $condition = DBA::mergeConditions(
82                                         $condition,
83                                         ["NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid]
84                                 );
85                         }
86
87                         $posts = Post::selectPosts(['uri-id', 'thr-parent-id', 'deleted'], $condition, $params);
88                         while ($post = Post::fetch($posts)) {
89                                 if ($post['uri-id'] == $post['thr-parent-id']) {
90                                         continue;
91                                 }
92                                 self::setBoundaries($post['uri-id']);
93
94                                 $parents[$post['uri-id']] = $post['thr-parent-id'];
95
96                                 $children[$post['thr-parent-id']][] = $post['uri-id'];
97
98                                 if ($post['deleted']) {
99                                         $deleted[] = $post['uri-id'];
100                                 }
101                         }
102                         DBA::close($posts);
103
104                         self::setLinkHeader();
105                 } else {
106                         $parent = DBA::selectFirst('mail', ['parent-uri-id'], ['uri-id' => $id, 'uid' => $uid]);
107                         if (DBA::isResult($parent)) {
108                                 $posts = DBA::select('mail', ['uri-id', 'thr-parent-id'], ['parent-uri-id' => $parent['parent-uri-id']]);
109                                 while ($post = DBA::fetch($posts)) {
110                                         if ($post['uri-id'] == $post['thr-parent-id']) {
111                                                 continue;
112                                         }
113                                         $parents[$post['uri-id']] = $post['thr-parent-id'];
114
115                                         $children[$post['thr-parent-id']][] = $post['uri-id'];
116                                 }
117                                 DBA::close($posts);
118                         } else {
119                                 $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
120                         }
121                 }
122
123                 $statuses = ['ancestors' => [], 'descendants' => []];
124
125                 $ancestors = array_diff(self::getParents($id, $parents), $deleted);
126
127                 asort($ancestors);
128
129                 $display_quotes = self::appSupportsQuotes();
130
131                 foreach (array_slice($ancestors, 0, $request['limit']) as $ancestor) {
132                         $statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid, $display_quotes);
133                 }
134
135                 $descendants = array_diff(self::getChildren($id, $children), $deleted);
136
137                 asort($descendants);
138
139                 foreach (array_slice($descendants, 0, $request['limit']) as $descendant) {
140                         $statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid, $display_quotes);
141                 }
142
143                 $this->jsonExit($statuses);
144         }
145
146         private static function getParents(int $id, array $parents, array $list = [])
147         {
148                 if (!empty($parents[$id])) {
149                         $list[] = $parents[$id];
150
151                         $list = self::getParents($parents[$id], $parents, $list);
152                 }
153                 return $list;
154         }
155
156         private static function getChildren(int $id, array $children, array $list = [])
157         {
158                 if (!empty($children[$id])) {
159                         foreach ($children[$id] as $child) {
160                                 $list[] = $child;
161
162                                 $list = self::getChildren($child, $children, $list);
163                         }
164                 }
165                 return $list;
166         }
167 }