aboutsummaryrefslogtreecommitdiff
path: root/src/main.js
diff options
context:
space:
mode:
authorEgor Kislitsyn <egor@kislitsyn.com>2018-12-13 18:04:09 +0700
committerEgor Kislitsyn <egor@kislitsyn.com>2018-12-13 18:04:09 +0700
commitb3455649c53034e01725977260e69cff59c47e87 (patch)
tree88e7209fedb48941f1b2763f36ff0b7362e4efa6 /src/main.js
parent02c0e15781fa0a499c736e710755e799bfaec77d (diff)
improve notification subscription
Diffstat (limited to 'src/main.js')
-rw-r--r--src/main.js35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/main.js b/src/main.js
index e4621482..23ea854b 100644
--- a/src/main.js
+++ b/src/main.js
@@ -50,6 +50,32 @@ const persistedStateOptions = {
'oauth'
]
}
+
+const registerPushNotifications = store => {
+ store.subscribe((mutation, state) => {
+ const vapidPublicKey = state.instance.vapidPublicKey
+ const permission = state.interface.notificationPermission === 'granted'
+ const isUserMutation = mutation.type === 'setCurrentUser'
+
+ if (isUserMutation && vapidPublicKey && permission) {
+ return store.dispatch('registerPushNotifications')
+ }
+
+ const user = state.users.currentUser
+ const isVapidMutation = mutation.type === 'setInstanceOption' && mutation.payload.name === 'vapidPublicKey'
+
+ if (isVapidMutation && user && permission) {
+ return store.dispatch('registerPushNotifications')
+ }
+
+ const isPermMutation = mutation.type === 'setNotificationPermission' && mutation.payload === 'granted'
+
+ if (isPermMutation && user && vapidPublicKey) {
+ return store.dispatch('registerPushNotifications')
+ }
+ })
+}
+
createPersistedState(persistedStateOptions).then((persistedState) => {
const store = new Vuex.Store({
modules: {
@@ -62,17 +88,10 @@ createPersistedState(persistedStateOptions).then((persistedState) => {
chat: chatModule,
oauth: oauthModule
},
- plugins: [persistedState],
+ plugins: [persistedState, registerPushNotifications],
strict: false // Socket modifies itself, let's ignore this for now.
// strict: process.env.NODE_ENV !== 'production'
})
- store.subscribe((mutation, state) => {
- if ((mutation.type === 'setCurrentUser' && state.instance.vapidPublicKey) || // Login + existing key
- (mutation.type === 'setInstanceOption' && mutation.payload.name === 'vapidPublicKey' && state.users.currentUser)) { // Logged in, key arrives late
- store.dispatch('registerPushNotifications')
- }
- })
-
afterStoreSetup({ store, i18n })
})