rewrote push notifications plugin
This commit is contained in:
parent
90ac30249b
commit
a91821933f
1 changed files with 79 additions and 25 deletions
|
|
@ -1,37 +1,91 @@
|
|||
import { useInstanceStore } from 'src/stores/instance.js'
|
||||
import { useInterfaceStore } from 'src/stores/interface.js'
|
||||
import { useSyncConfigStore } from 'src/stores/sync_config.js'
|
||||
|
||||
export default (store) => {
|
||||
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
|
||||
let enabled = useSyncConfigStore().mergedConfig.webPushNotifications
|
||||
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) {
|
||||
console.log('piniaReg')
|
||||
return window.vuex.dispatch('registerPushNotifications')
|
||||
} else {
|
||||
console.log('piniaUnreg')
|
||||
return window.vuex.dispatch('unregisterPushNotifications')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const vuexPushNotificationsPlugin = (store) => {
|
||||
store.subscribe((mutation, state) => {
|
||||
// Initial state
|
||||
const vapidPublicKey = useInstanceStore().vapidPublicKey
|
||||
const webPushNotification = state.config.webPushNotifications
|
||||
const permission = useInterfaceStore().notificationPermission === 'granted'
|
||||
const enabled = useSyncConfigStore().mergedConfig.webPushNotifications
|
||||
const permissionGranted = useInterfaceStore().notificationPermission === 'granted'
|
||||
const permissionPresent = useInterfaceStore().notificationPermission !== undefined
|
||||
const user = state.users.currentUser
|
||||
|
||||
const isUserMutation = mutation.type === 'setCurrentUser'
|
||||
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'
|
||||
if (!permissionPresent || !vapidPublicKey) return
|
||||
|
||||
if (
|
||||
isUserMutation ||
|
||||
isVapidMutation ||
|
||||
isPermMutation ||
|
||||
isUserConfigMutation ||
|
||||
isVisibilityMutation
|
||||
) {
|
||||
if (user && vapidPublicKey && permission && webPushNotification) {
|
||||
if (mutation.type === 'setCurrentUser' || mutation.type === 'clearCurrentUser') {
|
||||
console.log(!!user, permissionGranted, enabled)
|
||||
if (user && permissionGranted && enabled) {
|
||||
console.log('vuexReg')
|
||||
return store.dispatch('registerPushNotifications')
|
||||
} else if (isUserConfigMutation && !webPushNotification) {
|
||||
} else {
|
||||
console.log('vuexUnreg')
|
||||
return store.dispatch('unregisterPushNotifications')
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue