From 07f1b8523eddd596d154e68561a389d6b9742d95 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 7 Dec 2018 18:13:04 +0700 Subject: [PATCH] add subscribe module and fix race condition --- src/main.js | 4 +++- src/modules/subscribe.js | 21 +++++++++++++++++++++ src/modules/users.js | 5 +---- src/services/push/push.js | 8 ++++---- 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/modules/subscribe.js diff --git a/src/main.js b/src/main.js index 378fe95c..4285e70b 100644 --- a/src/main.js +++ b/src/main.js @@ -10,6 +10,7 @@ import apiModule from './modules/api.js' import configModule from './modules/config.js' import chatModule from './modules/chat.js' import oauthModule from './modules/oauth.js' +import subscribeModule from './modules/subscribe.js' import VueTimeago from 'vue-timeago' import VueI18n from 'vue-i18n' @@ -60,7 +61,8 @@ createPersistedState(persistedStateOptions).then((persistedState) => { api: apiModule, config: configModule, chat: chatModule, - oauth: oauthModule + oauth: oauthModule, + subscribe: subscribeModule }, plugins: [persistedState], strict: false // Socket modifies itself, let's ignore this for now. diff --git a/src/modules/subscribe.js b/src/modules/subscribe.js new file mode 100644 index 00000000..e705904c --- /dev/null +++ b/src/modules/subscribe.js @@ -0,0 +1,21 @@ +import registerPushNotifications from '../services/push/push.js' + +const subscribe = { + state: { + token: null, + vapidPublicKey: null + }, + mutations: { + setCurrentUser (state, user) { + state.token = user.credentials + if (state.token && state.vapidPublicKey) registerPushNotifications(this) + }, + setInstanceOption (state, { name, value }) { + if (name !== 'vapidPublicKey') return + state.vapidPublicKey = value + if (state.token && state.vapidPublicKey) registerPushNotifications(this) + } + } +} + +export default subscribe diff --git a/src/modules/users.js b/src/modules/users.js index d2c7fdf6..88ec7115 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,5 +1,4 @@ import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' -import registerPushNotifications from '../services/push/push.js' import { compact, map, each, merge } from 'lodash' import { set } from 'vue' @@ -120,10 +119,8 @@ const users = { }) // Fetch our friends - store.rootState.api.backendInteractor.fetchFriends({id: user.id}) + store.rootState.api.backendInteractor.fetchFriends({ id: user.id }) .then((friends) => commit('addNewUsers', friends)) - - registerPushNotifications(store) }) } else { // Authentication failed diff --git a/src/services/push/push.js b/src/services/push/push.js index ffab5918..923c2178 100644 --- a/src/services/push/push.js +++ b/src/services/push/push.js @@ -45,17 +45,17 @@ function askPermission () { } function subscribe (registration, store) { - if (!store.rootState.config.webPushNotifications) { + if (!store.state.config.webPushNotifications) { return Promise.reject(new Error('Web Push is disabled in config')) } - if (!store.rootState.instance.vapidPublicKey) { + if (!store.state.subscribe.vapidPublicKey) { return Promise.reject(new Error('VAPID public key is not found')) } const subscribeOptions = { userVisibleOnly: true, - applicationServerKey: urlBase64ToUint8Array(store.rootState.instance.vapidPublicKey) + applicationServerKey: urlBase64ToUint8Array(store.state.subscribe.vapidPublicKey) } return registration.pushManager.subscribe(subscribeOptions) } @@ -65,7 +65,7 @@ function sendSubscriptionToBackEnd (subscription, store) { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${store.rootState.oauth.token}` + 'Authorization': `Bearer ${store.state.subscribe.token}` }, body: JSON.stringify({ subscription,