stacker.news/lib/cursor.js
Keyan 01b021a337
comment pagination with limit/offset (#1824)
* basic query with limit/offset

* roughly working increment

* working limiting/pageable queries

* denormalize direct comments + full comments below threshold

* notifications in megathread + working nest view more buttons

* fix empty comment footer

* make comments nested resolver again

* use time in cursor to avoid duplicates

* squash migrations

* do not need item.comments undefined checks
2025-01-29 19:00:05 -06:00

25 lines
779 B
JavaScript

export const LIMIT = 21
export function decodeCursor (cursor) {
if (!cursor) {
return { offset: 0, time: new Date() }
} else {
const res = JSON.parse(Buffer.from(cursor, 'base64'))
res.offset = Number(res.offset)
res.time = new Date(res.time)
return res
}
}
export function nextCursorEncoded (cursor, limit = LIMIT) {
cursor.offset += limit
return Buffer.from(JSON.stringify(cursor)).toString('base64')
}
export function nextNoteCursorEncoded (cursor, notifications = [], limit = LIMIT) {
// what we are looking for this oldest sort time for every table we are looking at
cursor.time = new Date(notifications.slice(-1).pop()?.sortTime ?? cursor.time)
cursor.offset += limit
return Buffer.from(JSON.stringify(cursor)).toString('base64')
}