Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: daily stacked and spent sats notification #1756

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/webPush.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,11 @@ export async function notifyStreakLost (userId, streak) {
}
}

export async function notifySatSummary (userId, stats) {
export async function notifySatSummary (userId, stacked, spent) {
try {
await sendUserNotification(userId, {
title: 'your daily sat summary is ready',
body: `you stacked ${numWithUnits(stats.stacked, { abbreviate: false })} and spent ${numWithUnits(stats.spent, { abbreviate: false })}`,
body: `you stacked ${numWithUnits(stacked, { abbreviate: false })} and spent ${numWithUnits(spent, { abbreviate: false })}`,
tag: 'DAILY_STATS'
})
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'
import { indexItem, indexAllItems } from './search'
import { timestampItem } from './ots'
import { computeStreaks, checkStreak } from './streak'
import { summarizeDailySats } from './satSummary'
import { dailySatSummary } from './satSummary'
import { nip57 } from './nostr'
import fetch from 'cross-fetch'
import { authenticatedLndGrpc } from '@/lib/lnd'
Expand Down Expand Up @@ -132,7 +132,7 @@ async function work () {
await boss.work('earn', jobWrapper(earn))
await boss.work('streak', jobWrapper(computeStreaks))
await boss.work('checkStreak', jobWrapper(checkStreak))
await boss.work('dailySatSummary', jobWrapper(summarizeDailySats))
await boss.work('dailySatSummary', jobWrapper(dailySatSummary))
await boss.work('nip57', jobWrapper(nip57))
await boss.work('views-*', jobWrapper(views))
await boss.work('rankViews', jobWrapper(rankViews))
Expand Down
18 changes: 9 additions & 9 deletions worker/satSummary.js
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realized I didn't push this before, sorry

Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { notifySatSummary } from '@/lib/webPush'
export async function summarizeDailySats ({ data: { userId }, models }) {
export async function dailySatSummary ({ models }) {
try {
const stats = await models.$queryRaw`
SELECT
sum(msats_stacked) as stacked, sum(msats_spent) as spent,
id as userId, sum(msats_stacked) as stacked, sum(msats_spent) as spent
FROM user_stats_days
WHERE id = ${userId}
AND t >= date_trunc('day', CURRENT_DATE - INTERVAL '1 day')
WHERE t >= date_trunc('day', CURRENT_DATE - INTERVAL '1 day')
AND t <= date_trunc('day', CURRENT_DATE)
GROUP BY id
HAVING sum(msats_stacked) != 0 OR sum(msats_spent) != 0
LIMIT 1
`

if (stats.length) {
await notifySatSummary({
userId,
satSummary: stats[0]
})
for (const stat of stats) {
const user = await models.user.findUnique({ where: { id: stat.userid } })
if (user && user.noteSatSummary) {
await notifySatSummary(stat.userid, stat.stacked || 0, stat.spent || 0)
}
}
}
} catch (err) {
console.error('failed to process daily sat summary', err)
Expand Down
Loading