3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon\Statuses;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Item;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
32 * @see https://docs.joinmastodon.org/methods/statuses/
34 class Context extends BaseApi
37 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
39 protected function rawContent(array $request = [])
41 $uid = self::getCurrentUserID();
43 if (empty($this->parameters['id'])) {
44 DI::mstdnError()->UnprocessableEntity();
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
55 $id = $this->parameters['id'];
60 $parent = Post::selectOriginal(['uri-id', 'parent-uri-id'], ['uri-id' => $id]);
61 if (DBA::isResult($parent)) {
62 $id = $parent['uri-id'];
63 $params = ['order' => ['uri-id' => true]];
64 $condition = ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]];
66 if (!empty($request['max_id'])) {
67 $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]);
70 if (!empty($request['since_id'])) {
71 $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]);
74 if (!empty($request['min_id'])) {
75 $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]);
76 $params['order'] = ['uri-id'];
79 if (!empty($uid) && !$request['show_all']) {
80 $condition = DBA::mergeConditions(
82 ["NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid]
86 $posts = Post::selectPosts(['uri-id', 'thr-parent-id'], $condition, $params);
87 while ($post = Post::fetch($posts)) {
88 if ($post['uri-id'] == $post['thr-parent-id']) {
91 self::setBoundaries($post['uri-id']);
93 $parents[$post['uri-id']] = $post['thr-parent-id'];
95 $children[$post['thr-parent-id']][] = $post['uri-id'];
99 self::setLinkHeader();
101 $parent = DBA::selectFirst('mail', ['parent-uri-id'], ['uri-id' => $id, 'uid' => $uid]);
102 if (DBA::isResult($parent)) {
103 $posts = DBA::select('mail', ['uri-id', 'thr-parent-id'], ['parent-uri-id' => $parent['parent-uri-id']]);
104 while ($post = DBA::fetch($posts)) {
105 if ($post['uri-id'] == $post['thr-parent-id']) {
108 $parents[$post['uri-id']] = $post['thr-parent-id'];
110 $children[$post['thr-parent-id']][] = $post['uri-id'];
114 DI::mstdnError()->RecordNotFound();
118 $statuses = ['ancestors' => [], 'descendants' => []];
120 $ancestors = self::getParents($id, $parents);
124 $display_quotes = self::appSupportsQuotes();
126 foreach (array_slice($ancestors, 0, $request['limit']) as $ancestor) {
127 $statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid, $display_quotes);
130 $descendants = self::getChildren($id, $children);
134 foreach (array_slice($descendants, 0, $request['limit']) as $descendant) {
135 $statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid, $display_quotes);
138 System::jsonExit($statuses);
141 private static function getParents(int $id, array $parents, array $list = [])
143 if (!empty($parents[$id])) {
144 $list[] = $parents[$id];
146 $list = self::getParents($parents[$id], $parents, $list);
151 private static function getChildren(int $id, array $children, array $list = [])
153 if (!empty($children[$id])) {
154 foreach ($children[$id] as $child) {
157 $list = self::getChildren($child, $children, $list);