Repository navigation
feat(posts): cursor pagination for token comments API #32
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
Merged
kevincodex1
merged 3 commits into
Twigpine:main
from
Ayush7614:feat/posts-cursor-pagination
Sep 13, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { test } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { TOKEN_POSTS_DEFAULT_LIMIT, TOKEN_POSTS_MAX_LIMIT, nextPostsCursor, parseTokenPostsPaging, postsCursorKey } from "./posts-paging.ts"; | ||
|
|
||
| test("parseTokenPostsPaging defaults, clamps and reads the cursor", () => { | ||
| assert.deepEqual(parseTokenPostsPaging({}), { limit: TOKEN_POSTS_DEFAULT_LIMIT, beforeId: null }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: null }), { limit: TOKEN_POSTS_DEFAULT_LIMIT, beforeId: null }, "absent query param (URLSearchParams.get → null) uses the default, not 1"); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: "" }), { limit: TOKEN_POSTS_DEFAULT_LIMIT, beforeId: null }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: undefined }), { limit: TOKEN_POSTS_DEFAULT_LIMIT, beforeId: null }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: "20", before: "42" }), { limit: 20, beforeId: 42 }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: 0, before: 0 }), { limit: 1, beforeId: null }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: 9999 }), { limit: TOKEN_POSTS_MAX_LIMIT, beforeId: null }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: "abc", before: "xyz" }), { limit: TOKEN_POSTS_DEFAULT_LIMIT, beforeId: null }); | ||
| assert.deepEqual(parseTokenPostsPaging({ limit: 25.9 }), { limit: 25, beforeId: null }, "truncates"); | ||
| assert.deepEqual(parseTokenPostsPaging({ before: "" }), { limit: TOKEN_POSTS_DEFAULT_LIMIT, beforeId: null }); | ||
| assert.equal(TOKEN_POSTS_DEFAULT_LIMIT, 100, "default preserves the previous response (100 posts, not 50) so existing clients lose nothing"); | ||
| assert.equal(TOKEN_POSTS_MAX_LIMIT, 100); | ||
| }); | ||
|
|
||
| test("cursor key namespaces chain/token/limit/cursor", () => { | ||
| assert.equal(postsCursorKey("base", "0xABC", 50, null), "posts:base:0xabc:50:head"); | ||
| assert.equal(postsCursorKey("base", "0xabc", 20, 42), "posts:base:0xabc:20:42"); | ||
| assert.notEqual(postsCursorKey("base", "0xabc", 20, 42), postsCursorKey("base", "0xabc", 20, 43)); | ||
| }); | ||
|
|
||
| test("nextPostsCursor ends pagination on a short page", () => { | ||
| assert.equal(nextPostsCursor([9, 8, 7], 3), 7, "full page → oldest id is the next cursor"); | ||
| assert.equal(nextPostsCursor([9, 8], 3), null, "short page → done"); | ||
| assert.equal(nextPostsCursor([], 3), null); | ||
| }); | ||
|
|
||
| test("default page preserves existing discussions until the client follows cursors (PR #32)", () => { | ||
| // Maintainer repro: 75 top-level comments must not truncate, and 1 parent + | ||
| // 50 replies (51 rows) must arrive together so the UI never sees a | ||
| // replies-only page with "No comments yet." | ||
| assert.ok(TOKEN_POSTS_DEFAULT_LIMIT >= 75, "75 previously visible comments stay visible"); | ||
| assert.ok(TOKEN_POSTS_DEFAULT_LIMIT >= 51, "1 parent + 50 replies arrive on the first page"); | ||
| assert.equal(nextPostsCursor(Array.from({ length: 75 }, (_, i) => 75 - i), TOKEN_POSTS_DEFAULT_LIMIT), null); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Cursor pagination for token comments (pure; unit-tested). | ||
| * | ||
| * GET /api/posts?chain=&token= previously returned the newest 100 posts with | ||
| * no cursor (server ceiling 300, route never passed a limit). A viral token | ||
| * pays the full scan on every poll. This owns the query parsing so the route | ||
| * and postsServer share one definition: `limit` (1–100, default 100 to match | ||
| * the previous response) and `before` (exclusive id cursor, newest page first). | ||
| * Callers without params get the same first page as before, plus nextCursor; | ||
| * clients can now fetch older pages instead of re-scanning everything. | ||
| */ | ||
|
|
||
| export const TOKEN_POSTS_DEFAULT_LIMIT = 100; | ||
| export const TOKEN_POSTS_MAX_LIMIT = 100; | ||
|
|
||
| export function parseTokenPostsPaging(query: { limit?: unknown; before?: unknown }): { limit: number; beforeId: number | null } { | ||
| // NB: URLSearchParams.get() returns null when absent, and Number(null) / | ||
| // Number("") is 0 — both must fall through to the default, not clamp to 1. | ||
| const rawLimit = query.limit === null || query.limit === undefined || (typeof query.limit === "string" && query.limit.trim() === "") ? NaN : Number(query.limit); | ||
| const limit = Number.isFinite(rawLimit) | ||
| ? Math.min(TOKEN_POSTS_MAX_LIMIT, Math.max(1, Math.trunc(rawLimit))) | ||
| : TOKEN_POSTS_DEFAULT_LIMIT; | ||
| const rawBefore = query.before === null || query.before === undefined || query.before === "" ? NaN : Number(query.before); | ||
| const beforeId = Number.isInteger(rawBefore) && rawBefore > 0 ? rawBefore : null; | ||
| return { limit, beforeId }; | ||
| } | ||
|
|
||
| export function postsCursorKey(chain: string, token: string, limit: number, beforeId: number | null): string { | ||
| return `posts:${chain}:${token.toLowerCase()}:${limit}:${beforeId ?? "head"}`; | ||
| } | ||
|
|
||
| /** Cursor for the next page: oldest id on a full page, else null (no more). */ | ||
| export function nextPostsCursor(ids: number[], limit: number): number | null { | ||
| if (ids.length < limit) return null; | ||
| const oldest = ids[ids.length - 1]; | ||
| return Number.isInteger(oldest) && oldest > 0 ? oldest : null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Keep existing discussions reachable when introducing cursor pages. This call changes the default response from 100 posts to 50, but
TokenCommentsonly readsposts/muted, replaces its array on refresh, and never followsnextCursor; #31 still does the same. With 75 top-level comments, 25 previously visible comments disappear. With one parent followed by 50 replies, the page contains only replies and the UI says "No comments yet." Preserve the old default until the client supports paging, or wire cursor fetching and merging into the client here, with tests for threads crossing page boundaries and live refresh.