X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FModule%2FApi%2FMastodon%2FStatuses%2FContext.php;h=6e35d717bf741d09bec7f51937beea1b1ff8edaa;hb=1b9ec3a2147fc10e4037ff1b2a7aac506cdf3b13;hp=d64b4d24cf66fef2d9c87cea9c2d41b76688101b;hpb=3bbf543d2523abf1c9e4eda0501be73d7975a71d;p=friendica.git diff --git a/src/Module/Api/Mastodon/Statuses/Context.php b/src/Module/Api/Mastodon/Statuses/Context.php index d64b4d24cf..6e35d717bf 100644 --- a/src/Module/Api/Mastodon/Statuses/Context.php +++ b/src/Module/Api/Mastodon/Statuses/Context.php @@ -1,6 +1,6 @@ RecordNotFound(); + if (empty($this->parameters['id'])) { + $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity()); } - $id = $parameters['id']; - $parent = Post::selectFirst(['parent-uri-id'], ['uri-id' => $id]); - if (!DBA::isResult($parent)) { - DI::mstdnError()->RecordNotFound(); - } + $request = $this->getRequest([ + 'max_id' => 0, // Return results older than this id + 'since_id' => 0, // Return results newer than this id + 'min_id' => 0, // Return results immediately newer than this id + 'limit' => 40, // Maximum number of results to return. Defaults to 40. + 'show_all' => false, // shows posts for all users including blocked and ignored users + ], $request); + + $id = $this->parameters['id']; - $parents = []; + $parents = []; $children = []; - $posts = Post::select(['uri-id', 'thr-parent-id'], ['parent-uri-id' => $parent['parent-uri-id']], [], false); - while ($post = Post::fetch($posts)) { - if ($post['uri-id'] == $post['thr-parent-id']) { - continue; + $deleted = []; + + $parent = Post::selectOriginal(['uri-id', 'parent-uri-id'], ['uri-id' => $id]); + if (DBA::isResult($parent)) { + $id = $parent['uri-id']; + $params = ['order' => ['uri-id' => true]]; + $condition = ['parent-uri-id' => $parent['parent-uri-id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]]; + + if (!empty($request['max_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $request['max_id']]); + } + + if (!empty($request['since_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['since_id']]); + } + + if (!empty($request['min_id'])) { + $condition = DBA::mergeConditions($condition, ["`uri-id` > ?", $request['min_id']]); + $params['order'] = ['uri-id']; + } + + if (!empty($uid) && !$request['show_all']) { + $condition = DBA::mergeConditions( + $condition, + ["NOT `author-id` IN (SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND (`blocked` OR `ignored`))", $uid] + ); + } + + $posts = Post::selectPosts(['uri-id', 'thr-parent-id', 'deleted'], $condition, $params); + while ($post = Post::fetch($posts)) { + if ($post['uri-id'] == $post['thr-parent-id']) { + continue; + } + self::setBoundaries($post['uri-id']); + + $parents[$post['uri-id']] = $post['thr-parent-id']; + + $children[$post['thr-parent-id']][] = $post['uri-id']; + + if ($post['deleted']) { + $deleted[] = $post['uri-id']; + } + } + DBA::close($posts); + + self::setLinkHeader(); + } else { + $parent = DBA::selectFirst('mail', ['parent-uri-id'], ['uri-id' => $id, 'uid' => $uid]); + if (DBA::isResult($parent)) { + $posts = DBA::select('mail', ['uri-id', 'thr-parent-id'], ['parent-uri-id' => $parent['parent-uri-id']]); + while ($post = DBA::fetch($posts)) { + if ($post['uri-id'] == $post['thr-parent-id']) { + continue; + } + $parents[$post['uri-id']] = $post['thr-parent-id']; + + $children[$post['thr-parent-id']][] = $post['uri-id']; + } + DBA::close($posts); + } else { + $this->logAndJsonError(404, $this->errorFactory->RecordNotFound()); } - $parents[$post['uri-id']] = $post['thr-parent-id']; - $children[$post['thr-parent-id']][] = $post['uri-id']; } - DBA::close($posts); $statuses = ['ancestors' => [], 'descendants' => []]; - foreach (self::getParents($id, $parents) as $ancestor) { - $statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid); + $ancestors = array_diff(self::getParents($id, $parents), $deleted); + + asort($ancestors); + + $display_quotes = self::appSupportsQuotes(); + + foreach (array_slice($ancestors, 0, $request['limit']) as $ancestor) { + $statuses['ancestors'][] = DI::mstdnStatus()->createFromUriId($ancestor, $uid, $display_quotes); } - foreach (self::getChildren($id, $children) as $descendant) { - $statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid); + $descendants = array_diff(self::getChildren($id, $children), $deleted); + + asort($descendants); + + foreach (array_slice($descendants, 0, $request['limit']) as $descendant) { + $statuses['descendants'][] = DI::mstdnStatus()->createFromUriId($descendant, $uid, $display_quotes); } - System::jsonExit($statuses); + $this->jsonExit($statuses); } private static function getParents(int $id, array $parents, array $list = []) { if (!empty($parents[$id])) { $list[] = $parents[$id]; + $list = self::getParents($parents[$id], $parents, $list); } return $list; @@ -89,6 +158,7 @@ class Context extends BaseApi if (!empty($children[$id])) { foreach ($children[$id] as $child) { $list[] = $child; + $list = self::getChildren($child, $children, $list); } }