Skip to content

Commit

Permalink
Add push notifications for earning cowboy hats
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Oct 3, 2023
1 parent c507dda commit 7fd0a3f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion api/webPush/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 1 addition & 19 deletions components/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
]
16 changes: 15 additions & 1 deletion worker/streak.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { sendUserNotification } from '../api/webPush'
import { FOUND_BLURBS } from '../lib/constants'

const STREAK_THRESHOLD = 100

export function computeStreaks ({ models }) {
Expand Down Expand Up @@ -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
Expand All @@ -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'
})
}
}

0 comments on commit 7fd0a3f

Please sign in to comment.