UserPostQueries.php: fix paging boundaries

This commit is contained in:
Zankaria 2025-02-14 12:48:58 +01:00 committed by Zankaria
parent b1989a69b0
commit bf060ce174

View file

@ -44,19 +44,20 @@ class UserPostQueries {
$posts_count = \count($posts);
// By fetching one extra post bellow and/or above the limit, we know if there are any posts beside the current page.
if ($posts_count === $page_size + 2) {
$has_extra_prev_post = true;
$has_extra_end_post = true;
} elseif ($posts_count === $page_size + 1) {
$has_extra_prev_post = $start_id !== null && $start_id === (int)$posts[0]['id'];
$has_extra_end_post = !$has_extra_prev_post;
} else {
$has_extra_prev_post = false;
$has_extra_end_post = false;
/*
* If the id we start fetching from is also the first id fetched from the DB, then we exclude it from
* the results, noting that we fetched 1 more posts than we needed, and it was before the current page.
* Hence, we have no extra post at the end and no next page.
*/
$has_extra_prev_post = $start_id !== null && $start_id === (int)$posts[0]['id'];
$has_extra_end_post = !$has_extra_prev_post && $posts_count > $page_size;
}
// Since we fetched one post bellow and/or above the limit, we always know if there are any posts after the current page.
// Get the previous cursor, if any.
if ($has_extra_prev_post) {
\array_shift($posts);