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 { useInstanceStore } from 'src/stores/instance.js'
|
||||||
import { useInterfaceStore } from 'src/stores/interface.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) => {
|
store.subscribe((mutation, state) => {
|
||||||
|
// Initial state
|
||||||
const vapidPublicKey = useInstanceStore().vapidPublicKey
|
const vapidPublicKey = useInstanceStore().vapidPublicKey
|
||||||
const webPushNotification = state.config.webPushNotifications
|
const enabled = useSyncConfigStore().mergedConfig.webPushNotifications
|
||||||
const permission = useInterfaceStore().notificationPermission === 'granted'
|
const permissionGranted = useInterfaceStore().notificationPermission === 'granted'
|
||||||
|
const permissionPresent = useInterfaceStore().notificationPermission !== undefined
|
||||||
const user = state.users.currentUser
|
const user = state.users.currentUser
|
||||||
|
|
||||||
const isUserMutation = mutation.type === 'setCurrentUser'
|
if (!permissionPresent || !vapidPublicKey) return
|
||||||
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 (
|
if (mutation.type === 'setCurrentUser' || mutation.type === 'clearCurrentUser') {
|
||||||
isUserMutation ||
|
console.log(!!user, permissionGranted, enabled)
|
||||||
isVapidMutation ||
|
if (user && permissionGranted && enabled) {
|
||||||
isPermMutation ||
|
console.log('vuexReg')
|
||||||
isUserConfigMutation ||
|
|
||||||
isVisibilityMutation
|
|
||||||
) {
|
|
||||||
if (user && vapidPublicKey && permission && webPushNotification) {
|
|
||||||
return store.dispatch('registerPushNotifications')
|
return store.dispatch('registerPushNotifications')
|
||||||
} else if (isUserConfigMutation && !webPushNotification) {
|
} else {
|
||||||
|
console.log('vuexUnreg')
|
||||||
return store.dispatch('unregisterPushNotifications')
|
return store.dispatch('unregisterPushNotifications')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue