Skip to content

Commit bfced69

Browse files
authored
Rename to sendPushSubscriptionReply & remove unused nid argument (#2273)
* Refactor push subscription reply * Remove unused notification id
1 parent 17aada6 commit bfced69

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

api/resolvers/notifications.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { decodeCursor, LIMIT, nextNoteCursorEncoded } from '@/lib/cursor'
22
import { getItem, filterClause, whereClause, muteClause, activeOrMine } from './item'
33
import { getInvoice, getWithdrawl } from './wallet'
44
import { pushSubscriptionSchema, validateSchema } from '@/lib/validate'
5-
import { replyToSubscription } from '@/lib/webPush'
5+
import { sendPushSubscriptionReply } from '@/lib/webPush'
66
import { getSub } from './sub'
77
import { GqlAuthenticationError, GqlInputError } from '@/lib/error'
88
import { WALLET_MAX_RETRIES, WALLET_RETRY_BEFORE_MS } from '@/lib/constants'
@@ -439,7 +439,7 @@ export default {
439439
console.log(`[webPush] created subscription for user ${me.id}: endpoint=${endpoint}`)
440440
}
441441

442-
await replyToSubscription(dbPushSubscription.id, { title: 'Stacker News notifications are now active' })
442+
await sendPushSubscriptionReply(dbPushSubscription)
443443

444444
return dbPushSubscription
445445
},

lib/webPush.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,9 @@ async function sendUserNotification (userId, notification) {
116116
}
117117
}
118118

119-
export async function replyToSubscription (subscriptionId, notification) {
119+
export async function sendPushSubscriptionReply (subscription) {
120120
try {
121-
const payload = createPayload(notification)
122-
const subscription = await models.pushSubscription.findUnique({ where: { id: subscriptionId } })
121+
const payload = createPayload({ title: 'Stacker News notifications are now active' })
123122
await sendNotification(subscription, payload)
124123
} catch (err) {
125124
console.log('[webPush] error sending subscription reply: ', err)

sw/eventListener.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function onPush (sw) {
1414
let payload = event.data?.json()
1515
if (!payload) return // ignore push events without payload, like isTrusted events
1616
const { tag } = payload.options
17-
const nid = crypto.randomUUID() // notification id for tracking
1817

1918
// iOS requirement: group all promises
2019
const promises = []
@@ -26,7 +25,7 @@ export function onPush (sw) {
2625
// Check if there are already notifications with the same tag and merge them
2726
promises.push(sw.registration.getNotifications({ tag }).then((notifications) => {
2827
if (notifications.length) {
29-
payload = mergeNotification(event, sw, payload, notifications, tag, nid)
28+
payload = mergeNotification(event, sw, payload, notifications, tag)
3029
}
3130
}))
3231
}
@@ -44,7 +43,7 @@ const immediatelyShowNotification = (tag) =>
4443
!tag || ['TIP', 'FORWARDEDTIP', 'EARN', 'STREAK', 'TERRITORY_TRANSFER'].includes(tag.split('-')[0])
4544

4645
// merge notifications with the same tag
47-
const mergeNotification = (event, sw, payload, currentNotifications, tag, nid) => {
46+
const mergeNotification = (event, sw, payload, currentNotifications, tag) => {
4847
// sanity check
4948
const otherTagNotifications = currentNotifications.filter(({ tag: nTag }) => nTag !== tag)
5049
if (otherTagNotifications.length > 0) {

0 commit comments

Comments
 (0)