Skip to content

Commit

Permalink
Add push notifications for daily rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Oct 3, 2023
1 parent d6945ff commit 34c48ea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion api/webPush/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const createUserFilter = (tag) => {
MENTION: 'noteMentions',
TIP: 'noteItemSats',
FORWARDEDTIP: 'noteForwardedSats',
REFERRAL: 'noteInvites'
REFERRAL: 'noteInvites',
EARN: 'noteEarning'
}
const key = tagMap[tag.split('-')[0]]
return key ? { user: { [key]: true } } : undefined
Expand Down
4 changes: 2 additions & 2 deletions sw/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ self.addEventListener('push', async function (event) {
if (!payload) return
const { tag } = payload.options
event.waitUntil((async () => {
// TIP notifications simply replace the previous notifications
if (!tag || tag.split('-')[0] === 'TIP') {
// TIP and EARN notifications simply replace the previous notifications
if (!tag || ['TIP', 'EARN'].includes(tag.split('-')[0])) {
return self.registration.showNotification(payload.title, payload.options)
}

Expand Down
5 changes: 5 additions & 0 deletions worker/earn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import serialize from '../api/resolvers/serial.js'
import { sendUserNotification } from '../api/webPush/index.js'
import { ANON_USER_ID } from '../lib/constants.js'

const ITEM_EACH_REWARD = 4.0
Expand Down Expand Up @@ -151,6 +152,10 @@ export function earn ({ models }) {
await serialize(models,
models.$executeRaw`SELECT earn(${earner.userId}::INTEGER, ${earnings},
${now}::timestamp without time zone, ${earner.type}::"EarnType", ${earner.id}::INTEGER, ${earner.rank}::INTEGER)`)
sendUserNotification(earner.userId, {
title: `you stacked ${earnings} in rewards`,
tag: 'EARN'
}).catch(console.error)
}
})

Expand Down

0 comments on commit 34c48ea

Please sign in to comment.