Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support after parameter for comment pagination #10361

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/Http/Controllers/CommentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ public function destroy($id)
*
* `pinned_comments` is only included when `commentable_type` and `commentable_id` are specified.
*
* @queryParam commentable_type The type of resource to get comments for.
* @queryParam commentable_id The id of the resource to get comments for.
* @queryParam cursor Pagination option. See [CommentSort](#commentsort) for detail. The format follows [Cursor](#cursor) except it's not currently included in the response.
* @queryParam parent_id Limit to comments which are reply to the specified id. Specify 0 to get top level comments.
* @queryParam sort Sort option as defined in [CommentSort](#commentsort). Defaults to `new` for guests and user-specified default when authenticated.
* @queryParam after Return comments which come after the specified comment id as per sort option. No-example
* @queryParam commentable_type The type of resource to get comments for. Example: beatmapset
* @queryParam commentable_id The id of the resource to get comments for. Example: 1
* @queryParam cursor Pagination option. See [CommentSort](#commentsort) for detail. The format follows [Cursor](#cursor) except it's not currently included in the response. No-example
* @queryParam parent_id Limit to comments which are reply to the specified id. Specify 0 to get top level comments. Example: 1
* @queryParam sort Sort option as defined in [CommentSort](#commentsort). Defaults to `new` for guests and user-specified default when authenticated. Example: new
*/
public function index()
{
Expand Down
14 changes: 11 additions & 3 deletions app/Libraries/CommentBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public function countForPaginator()

private function getComments($query, $isChildren = true, $pinnedOnly = false)
{
$sortOrCursorHelper = $pinnedOnly ? 'new' : $this->params->cursorHelper;
$cursorHelper = $pinnedOnly
? Comment::makeDbCursorHelper('new')
: $this->params->cursorHelper;
$queryLimit = $this->params->limit;

if (!$isChildren) {
Expand All @@ -180,14 +182,20 @@ private function getComments($query, $isChildren = true, $pinnedOnly = false)
}

$queryLimit++;
$cursor = $this->params->cursor;

if ($this->params->after === null) {
$cursor = $this->params->cursor;
} else {
$lastComment = Comment::findOrFail($this->params->after);
$cursor = $cursorHelper->next([$lastComment]);
}

if ($cursor === null) {
$query->offset(max_offset($this->params->page, $this->params->limit));
}
}

$query->cursorSort($sortOrCursorHelper, $cursor ?? null);
$query->cursorSort($cursorHelper, $cursor ?? null);

if (!$this->includeDeleted) {
$query->whereNull('deleted_at');
Expand Down
2 changes: 2 additions & 0 deletions app/Libraries/CommentBundleParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CommentBundleParams
const DEFAULT_PAGE = 1;
const DEFAULT_LIMIT = 50;

public ?int $after = null;
public $userId;
public $commentableId;
public $commentableType;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function setAll($params)
$this->cursorHelper = Comment::makeDbCursorHelper($params['sort'] ?? $this->sort);
$this->cursor = get_arr($params['cursor'] ?? null);
$this->sort = $this->cursorHelper->getSortName();
$this->after = get_int($params['after'] ?? null);
}

public function filterByParentId()
Expand Down
7 changes: 1 addition & 6 deletions resources/js/components/comment-show-more.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ export default class CommentShowMore extends React.Component<Props> {

const lastComment = last(this.props.comments);
if (lastComment != null) {
// TODO: convert to plain after_id params of some sort instead of cursor
params.cursor = {
created_at: lastComment.createdAt,
id: lastComment.id,
votes_count: lastComment.votesCount,
};
params.after = lastComment.id;
}

this.xhr = $.ajax(route('comments.index'), { data: params, dataType: 'json' });
Expand Down