From bfced699ea4317b380f9c5531b8ba5457f4ab19e Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 9 Jul 2025 19:50:12 +0200 Subject: [PATCH] Rename to sendPushSubscriptionReply & remove unused nid argument (#2273) * Refactor push subscription reply * Remove unused notification id --- api/resolvers/notifications.js | 4 ++-- lib/webPush.js | 5 ++--- sw/eventListener.js | 5 ++--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index f1558d2d..af9d9ca6 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -2,7 +2,7 @@ import { decodeCursor, LIMIT, nextNoteCursorEncoded } from '@/lib/cursor' import { getItem, filterClause, whereClause, muteClause, activeOrMine } from './item' import { getInvoice, getWithdrawl } from './wallet' import { pushSubscriptionSchema, validateSchema } from '@/lib/validate' -import { replyToSubscription } from '@/lib/webPush' +import { sendPushSubscriptionReply } from '@/lib/webPush' import { getSub } from './sub' import { GqlAuthenticationError, GqlInputError } from '@/lib/error' 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}`) } - await replyToSubscription(dbPushSubscription.id, { title: 'Stacker News notifications are now active' }) + await sendPushSubscriptionReply(dbPushSubscription) return dbPushSubscription }, diff --git a/lib/webPush.js b/lib/webPush.js index c697afd5..c1e33d33 100644 --- a/lib/webPush.js +++ b/lib/webPush.js @@ -116,10 +116,9 @@ async function sendUserNotification (userId, notification) { } } -export async function replyToSubscription (subscriptionId, notification) { +export async function sendPushSubscriptionReply (subscription) { try { - const payload = createPayload(notification) - const subscription = await models.pushSubscription.findUnique({ where: { id: subscriptionId } }) + const payload = createPayload({ title: 'Stacker News notifications are now active' }) await sendNotification(subscription, payload) } catch (err) { console.log('[webPush] error sending subscription reply: ', err) diff --git a/sw/eventListener.js b/sw/eventListener.js index 272edc44..67fc275b 100644 --- a/sw/eventListener.js +++ b/sw/eventListener.js @@ -14,7 +14,6 @@ export function onPush (sw) { let payload = event.data?.json() if (!payload) return // ignore push events without payload, like isTrusted events const { tag } = payload.options - const nid = crypto.randomUUID() // notification id for tracking // iOS requirement: group all promises const promises = [] @@ -26,7 +25,7 @@ export function onPush (sw) { // Check if there are already notifications with the same tag and merge them promises.push(sw.registration.getNotifications({ tag }).then((notifications) => { 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]) // merge notifications with the same tag -const mergeNotification = (event, sw, payload, currentNotifications, tag, nid) => { +const mergeNotification = (event, sw, payload, currentNotifications, tag) => { // sanity check const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag) if (otherTagNotifications.length > 0) {