attempt another fix for #411
This commit is contained in:
parent
e20adaebdc
commit
9698679d38
@ -1033,19 +1033,23 @@ export const createMentions = async (item, models) => {
|
|||||||
userId: user.id
|
userId: user.id
|
||||||
}
|
}
|
||||||
|
|
||||||
await models.mention.upsert({
|
const mention = await models.mention.upsert({
|
||||||
where: {
|
where: {
|
||||||
itemId_userId: data
|
itemId_userId: data
|
||||||
},
|
},
|
||||||
update: data,
|
update: data,
|
||||||
create: data
|
create: data
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// only send if mention is new to avoid duplicates
|
||||||
|
if (mention.createdAt.getTime() === mention.updatedAt.getTime()) {
|
||||||
sendUserNotification(user.id, {
|
sendUserNotification(user.id, {
|
||||||
title: 'you were mentioned',
|
title: 'you were mentioned',
|
||||||
body: item.text,
|
body: item.text,
|
||||||
item,
|
item,
|
||||||
tag: 'MENTION'
|
tag: 'MENTION'
|
||||||
}).catch(console.error)
|
}).catch(console.error)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -10,9 +10,6 @@ const storage = new ServiceWorkerStorage('sw:storage', 1)
|
|||||||
let messageChannelPort
|
let messageChannelPort
|
||||||
let actionChannelPort
|
let actionChannelPort
|
||||||
|
|
||||||
// keep track of item ids where we received a MENTION notification already to not show one again
|
|
||||||
const itemMentions = []
|
|
||||||
|
|
||||||
// current push notification count for badge purposes
|
// current push notification count for badge purposes
|
||||||
let activeCount = 0
|
let activeCount = 0
|
||||||
|
|
||||||
@ -22,10 +19,9 @@ export function onPush (sw) {
|
|||||||
if (!payload) return
|
if (!payload) return
|
||||||
const { tag } = payload.options
|
const { tag } = payload.options
|
||||||
event.waitUntil((async () => {
|
event.waitUntil((async () => {
|
||||||
if (skipNotification(payload)) return
|
|
||||||
if (immediatelyShowNotification(payload)) {
|
if (immediatelyShowNotification(payload)) {
|
||||||
setAppBadge(sw, ++activeCount)
|
setAppBadge(sw, ++activeCount)
|
||||||
return sw.registration.showNotification(payload.title, payload.options)
|
return await sw.registration.showNotification(payload.title, payload.options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch existing notifications with same tag
|
// fetch existing notifications with same tag
|
||||||
@ -39,30 +35,23 @@ export function onPush (sw) {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// save item id of MENTION notification so we can skip following ones
|
|
||||||
if (tag === 'MENTION' && payload.options.data?.itemId) itemMentions.push(payload.options.data.itemId)
|
|
||||||
|
|
||||||
if (notifications.length === 0) {
|
if (notifications.length === 0) {
|
||||||
// incoming notification is first notification with this tag
|
// incoming notification is first notification with this tag
|
||||||
setAppBadge(sw, ++activeCount)
|
setAppBadge(sw, ++activeCount)
|
||||||
return sw.registration.showNotification(payload.title, payload.options)
|
return await sw.registration.showNotification(payload.title, payload.options)
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentNotification = notifications[0]
|
const currentNotification = notifications[0]
|
||||||
return mergeAndShowNotification(sw, payload, currentNotification)
|
return await mergeAndShowNotification(sw, payload, currentNotification)
|
||||||
})())
|
})())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const skipNotification = ({ options: { tag, data } }) => {
|
|
||||||
return tag === 'MENTION' && itemMentions.includes(data.itemId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if there is no tag or it's a TIP, FORWARDEDTIP or EARN notification
|
// if there is no tag or it's a TIP, FORWARDEDTIP or EARN notification
|
||||||
// we don't need to merge notifications and thus the notification should be immediately shown using `showNotification`
|
// we don't need to merge notifications and thus the notification should be immediately shown using `showNotification`
|
||||||
const immediatelyShowNotification = ({ options: { tag } }) => !tag || ['TIP', 'FORWARDEDTIP', 'EARN'].includes(tag.split('-')[0])
|
const immediatelyShowNotification = ({ options: { tag } }) => !tag || ['TIP', 'FORWARDEDTIP', 'EARN'].includes(tag.split('-')[0])
|
||||||
|
|
||||||
const mergeAndShowNotification = (sw, payload, currentNotification) => {
|
const mergeAndShowNotification = async (sw, payload, currentNotification) => {
|
||||||
const { data: incomingData } = payload.options
|
const { data: incomingData } = payload.options
|
||||||
const { tag, data: currentData } = currentNotification
|
const { tag, data: currentData } = currentNotification
|
||||||
|
|
||||||
@ -95,7 +84,7 @@ const mergeAndShowNotification = (sw, payload, currentNotification) => {
|
|||||||
// close current notification before showing new one to "merge" notifications
|
// close current notification before showing new one to "merge" notifications
|
||||||
currentNotification.close()
|
currentNotification.close()
|
||||||
const newNotificationOptions = { icon: currentNotification.icon, tag, data: { url: '/notifications', amount, ...newData } }
|
const newNotificationOptions = { icon: currentNotification.icon, tag, data: { url: '/notifications', amount, ...newData } }
|
||||||
return sw.registration.showNotification(title, newNotificationOptions)
|
return await sw.registration.showNotification(title, newNotificationOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onNotificationClick (sw) {
|
export function onNotificationClick (sw) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user