Don't export sendUserNotification (#937)
* Rename file to webPush.js * Move webPush code into lib/webPush * Don't export sendUserNotification * Fix null in deposit push notification * restore deposit notification change --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
This commit is contained in:
parent
687d71f246
commit
22ff832efb
|
@ -15,7 +15,7 @@ import { msatsToSats } from '../../lib/format'
|
|||
import { parse } from 'tldts'
|
||||
import uu from 'url-unshort'
|
||||
import { actSchema, advSchema, bountySchema, commentSchema, discussionSchema, jobSchema, linkSchema, pollSchema, ssValidate } from '../../lib/validate'
|
||||
import { sendUserNotification, notifyItemParents, notifyUserSubscribers, notifyZapped, notifyTerritorySubscribers } from '../../lib/webPush'
|
||||
import { notifyItemParents, notifyUserSubscribers, notifyZapped, notifyTerritorySubscribers, notifyMention } from '../../lib/webPush'
|
||||
import { defaultCommentSort, isJob, deleteItemByAuthor, getDeleteCommand, hasDeleteCommand } from '../../lib/item'
|
||||
import { datePivot, whenRange } from '../../lib/time'
|
||||
import { imageFeesInfo, uploadIdsFromText } from './image'
|
||||
|
@ -1225,12 +1225,7 @@ export const createMentions = async (item, models) => {
|
|||
|
||||
// only send if mention is new to avoid duplicates
|
||||
if (mention.createdAt.getTime() === mention.updatedAt.getTime()) {
|
||||
sendUserNotification(user.id, {
|
||||
title: 'you were mentioned',
|
||||
body: item.text,
|
||||
item,
|
||||
tag: 'MENTION'
|
||||
}).catch(console.error)
|
||||
notifyMention(user.id, item)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import webPush from 'web-push'
|
||||
import removeMd from 'remove-markdown'
|
||||
import { ANON_USER_ID, COMMENT_DEPTH_LIMIT } from './constants'
|
||||
import { ANON_USER_ID, COMMENT_DEPTH_LIMIT, FOUND_BLURBS, LOST_BLURBS } from './constants'
|
||||
import { msatsToSats, numWithUnits } from './format'
|
||||
import models from '../api/models'
|
||||
|
||||
|
@ -83,7 +83,7 @@ const sendNotification = (subscription, payload) => {
|
|||
})
|
||||
}
|
||||
|
||||
export async function sendUserNotification (userId, notification) {
|
||||
async function sendUserNotification (userId, notification) {
|
||||
try {
|
||||
notification.data ??= {}
|
||||
if (notification.item) {
|
||||
|
@ -243,6 +243,27 @@ export const notifyZapped = async ({ models, id }) => {
|
|||
}
|
||||
}
|
||||
|
||||
export const notifyMention = async (userId, item) => {
|
||||
try {
|
||||
await sendUserNotification(userId, {
|
||||
title: 'you were mentioned',
|
||||
body: item.text,
|
||||
item,
|
||||
tag: 'MENTION'
|
||||
}).catch(console.error)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export const notifyReferral = async (userId) => {
|
||||
try {
|
||||
await sendUserNotification(userId, { title: 'someone joined via one of your referral links', tag: 'REFERRAL' }).catch(console.error)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export const notifyTerritoryTransfer = async ({ models, sub, to }) => {
|
||||
try {
|
||||
await sendUserNotification(to.id, {
|
||||
|
@ -253,3 +274,64 @@ export const notifyTerritoryTransfer = async ({ models, sub, to }) => {
|
|||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export async function notifyEarner (userId, earnings) {
|
||||
const fmt = msats => numWithUnits(msatsToSats(msats, { abbreviate: false }))
|
||||
|
||||
const title = `you stacked ${fmt(earnings.msats)} in rewards`
|
||||
const tag = 'EARN'
|
||||
let body = ''
|
||||
if (earnings.POST) body += `#${earnings.POST.bestRank} among posts with ${fmt(earnings.POST.msats)} in total\n`
|
||||
if (earnings.COMMENT) body += `#${earnings.COMMENT.bestRank} among comments with ${fmt(earnings.COMMENT.msats)} in total\n`
|
||||
if (earnings.TIP_POST) body += `#${earnings.TIP_POST.bestRank} in post zapping with ${fmt(earnings.TIP_POST.msats)} in total\n`
|
||||
if (earnings.TIP_COMMENT) body += `#${earnings.TIP_COMMENT.bestRank} in comment zapping with ${fmt(earnings.TIP_COMMENT.msats)} in total`
|
||||
|
||||
try {
|
||||
await sendUserNotification(userId, { title, tag, body })
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export async function notifyDeposit (userId, invoice) {
|
||||
try {
|
||||
await sendUserNotification(userId, {
|
||||
title: `${numWithUnits(msatsToSats(invoice.received_mtokens), { abbreviate: false })} were deposited in your account`,
|
||||
body: invoice.comment || undefined,
|
||||
tag: 'DEPOSIT',
|
||||
data: { sats: msatsToSats(invoice.received_mtokens) }
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export async function notifyNewStreak (userId, streak) {
|
||||
const index = streak.id % FOUND_BLURBS.length
|
||||
const blurb = FOUND_BLURBS[index]
|
||||
|
||||
try {
|
||||
await sendUserNotification(userId, {
|
||||
title: 'you found a cowboy hat',
|
||||
body: blurb,
|
||||
tag: 'STREAK-FOUND'
|
||||
}).catch(console.error)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export async function notifyStreakLost (userId, streak) {
|
||||
const index = streak.id % LOST_BLURBS.length
|
||||
const blurb = LOST_BLURBS[index]
|
||||
|
||||
try {
|
||||
await sendUserNotification(userId, {
|
||||
title: 'you lost your cowboy hat',
|
||||
body: blurb,
|
||||
tag: 'STREAK-LOST'
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import { PrismaAdapter } from '@auth/prisma-adapter'
|
|||
import { getToken } from 'next-auth/jwt'
|
||||
import { NodeNextRequest } from 'next/dist/server/base-http/node'
|
||||
import { schnorr } from '@noble/curves/secp256k1'
|
||||
import { sendUserNotification } from '../../../lib/webPush'
|
||||
import { notifyReferral } from '../../../lib/webPush'
|
||||
|
||||
/**
|
||||
* Stores userIds in user table
|
||||
|
@ -66,7 +66,7 @@ function getCallbacks (req) {
|
|||
const referrer = await prisma.user.findUnique({ where: { name: req.cookies.sn_referrer } })
|
||||
if (referrer) {
|
||||
await prisma.user.update({ where: { id: user.id }, data: { referrerId: referrer.id } })
|
||||
sendUserNotification(referrer.id, { title: 'someone joined via one of your referral links', tag: 'REFERRAL' }).catch(console.error)
|
||||
notifyReferral(referrer.id)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import serialize from '../api/resolvers/serial.js'
|
||||
import { sendUserNotification } from '../lib/webPush.js'
|
||||
import { msatsToSats, numWithUnits } from '../lib/format.js'
|
||||
import { notifyEarner } from '../lib/webPush.js'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { proportions } from '../lib/madness.js'
|
||||
import { SN_USER_IDS } from '../lib/constants.js'
|
||||
|
@ -106,24 +105,10 @@ export async function earn ({ name }) {
|
|||
}
|
||||
}
|
||||
|
||||
Promise.allSettled(Object.entries(notifications).map(([userId, earnings]) =>
|
||||
sendUserNotification(parseInt(userId, 10), buildUserNotification(earnings))
|
||||
)).catch(console.error)
|
||||
Promise.allSettled(
|
||||
Object.entries(notifications).map(([userId, earnings]) => notifyEarner(parseInt(userId, 10), earnings))
|
||||
).catch(console.error)
|
||||
} finally {
|
||||
models.$disconnect().catch(console.error)
|
||||
}
|
||||
}
|
||||
|
||||
function buildUserNotification (earnings) {
|
||||
const fmt = msats => numWithUnits(msatsToSats(msats, { abbreviate: false }))
|
||||
|
||||
const title = `you stacked ${fmt(earnings.msats)} in rewards`
|
||||
const tag = 'EARN'
|
||||
let body = ''
|
||||
if (earnings.POST) body += `#${earnings.POST.bestRank} among posts with ${fmt(earnings.POST.msats)} in total\n`
|
||||
if (earnings.COMMENT) body += `#${earnings.COMMENT.bestRank} among comments with ${fmt(earnings.COMMENT.msats)} in total\n`
|
||||
if (earnings.TIP_POST) body += `#${earnings.TIP_POST.bestRank} in post zapping with ${fmt(earnings.TIP_POST.msats)} in total\n`
|
||||
if (earnings.TIP_COMMENT) body += `#${earnings.TIP_COMMENT.bestRank} in comment zapping with ${fmt(earnings.TIP_COMMENT.msats)} in total`
|
||||
|
||||
return { title, tag, body }
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { sendUserNotification } from '../lib/webPush'
|
||||
import { FOUND_BLURBS, LOST_BLURBS } from '../lib/constants'
|
||||
import { notifyNewStreak, notifyStreakLost } from '../lib/webPush'
|
||||
|
||||
const STREAK_THRESHOLD = 100
|
||||
|
||||
|
@ -61,17 +60,7 @@ export async function computeStreaks ({ models }) {
|
|||
WHERE ending_streaks.id = "Streak"."userId" AND "endedAt" IS NULL
|
||||
RETURNING "Streak".id, ending_streaks."id" AS "userId"`
|
||||
|
||||
Promise.allSettled(
|
||||
endingStreaks.map(({ id, userId }) => {
|
||||
const index = id % LOST_BLURBS.length
|
||||
const blurb = LOST_BLURBS[index]
|
||||
return sendUserNotification(userId, {
|
||||
title: 'you lost your cowboy hat',
|
||||
body: blurb,
|
||||
tag: 'STREAK-LOST'
|
||||
}).catch(console.error)
|
||||
})
|
||||
)
|
||||
Promise.allSettled(endingStreaks.map(streak => notifyStreakLost(streak.userId, streak)))
|
||||
}
|
||||
|
||||
export async function checkStreak ({ data: { id }, models }) {
|
||||
|
@ -115,11 +104,5 @@ export async function checkStreak ({ data: { id }, models }) {
|
|||
if (!streak) return
|
||||
|
||||
// new streak started for user
|
||||
const index = streak.id % FOUND_BLURBS.length
|
||||
const blurb = FOUND_BLURBS[index]
|
||||
sendUserNotification(id, {
|
||||
title: 'you found a cowboy hat',
|
||||
body: blurb,
|
||||
tag: 'STREAK-FOUND'
|
||||
}).catch(console.error)
|
||||
notifyNewStreak(id, streak)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,7 @@ import {
|
|||
getInvoice, getPayment, cancelHodlInvoice, deletePayment,
|
||||
subscribeToInvoices, subscribeToPayments, subscribeToInvoice
|
||||
} from 'ln-service'
|
||||
import { sendUserNotification } from '../lib/webPush'
|
||||
import { msatsToSats, numWithUnits } from '../lib/format'
|
||||
import { notifyDeposit } from '../lib/webPush'
|
||||
import { INVOICE_RETENTION_DAYS } from '../lib/constants'
|
||||
import { datePivot, sleep } from '../lib/time.js'
|
||||
import retry from 'async-retry'
|
||||
|
@ -129,14 +128,8 @@ async function checkInvoice ({ data: { hash }, boss, models, lnd }) {
|
|||
|
||||
// don't send notifications for JIT invoices
|
||||
if (dbInv.preimage) return
|
||||
|
||||
if (code === 0) {
|
||||
sendUserNotification(dbInv.userId, {
|
||||
title: `${numWithUnits(msatsToSats(inv.received_mtokens), { abbreviate: false })} were deposited in your account`,
|
||||
body: dbInv.comment || undefined,
|
||||
tag: 'DEPOSIT',
|
||||
data: { sats: msatsToSats(inv.received_mtokens) }
|
||||
}).catch(console.error)
|
||||
notifyDeposit(dbInv.userId, { comment: dbInv.comment, ...inv })
|
||||
}
|
||||
|
||||
return await boss.send('nip57', { hash })
|
||||
|
|
Loading…
Reference in New Issue