From d6945ff8a2bd184f0bf4782c15512edb91e6298b Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 28 Sep 2023 23:46:07 +0200 Subject: [PATCH 01/13] Add push notifications for referrals --- api/webPush/index.js | 3 ++- pages/api/auth/[...nextauth].js | 2 ++ sw/index.js | 5 ++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/api/webPush/index.js b/api/webPush/index.js index a09cb52a0..96f9da01a 100644 --- a/api/webPush/index.js +++ b/api/webPush/index.js @@ -37,7 +37,8 @@ const createUserFilter = (tag) => { REPLY: 'noteAllDescendants', MENTION: 'noteMentions', TIP: 'noteItemSats', - FORWARDEDTIP: 'noteForwardedSats' + FORWARDEDTIP: 'noteForwardedSats', + REFERRAL: 'noteInvites' } const key = tagMap[tag.split('-')[0]] return key ? { user: { [key]: true } } : undefined diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index 2dc0e4ab3..8cf95a0fd 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -11,6 +11,7 @@ import { decode, getToken } from 'next-auth/jwt' import { NodeNextRequest } from 'next/dist/server/base-http/node' import jose1 from 'jose1' import { schnorr } from '@noble/curves/secp256k1' +import { sendUserNotification } from '../../../api/webPush' function getCallbacks (req) { return { @@ -42,6 +43,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) } } diff --git a/sw/index.js b/sw/index.js index baf6525f9..886eadd75 100644 --- a/sw/index.js +++ b/sw/index.js @@ -49,7 +49,8 @@ self.addEventListener('push', async function (event) { if (!payload) return const { tag } = payload.options event.waitUntil((async () => { - if (!['REPLY', 'MENTION'].includes(tag)) { + // TIP notifications simply replace the previous notifications + if (!tag || tag.split('-')[0] === 'TIP') { return self.registration.showNotification(payload.title, payload.options) } @@ -71,6 +72,8 @@ self.addEventListener('push', async function (event) { title = `You have ${amount} new replies` } else if (tag === 'MENTION') { title = `You were mentioned ${amount} times` + } else if (tag === 'REFERRAL') { + title = `${amount} stackers joined via your referral links` } currentNotification.close() const { icon } = currentNotification From 34c48ea3ac9c9a904356a423ee3036a783964b37 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 28 Sep 2023 23:52:25 +0200 Subject: [PATCH 02/13] Add push notifications for daily rewards --- api/webPush/index.js | 3 ++- sw/index.js | 4 ++-- worker/earn.js | 5 +++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/api/webPush/index.js b/api/webPush/index.js index 96f9da01a..3bacfc885 100644 --- a/api/webPush/index.js +++ b/api/webPush/index.js @@ -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 diff --git a/sw/index.js b/sw/index.js index 886eadd75..89738adc2 100644 --- a/sw/index.js +++ b/sw/index.js @@ -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) } diff --git a/worker/earn.js b/worker/earn.js index 336bb48db..f1916aff7 100644 --- a/worker/earn.js +++ b/worker/earn.js @@ -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 @@ -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) } }) From c507dda812a4a59cac26b39c32bf9b45eae14a6d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 29 Sep 2023 00:03:20 +0200 Subject: [PATCH 03/13] Add push notifications for deposits --- api/webPush/index.js | 3 ++- sw/index.js | 17 ++++++++++++----- worker/wallet.js | 7 +++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/api/webPush/index.js b/api/webPush/index.js index 3bacfc885..fa8b90a8d 100644 --- a/api/webPush/index.js +++ b/api/webPush/index.js @@ -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 diff --git a/sw/index.js b/sw/index.js index 89738adc2..1a9087c74 100644 --- a/sw/index.js +++ b/sw/index.js @@ -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 @@ -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 @@ -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 } }) })()) }) diff --git a/worker/wallet.js b/worker/wallet.js index 063bbbcc4..747cefacf 100644 --- a/worker/wallet.js +++ b/worker/wallet.js @@ -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 } @@ -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 }) } From 7fd0a3fa7849bcb5cc656cf35948307621f740dd Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 29 Sep 2023 01:01:03 +0200 Subject: [PATCH 04/13] Add push notifications for earning cowboy hats --- api/webPush/index.js | 3 ++- components/notifications.js | 20 +------------------- lib/constants.js | 17 +++++++++++++++++ worker/streak.js | 16 +++++++++++++++- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/api/webPush/index.js b/api/webPush/index.js index fa8b90a8d..cf08f688b 100644 --- a/api/webPush/index.js +++ b/api/webPush/index.js @@ -40,7 +40,8 @@ const createUserFilter = (tag) => { FORWARDEDTIP: 'noteForwardedSats', REFERRAL: 'noteInvites', EARN: 'noteEarning', - DEPOSIT: 'noteDeposits' + DEPOSIT: 'noteDeposits', + STREAK: 'noteCowboyHat' } const key = tagMap[tag.split('-')[0]] return key ? { user: { [key]: true } } : undefined diff --git a/components/notifications.js b/components/notifications.js index 33b0014db..21e97c038 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -11,7 +11,7 @@ import { dayMonthYear, timeSince } from '../lib/time' import Link from 'next/link' import Check from '../svgs/check-double-line.svg' import HandCoin from '../svgs/hand-coin-fill.svg' -import { COMMENT_DEPTH_LIMIT } from '../lib/constants' +import { COMMENT_DEPTH_LIMIT, LOST_BLURBS, FOUND_BLURBS } from '../lib/constants' import CowboyHatIcon from '../svgs/cowboy.svg' import BaldIcon from '../svgs/bald.svg' import { RootProvider } from './root' @@ -123,24 +123,6 @@ const defaultOnClick = n => { function Streak ({ n }) { function blurb (n) { const index = Number(n.id) % 6 - const FOUND_BLURBS = [ - 'The harsh frontier is no place for the unprepared. This hat will protect you from the sun, dust, and other elements Mother Nature throws your way.', - 'A cowboy is nothing without a cowboy hat. Take good care of it, and it will protect you from the sun, dust, and other elements on your journey.', - "This is not just a hat, it's a matter of survival. Take care of this essential tool, and it will shield you from the scorching sun and the elements.", - "A cowboy hat isn't just a fashion statement. It's your last defense against the unforgiving elements of the Wild West. Hang onto it tight.", - "A good cowboy hat is worth its weight in gold, shielding you from the sun, wind, and dust of the western frontier. Don't lose it.", - 'Your cowboy hat is the key to your survival in the wild west. Treat it with respect and it will protect you from the elements.' - ] - - const LOST_BLURBS = [ - 'your cowboy hat was taken by the wind storm that blew in from the west. No worries, a true cowboy always finds another hat.', - "you left your trusty cowboy hat in the saloon before leaving town. You'll need a replacement for the long journey west.", - 'you lost your cowboy hat in a wild shoot-out on the outskirts of town. Tough luck, tIme to start searching for another one.', - 'you ran out of food and had to trade your hat for supplies. Better start looking for another hat.', - "your hat was stolen by a mischievous prairie dog. You won't catch the dog, but you can always find another hat.", - 'you lost your hat while crossing the river on your journey west. Maybe you can find a replacement hat in the next town.' - ] - if (n.days) { return `After ${numWithUnits(n.days, { abbreviate: false, diff --git a/lib/constants.js b/lib/constants.js index d679689b9..cc1eeb7d8 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -52,3 +52,20 @@ export const ANON_COMMENT_FEE = 100 export const SSR = typeof window === 'undefined' export const MAX_FORWARDS = 5 export const LNURLP_COMMENT_MAX_LENGTH = 1000 + +export const FOUND_BLURBS = [ + 'The harsh frontier is no place for the unprepared. This hat will protect you from the sun, dust, and other elements Mother Nature throws your way.', + 'A cowboy is nothing without a cowboy hat. Take good care of it, and it will protect you from the sun, dust, and other elements on your journey.', + "This is not just a hat, it's a matter of survival. Take care of this essential tool, and it will shield you from the scorching sun and the elements.", + "A cowboy hat isn't just a fashion statement. It's your last defense against the unforgiving elements of the Wild West. Hang onto it tight.", + "A good cowboy hat is worth its weight in gold, shielding you from the sun, wind, and dust of the western frontier. Don't lose it.", + 'Your cowboy hat is the key to your survival in the wild west. Treat it with respect and it will protect you from the elements.' +] +export const LOST_BLURBS = [ + 'your cowboy hat was taken by the wind storm that blew in from the west. No worries, a true cowboy always finds another hat.', + "you left your trusty cowboy hat in the saloon before leaving town. You'll need a replacement for the long journey west.", + 'you lost your cowboy hat in a wild shoot-out on the outskirts of town. Tough luck, tIme to start searching for another one.', + 'you ran out of food and had to trade your hat for supplies. Better start looking for another hat.', + "your hat was stolen by a mischievous prairie dog. You won't catch the dog, but you can always find another hat.", + 'you lost your hat while crossing the river on your journey west. Maybe you can find a replacement hat in the next town.' +] diff --git a/worker/streak.js b/worker/streak.js index 46e578c7f..5b73f6f71 100644 --- a/worker/streak.js +++ b/worker/streak.js @@ -1,3 +1,6 @@ +import { sendUserNotification } from '../api/webPush' +import { FOUND_BLURBS } from '../lib/constants' + const STREAK_THRESHOLD = 100 export function computeStreaks ({ models }) { @@ -81,7 +84,7 @@ export function checkStreak ({ models }) { return } - await models.$executeRaw` + const affected = await models.$executeRaw` WITH streak_started (id) AS ( SELECT "userId" FROM @@ -106,5 +109,16 @@ export function checkStreak ({ models }) { FROM streak_started` console.log('done checking streak', id) + + if (!affected) return + + // new streak started for user + const index = Math.floor(Math.random() * FOUND_BLURBS.length) + const blurb = FOUND_BLURBS[index] + sendUserNotification(id, { + title: 'you found a cowboy hat', + body: blurb, + tag: 'STREAK' + }) } } From 2962913be3c37111e33c51a135ba377fa7d08e0e Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 3 Oct 2023 18:25:05 +0200 Subject: [PATCH 05/13] Use streak id to synchronize blurb --- worker/streak.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/worker/streak.js b/worker/streak.js index 5b73f6f71..3223ffe5b 100644 --- a/worker/streak.js +++ b/worker/streak.js @@ -72,7 +72,7 @@ export function checkStreak ({ models }) { console.log('checking streak', id) // if user is actively streaking skip - const streak = await models.streak.findFirst({ + let streak = await models.streak.findFirst({ where: { userId: Number(id), endedAt: null @@ -84,7 +84,7 @@ export function checkStreak ({ models }) { return } - const affected = await models.$executeRaw` + [streak] = await models.$queryRaw` WITH streak_started (id) AS ( SELECT "userId" FROM @@ -106,14 +106,15 @@ export function checkStreak ({ models }) { ) INSERT INTO "Streak" ("userId", "startedAt", created_at, updated_at) SELECT id, (now() AT TIME ZONE 'America/Chicago')::date, now_utc(), now_utc() - FROM streak_started` + FROM streak_started + RETURNING "Streak".id` console.log('done checking streak', id) - if (!affected) return + if (!streak) return // new streak started for user - const index = Math.floor(Math.random() * FOUND_BLURBS.length) + const index = streak.id % FOUND_BLURBS.length const blurb = FOUND_BLURBS[index] sendUserNotification(id, { title: 'you found a cowboy hat', From 2bf7b5cd6cbdafeab7341339aad9196f06be860a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 3 Oct 2023 18:27:43 +0200 Subject: [PATCH 06/13] Fix usage of magic number for blurbs --- components/notifications.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/notifications.js b/components/notifications.js index 21e97c038..226e53f58 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -122,7 +122,7 @@ const defaultOnClick = n => { function Streak ({ n }) { function blurb (n) { - const index = Number(n.id) % 6 + const index = Number(n.id) % Math.min(FOUND_BLURBS.length, LOST_BLURBS.length) if (n.days) { return `After ${numWithUnits(n.days, { abbreviate: false, From 31ace27bd11e067f724192d01fecd3eb4bfc0b6a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 18:36:52 +0200 Subject: [PATCH 07/13] Fix missing catch --- worker/streak.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worker/streak.js b/worker/streak.js index 3223ffe5b..a912723b9 100644 --- a/worker/streak.js +++ b/worker/streak.js @@ -120,6 +120,6 @@ export function checkStreak ({ models }) { title: 'you found a cowboy hat', body: blurb, tag: 'STREAK' - }) + }).catch(console.error) } } From c49d19c6a2d53ed541d2f1731505a42912c09146 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 19:12:16 +0200 Subject: [PATCH 08/13] Add push notification for losing cowboy hats --- worker/streak.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/worker/streak.js b/worker/streak.js index a912723b9..83144974c 100644 --- a/worker/streak.js +++ b/worker/streak.js @@ -1,5 +1,5 @@ import { sendUserNotification } from '../api/webPush' -import { FOUND_BLURBS } from '../lib/constants' +import { FOUND_BLURBS, LOST_BLURBS } from '../lib/constants' const STREAK_THRESHOLD = 100 @@ -10,8 +10,8 @@ export function computeStreaks ({ models }) { // get all eligible users in the last day // if the user doesn't have an active streak, add one // if they have an active streak but didn't maintain it, end it - await models.$executeRawUnsafe( - `WITH day_streaks (id) AS ( + const endingStreaks = await models.$queryRaw` + WITH day_streaks (id) AS ( SELECT "userId" FROM ((SELECT "userId", floor(sum("ItemAct".msats)/1000) as sats_spent @@ -61,7 +61,20 @@ export function computeStreaks ({ models }) { UPDATE "Streak" SET "endedAt" = (now() AT TIME ZONE 'America/Chicago' - interval '1 day')::date, updated_at = now_utc() FROM ending_streaks - WHERE ending_streaks.id = "Streak"."userId" AND "endedAt" IS NULL`) + 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' + }).catch(console.error) + }) + ) console.log('done computing streaks') } From 1ba4cc282dd56dfc749937de15d2dd7376bcc7bc Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 19:31:48 +0200 Subject: [PATCH 09/13] Fix null in deposit push notification --- worker/wallet.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worker/wallet.js b/worker/wallet.js index 747cefacf..1888d9813 100644 --- a/worker/wallet.js +++ b/worker/wallet.js @@ -33,7 +33,7 @@ export function checkInvoice ({ boss, models, lnd }) { 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, + body: dbInv.comment || undefined, tag: 'DEPOSIT' }).catch(console.error) return boss.send('nip57', { hash }) From f7557b46c6aca9b739f09e3697536bd4753323b8 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 20:06:26 +0200 Subject: [PATCH 10/13] Add push notification for invites --- api/webPush/index.js | 1 + pages/invites/[id].js | 3 +++ sw/index.js | 2 ++ 3 files changed, 6 insertions(+) diff --git a/api/webPush/index.js b/api/webPush/index.js index cf08f688b..2bb321711 100644 --- a/api/webPush/index.js +++ b/api/webPush/index.js @@ -39,6 +39,7 @@ const createUserFilter = (tag) => { TIP: 'noteItemSats', FORWARDEDTIP: 'noteForwardedSats', REFERRAL: 'noteInvites', + INVITE: 'noteInvites', EARN: 'noteEarning', DEPOSIT: 'noteDeposits', STREAK: 'noteCowboyHat' diff --git a/pages/invites/[id].js b/pages/invites/[id].js index fda45c636..9270b0f86 100644 --- a/pages/invites/[id].js +++ b/pages/invites/[id].js @@ -9,6 +9,7 @@ import getSSRApolloClient from '../../api/ssrApollo' import Link from 'next/link' import { CenterLayout } from '../../components/layout' import { getAuthOptions } from '../api/auth/[...nextauth]' +import { sendUserNotification } from '../../api/webPush' export async function getServerSideProps ({ req, res, query: { id, error = null } }) { const session = await getServerSession(req, res, getAuthOptions(req)) @@ -36,6 +37,8 @@ export async function getServerSideProps ({ req, res, query: { id, error = null // catch any errors and just ignore them for now await serialize(models, models.$queryRawUnsafe('SELECT invite_drain($1::INTEGER, $2::INTEGER)', session.user.id, id)) + const invite = await models.invite.findUnique({ where: { id } }) + sendUserNotification(invite.userId, { title: 'your invite has been redeemed', tag: 'INVITE' }).catch(console.error) } catch (e) { console.log(e) } diff --git a/sw/index.js b/sw/index.js index 1a9087c74..933fe1608 100644 --- a/sw/index.js +++ b/sw/index.js @@ -76,6 +76,8 @@ self.addEventListener('push', async function (event) { newTitle = `You were mentioned ${amount} times` } else if (tag === 'REFERRAL') { newTitle = `${amount} stackers joined via your referral links` + } else if (tag === 'INVITE') { + newTitle = `your invite has been redeemed by ${amount} stackers` } else if (tag === 'DEPOSIT') { const currentAmount = Number(currentNotification.title.split(' ')[0]) const incomingAmount = Number(title.split(' ')[0]) From 540ba42ba70c2a5d58c74e36a506689c9e90b11c Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 20:53:52 +0200 Subject: [PATCH 11/13] Don't replace streak push notifications --- worker/streak.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/worker/streak.js b/worker/streak.js index 83144974c..4f90b2dd6 100644 --- a/worker/streak.js +++ b/worker/streak.js @@ -71,7 +71,7 @@ export function computeStreaks ({ models }) { return sendUserNotification(userId, { title: 'you lost your cowboy hat', body: blurb, - tag: 'STREAK' + tag: 'STREAK-LOST' }).catch(console.error) }) ) @@ -132,7 +132,7 @@ export function checkStreak ({ models }) { sendUserNotification(id, { title: 'you found a cowboy hat', body: blurb, - tag: 'STREAK' + tag: 'STREAK-FOUND' }).catch(console.error) } } From 9ad143459aa0de48ac0637334c1698dc34afe13b Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 21:10:07 +0200 Subject: [PATCH 12/13] Fix missing unit in daily reward push notification title --- worker/earn.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/worker/earn.js b/worker/earn.js index f1916aff7..23c844e66 100644 --- a/worker/earn.js +++ b/worker/earn.js @@ -1,6 +1,7 @@ import serialize from '../api/resolvers/serial.js' import { sendUserNotification } from '../api/webPush/index.js' import { ANON_USER_ID } from '../lib/constants.js' +import { msatsToSats, numWithUnits } from '../lib/format.js' const ITEM_EACH_REWARD = 4.0 const UPVOTE_EACH_REWARD = 4.0 @@ -153,7 +154,7 @@ export function earn ({ 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`, + title: `you stacked ${numWithUnits(msatsToSats(earnings), { abbreviate: false })} in rewards`, tag: 'EARN' }).catch(console.error) } From 21c586aaf0310fe15ab40d7454321eb68bc2a89a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 4 Oct 2023 21:17:05 +0200 Subject: [PATCH 13/13] Attach sats to payload options instead of parsing title --- sw/index.js | 13 +++++++------ worker/wallet.js | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/sw/index.js b/sw/index.js index 933fe1608..97268b3c7 100644 --- a/sw/index.js +++ b/sw/index.js @@ -48,7 +48,6 @@ 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 @@ -70,6 +69,7 @@ self.addEventListener('push', async function (event) { const currentNotification = notifications[0] const amount = currentNotification.data?.amount ? currentNotification.data.amount + 1 : 2 let newTitle = '' + const data = {} if (tag === 'REPLY') { newTitle = `You have ${amount} new replies` } else if (tag === 'MENTION') { @@ -79,14 +79,15 @@ self.addEventListener('push', async function (event) { } else if (tag === 'INVITE') { newTitle = `your invite has been redeemed by ${amount} stackers` } 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` + const currentSats = currentNotification.data.sats + const incomingSats = payload.options.data.sats + const newSats = currentSats + incomingSats + data.sats = newSats + newTitle = `${numWithUnits(newSats, { abbreviate: false })} were deposited in your account` } currentNotification.close() const { icon } = currentNotification - return self.registration.showNotification(newTitle, { icon, tag, data: { url: '/notifications', amount } }) + return self.registration.showNotification(newTitle, { icon, tag, data: { url: '/notifications', amount, ...data } }) })()) }) diff --git a/worker/wallet.js b/worker/wallet.js index 1888d9813..dd9c14c7c 100644 --- a/worker/wallet.js +++ b/worker/wallet.js @@ -34,7 +34,8 @@ export function checkInvoice ({ boss, models, lnd }) { sendUserNotification(dbInv.userId, { title: `${numWithUnits(msatsToSats(inv.received_mtokens), { abbreviate: false })} were deposited in your account`, body: dbInv.comment || undefined, - tag: 'DEPOSIT' + tag: 'DEPOSIT', + data: { sats: msatsToSats(inv.received_mtokens) } }).catch(console.error) return boss.send('nip57', { hash }) }