Rename to sendPushSubscriptionReply & remove unused nid argument (#2273)

* Refactor push subscription reply

* Remove unused notification id
This commit is contained in:
ekzyis 2025-07-09 19:50:12 +02:00 committed by GitHub
parent 17aada6dbc
commit bfced699ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 6 additions and 8 deletions

View File

@ -2,7 +2,7 @@ import { decodeCursor, LIMIT, nextNoteCursorEncoded } from '@/lib/cursor'
import { getItem, filterClause, whereClause, muteClause, activeOrMine } from './item' import { getItem, filterClause, whereClause, muteClause, activeOrMine } from './item'
import { getInvoice, getWithdrawl } from './wallet' import { getInvoice, getWithdrawl } from './wallet'
import { pushSubscriptionSchema, validateSchema } from '@/lib/validate' import { pushSubscriptionSchema, validateSchema } from '@/lib/validate'
import { replyToSubscription } from '@/lib/webPush' import { sendPushSubscriptionReply } from '@/lib/webPush'
import { getSub } from './sub' import { getSub } from './sub'
import { GqlAuthenticationError, GqlInputError } from '@/lib/error' import { GqlAuthenticationError, GqlInputError } from '@/lib/error'
import { WALLET_MAX_RETRIES, WALLET_RETRY_BEFORE_MS } from '@/lib/constants' import { WALLET_MAX_RETRIES, WALLET_RETRY_BEFORE_MS } from '@/lib/constants'
@ -439,7 +439,7 @@ export default {
console.log(`[webPush] created subscription for user ${me.id}: endpoint=${endpoint}`) console.log(`[webPush] created subscription for user ${me.id}: endpoint=${endpoint}`)
} }
await replyToSubscription(dbPushSubscription.id, { title: 'Stacker News notifications are now active' }) await sendPushSubscriptionReply(dbPushSubscription)
return dbPushSubscription return dbPushSubscription
}, },

View File

@ -116,10 +116,9 @@ async function sendUserNotification (userId, notification) {
} }
} }
export async function replyToSubscription (subscriptionId, notification) { export async function sendPushSubscriptionReply (subscription) {
try { try {
const payload = createPayload(notification) const payload = createPayload({ title: 'Stacker News notifications are now active' })
const subscription = await models.pushSubscription.findUnique({ where: { id: subscriptionId } })
await sendNotification(subscription, payload) await sendNotification(subscription, payload)
} catch (err) { } catch (err) {
console.log('[webPush] error sending subscription reply: ', err) console.log('[webPush] error sending subscription reply: ', err)

View File

@ -14,7 +14,6 @@ export function onPush (sw) {
let payload = event.data?.json() let payload = event.data?.json()
if (!payload) return // ignore push events without payload, like isTrusted events if (!payload) return // ignore push events without payload, like isTrusted events
const { tag } = payload.options const { tag } = payload.options
const nid = crypto.randomUUID() // notification id for tracking
// iOS requirement: group all promises // iOS requirement: group all promises
const promises = [] const promises = []
@ -26,7 +25,7 @@ export function onPush (sw) {
// Check if there are already notifications with the same tag and merge them // Check if there are already notifications with the same tag and merge them
promises.push(sw.registration.getNotifications({ tag }).then((notifications) => { promises.push(sw.registration.getNotifications({ tag }).then((notifications) => {
if (notifications.length) { if (notifications.length) {
payload = mergeNotification(event, sw, payload, notifications, tag, nid) payload = mergeNotification(event, sw, payload, notifications, tag)
} }
})) }))
} }
@ -44,7 +43,7 @@ const immediatelyShowNotification = (tag) =>
!tag || ['TIP', 'FORWARDEDTIP', 'EARN', 'STREAK', 'TERRITORY_TRANSFER'].includes(tag.split('-')[0]) !tag || ['TIP', 'FORWARDEDTIP', 'EARN', 'STREAK', 'TERRITORY_TRANSFER'].includes(tag.split('-')[0])
// merge notifications with the same tag // merge notifications with the same tag
const mergeNotification = (event, sw, payload, currentNotifications, tag, nid) => { const mergeNotification = (event, sw, payload, currentNotifications, tag) => {
// sanity check // sanity check
const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag) const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag)
if (otherTagNotifications.length > 0) { if (otherTagNotifications.length > 0) {