2025-02-03 13:02:14 +02:00
|
|
|
import { useInterfaceStore } from 'src/stores/interface'
|
2023-04-05 21:06:37 -06:00
|
|
|
|
2018-12-25 20:43:18 +07:00
|
|
|
export default (store) => {
|
|
|
|
|
store.subscribe((mutation, state) => {
|
|
|
|
|
const vapidPublicKey = state.instance.vapidPublicKey
|
|
|
|
|
const webPushNotification = state.config.webPushNotifications
|
2023-04-05 21:06:37 -06:00
|
|
|
const permission = useInterfaceStore().notificationPermission === 'granted'
|
2018-12-25 20:43:18 +07:00
|
|
|
const user = state.users.currentUser
|
|
|
|
|
|
|
|
|
|
const isUserMutation = mutation.type === 'setCurrentUser'
|
2026-01-06 16:22:52 +02:00
|
|
|
const isVapidMutation =
|
|
|
|
|
mutation.type === 'setInstanceOption' &&
|
|
|
|
|
mutation.payload.name === 'vapidPublicKey'
|
|
|
|
|
const isPermMutation =
|
|
|
|
|
mutation.type === 'setNotificationPermission' &&
|
|
|
|
|
mutation.payload === 'granted'
|
|
|
|
|
const isUserConfigMutation =
|
|
|
|
|
mutation.type === 'setOption' &&
|
|
|
|
|
mutation.payload.name === 'webPushNotifications'
|
|
|
|
|
const isVisibilityMutation =
|
|
|
|
|
mutation.type === 'setOption' &&
|
|
|
|
|
mutation.payload.name === 'notificationVisibility'
|
2018-12-25 20:43:18 +07:00
|
|
|
|
2026-01-06 16:22:52 +02:00
|
|
|
if (
|
|
|
|
|
isUserMutation ||
|
|
|
|
|
isVapidMutation ||
|
|
|
|
|
isPermMutation ||
|
|
|
|
|
isUserConfigMutation ||
|
|
|
|
|
isVisibilityMutation
|
|
|
|
|
) {
|
2018-12-25 20:43:18 +07:00
|
|
|
if (user && vapidPublicKey && permission && webPushNotification) {
|
|
|
|
|
return store.dispatch('registerPushNotifications')
|
|
|
|
|
} else if (isUserConfigMutation && !webPushNotification) {
|
|
|
|
|
return store.dispatch('unregisterPushNotifications')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|