From 7fd0a3fa7849bcb5cc656cf35948307621f740dd Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 29 Sep 2023 01:01:03 +0200 Subject: [PATCH] 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' + }) } }