stacker.news/lib/item.js
SatsAllDay 1d394bebe1
Ephemeral item support (#570)
backend impl and some of the UI for ephemeral item support

more to come, this is just a WIP so far

Consolidate client-side ephemeral fee logic in FeeButton components for easier reuse

* update the update_item function to handle the case where an item was not
ephemeral, but now is, so we charge the user accordingly

* introduce `hasDeleteCommand` for a better logical abstraction for some use cases in the code

* introduce `EPHEMERAL_FEE_SATS` which is derived from `EPHEMERAL_FEE_MSATS`, so we don't
have to the same calculation over and over

Remove fees for ephemeral items

* remove unused markdownField prop in FeeButton

* remove empty migration

minor code cleanup

Centralize delete item by author code to reduce duplication
2023-10-22 11:02:58 -05:00

49 lines
1.5 KiB
JavaScript

import { OLD_ITEM_DAYS } from './constants'
import { datePivot } from './time'
export const defaultCommentSort = (pinned, bio, createdAt) => {
// pins sort by recent
if (pinned) return 'recent'
// old items (that aren't bios) sort by top
if (!bio && new Date(createdAt) < datePivot(new Date(), { days: -OLD_ITEM_DAYS })) return 'top'
// everything else sorts by hot
return 'hot'
}
export const isJob = item => typeof item.maxBid !== 'undefined'
const deletePattern = /\B@delete\s+in\s+(\d+)\s+(second|minute|hour|day|week|month|year)s?/gi
export const getDeleteCommand = (text = '') => {
const matches = [...text.matchAll(deletePattern)]
const commands = matches?.map(match => ({ number: match[1], unit: match[2] }))
return commands.length ? commands[commands.length - 1] : undefined
}
export const hasDeleteCommand = (text) => !!getDeleteCommand(text)
export const deleteItemByAuthor = async ({ models, id, item }) => {
if (!item) {
item = await models.item.findUnique({ where: { id: Number(id) } })
}
if (!item) {
console.log('attempted to delete an item that does not exist', id)
return
}
const updateData = { deletedAt: new Date() }
if (item.text) {
updateData.text = '*deleted by author*'
}
if (item.title) {
updateData.title = 'deleted by author'
}
if (item.url) {
updateData.url = null
}
if (item.pollCost) {
updateData.pollCost = null
}
return await models.item.update({ where: { id: Number(id) }, data: updateData })
}