2026-01-29 00:49:26 +02:00
|
|
|
import { useInstanceStore } from 'src/stores/instance.js'
|
2026-01-29 20:44:55 +02:00
|
|
|
import { useInterfaceStore } from 'src/stores/interface.js'
|
2026-03-24 21:42:22 +02:00
|
|
|
import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
2023-04-05 21:06:37 -06:00
|
|
|
|
2026-02-13 14:11:33 +02:00
|
|
|
export const piniaPushNotificationsPlugin = ({ store }) => {
|
|
|
|
|
if (
|
|
|
|
|
store.$id !== 'sync_config' &&
|
|
|
|
|
store.$id !== 'instance' &&
|
|
|
|
|
store.$id !== 'interface'
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
store.$onAction(({ name: actionName, args }) => {
|
|
|
|
|
if (
|
|
|
|
|
store.$id === 'interface' &&
|
|
|
|
|
actionName !== 'setNotificationPermission' &&
|
|
|
|
|
actionName !== 'setLoginStatus'
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
// Initial state
|
|
|
|
|
let vapidPublicKey = useInstanceStore().vapidPublicKey
|
2026-03-24 21:42:22 +02:00
|
|
|
let enabled = useMergedConfigStore().mergedConfig.webPushNotifications
|
2026-02-13 14:11:33 +02:00
|
|
|
let permissionGranted =
|
|
|
|
|
useInterfaceStore().notificationPermission === 'granted'
|
|
|
|
|
let permissionPresent =
|
|
|
|
|
useInterfaceStore().notificationPermission !== undefined
|
|
|
|
|
let user = !!window.vuex.state.users.currentUser
|
|
|
|
|
|
|
|
|
|
if (store.$id === 'instance') {
|
|
|
|
|
if (actionName === 'set' && args[0].path === 'vapidPublicKey') {
|
|
|
|
|
const { value } = args[0]
|
|
|
|
|
vapidPublicKey = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!vapidPublicKey || !permissionPresent) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (store.$id === 'interface') {
|
|
|
|
|
if (actionName === 'setNotificationPermission') {
|
|
|
|
|
permissionGranted = args[0] === 'granted'
|
|
|
|
|
} else if (actionName === 'setLoginStatus') {
|
|
|
|
|
user = args[0]
|
|
|
|
|
} else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else if (store.$id === 'sync_config') {
|
|
|
|
|
if (
|
|
|
|
|
actionName === 'setPreference' &&
|
|
|
|
|
args[0].path === 'simple.webPushNotifications'
|
|
|
|
|
) {
|
|
|
|
|
const { value } = args[0]
|
|
|
|
|
enabled = value
|
|
|
|
|
} else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (permissionGranted && enabled && user) {
|
|
|
|
|
return window.vuex.dispatch('registerPushNotifications')
|
|
|
|
|
} else {
|
|
|
|
|
return window.vuex.dispatch('unregisterPushNotifications')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const vuexPushNotificationsPlugin = (store) => {
|
2018-12-25 20:43:18 +07:00
|
|
|
store.subscribe((mutation, state) => {
|
2026-02-13 14:11:33 +02:00
|
|
|
// Initial state
|
2026-01-29 00:49:26 +02:00
|
|
|
const vapidPublicKey = useInstanceStore().vapidPublicKey
|
2026-03-24 21:42:22 +02:00
|
|
|
const enabled = useMergedConfigStore().mergedConfig.webPushNotifications
|
2026-02-13 14:11:33 +02:00
|
|
|
const permissionGranted =
|
|
|
|
|
useInterfaceStore().notificationPermission === 'granted'
|
|
|
|
|
const permissionPresent =
|
|
|
|
|
useInterfaceStore().notificationPermission !== undefined
|
2018-12-25 20:43:18 +07:00
|
|
|
const user = state.users.currentUser
|
|
|
|
|
|
2026-02-13 14:11:33 +02:00
|
|
|
if (!permissionPresent || !vapidPublicKey) return
|
2018-12-25 20:43:18 +07:00
|
|
|
|
2026-01-06 16:22:52 +02:00
|
|
|
if (
|
2026-02-13 14:11:33 +02:00
|
|
|
mutation.type === 'setCurrentUser' ||
|
|
|
|
|
mutation.type === 'clearCurrentUser'
|
2026-01-06 16:22:52 +02:00
|
|
|
) {
|
2026-02-13 14:11:33 +02:00
|
|
|
if (user && permissionGranted && enabled) {
|
2018-12-25 20:43:18 +07:00
|
|
|
return store.dispatch('registerPushNotifications')
|
2026-02-13 14:11:33 +02:00
|
|
|
} else {
|
2018-12-25 20:43:18 +07:00
|
|
|
return store.dispatch('unregisterPushNotifications')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|