stacker.news/lib/item.js
SatsAllDay 852d2cf304
@remindme bot support (#1159)
* @remindme bot support

support reminders via @remindme bot, just like @delete bot

* minor cleanup

* minor query cleanup

* add db migration

* various fixes and updates:

* hasNewNotes implementation
* actually return notification component in ui
* delete reminder and job on item delete
* other goodies

* refactor to use prisma for deleting existing reminder

* * switch to deleteMany to delete existing Reminders upon edit/delete of post to satisfy prisma

* update wording in form toast for remindme bot usage

* update wording in the push notification sent

* transactional reminder inserts and expirein

* set expirein on @delete too

---------

Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-05-19 15:52:02 -05:00

75 lines
2.4 KiB
JavaScript

import { COMMENT_DEPTH_LIMIT, 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'
// a delete directive preceded by a non word character that isn't a backtick
const deletePattern = /\B@delete\s+in\s+(\d+)\s+(second|minute|hour|day|week|month|year)s?/gi
const deleteMentionPattern = /\B@delete/i
const reminderPattern = /\B@remindme\s+in\s+(\d+)\s+(second|minute|hour|day|week|month|year)s?/gi
const reminderMentionPattern = /\B@remindme/i
export const hasDeleteMention = (text) => deleteMentionPattern.test(text ?? '')
export const getDeleteCommand = (text) => {
if (!text) return false
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 hasReminderMention = (text) => reminderMentionPattern.test(text ?? '')
export const getReminderCommand = (text) => {
if (!text) return false
const matches = [...text.matchAll(reminderPattern)]
const commands = matches?.map(match => ({ number: match[1], unit: match[2] }))
return commands.length ? commands[commands.length - 1] : undefined
}
export const hasReminderCommand = (text) => !!getReminderCommand(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 })
}
export const commentSubTreeRootId = (item) => {
const path = item.path.split('.')
return path.slice(-(COMMENT_DEPTH_LIMIT - 1))[0]
}