Skip to content

Commit

Permalink
Add push notifications for deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Oct 3, 2023
1 parent 34c48ea commit c507dda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion api/webPush/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const createUserFilter = (tag) => {
TIP: 'noteItemSats',
FORWARDEDTIP: 'noteForwardedSats',
REFERRAL: 'noteInvites',
EARN: 'noteEarning'
EARN: 'noteEarning',
DEPOSIT: 'noteDeposits'
}
const key = tagMap[tag.split('-')[0]]
return key ? { user: { [key]: true } } : undefined
Expand Down
17 changes: 12 additions & 5 deletions sw/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NetworkOnly } from 'workbox-strategies'
import { enable } from 'workbox-navigation-preload'
import manifest from './precache-manifest.json'
import ServiceWorkerStorage from 'serviceworker-storage'
import { numWithUnits } from '../lib/format'

// comment out to enable workbox console logs
self.__WB_DISABLE_DEV_LOGS = true
Expand Down Expand Up @@ -47,6 +48,7 @@ offlineFallback({ pageFallback: '/offline' })
self.addEventListener('push', async function (event) {
const payload = event.data?.json()
if (!payload) return
const { title } = payload
const { tag } = payload.options
event.waitUntil((async () => {
// TIP and EARN notifications simply replace the previous notifications
Expand All @@ -67,17 +69,22 @@ self.addEventListener('push', async function (event) {
}
const currentNotification = notifications[0]
const amount = currentNotification.data?.amount ? currentNotification.data.amount + 1 : 2
let title = ''
let newTitle = ''
if (tag === 'REPLY') {
title = `You have ${amount} new replies`
newTitle = `You have ${amount} new replies`
} else if (tag === 'MENTION') {
title = `You were mentioned ${amount} times`
newTitle = `You were mentioned ${amount} times`
} else if (tag === 'REFERRAL') {
title = `${amount} stackers joined via your referral links`
newTitle = `${amount} stackers joined via your referral links`
} else if (tag === 'DEPOSIT') {
const currentAmount = Number(currentNotification.title.split(' ')[0])
const incomingAmount = Number(title.split(' ')[0])
const newAmount = currentAmount + incomingAmount
newTitle = `${numWithUnits(newAmount, { abbreviate: false })} were deposited in your account`
}
currentNotification.close()
const { icon } = currentNotification
return self.registration.showNotification(title, { icon, tag, data: { url: '/notifications', amount } })
return self.registration.showNotification(newTitle, { icon, tag, data: { url: '/notifications', amount } })
})())
})

Expand Down
7 changes: 7 additions & 0 deletions worker/wallet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import serialize from '../api/resolvers/serial.js'
import { getInvoice, getPayment, cancelHodlInvoice } from 'ln-service'
import { datePivot } from '../lib/time.js'
import { sendUserNotification } from '../api/webPush/index.js'
import { msatsToSats, numWithUnits } from '../lib/format'

const walletOptions = { startAfter: 5, retryLimit: 21, retryBackoff: true }

Expand Down Expand Up @@ -29,6 +31,11 @@ export function checkInvoice ({ boss, models, lnd }) {
// we manually confirm them when we settle them
await serialize(models,
models.$executeRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)})`)
sendUserNotification(dbInv.userId, {
title: `${numWithUnits(msatsToSats(inv.received_mtokens), { abbreviate: false })} were deposited in your account`,
body: dbInv.comment,
tag: 'DEPOSIT'
}).catch(console.error)
return boss.send('nip57', { hash })
}

Expand Down

0 comments on commit c507dda

Please sign in to comment.