stacker.news/worker/untrackOldItems.js
soxa b5af28c48b
Server-side tracking of comments view time (#2432)
* server-side comments view tracking, model structure, mutation

* full commentsViewedAt refactor, adjust comment creation and injection, adjust item navigation

* update server-side tracking only if there's a change, light cleanup

* coalesce meCommentsViewedAt to the item's createdAt, wip PoC comment outlining

* don't update cache on item visit, use useRoot hook for outlining

* add meCommentsViewedAt to root, better naming, light cleanup

* better timestamp logic and comparisons, add lastCommentAt to root item object, added TODOs

* fix: track commentsViewedAt only for root item, use topLevelId to fetch live comments only for the current item

* only track commentsViewedAt for root item, light cleanup

* light cleanup, correct live comments timestamp deps

* worker: on midnight, untrack items that were never viewed and had no comments in the last 21 days
2025-09-02 13:13:44 -05:00

23 lines
671 B
JavaScript

import { datePivot } from '@/lib/time'
// deletes from commentsViewAt items that were never viewed and had no comments in the last 21 days
export async function untrackOldItems ({ models }) {
const pivot = datePivot(new Date(), { days: -21 })
await models.commentsViewAt.deleteMany({
where: {
AND: [
// delete if never viewed in the last 21 days
{ lastViewedAt: { lt: pivot } },
// AND if the item had no comments in the last 21 days
{
OR: [
{ item: { lastCommentAt: { lt: pivot } } },
{ item: { lastCommentAt: null, createdAt: { lt: pivot } } }
]
}
]
}
})
}