From ee70ec4c7efb49c08f0a76b6b2694c0e9910978c Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Mon, 10 Dec 2018 22:36:25 +0700 Subject: [PATCH] fix race condition --- src/boot/after_store.js | 5 ++++- src/main.js | 11 +++++++--- src/modules/pushNotifications.js | 36 -------------------------------- src/modules/users.js | 13 ++++++++---- 4 files changed, 21 insertions(+), 44 deletions(-) delete mode 100644 src/modules/pushNotifications.js diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 0c121fe2..0d1cabd5 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -27,7 +27,10 @@ const afterStoreSetup = ({ store, i18n }) => { store.dispatch('setInstanceOption', { name: 'registrationOpen', value: (registrationClosed === '0') }) store.dispatch('setInstanceOption', { name: 'textlimit', value: parseInt(textlimit) }) store.dispatch('setInstanceOption', { name: 'server', value: server }) - store.dispatch('setInstanceOption', { name: 'vapidPublicKey', value: vapidPublicKey }) + + if (vapidPublicKey) { + store.dispatch('setInstanceOption', { name: 'vapidPublicKey', value: vapidPublicKey }) + } var apiConfig = data.site.pleromafe diff --git a/src/main.js b/src/main.js index 91592191..e4621482 100644 --- a/src/main.js +++ b/src/main.js @@ -10,7 +10,6 @@ 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 pushNotificationsModule from './modules/pushNotifications.js' import VueTimeago from 'vue-timeago' import VueI18n from 'vue-i18n' @@ -61,13 +60,19 @@ createPersistedState(persistedStateOptions).then((persistedState) => { api: apiModule, config: configModule, chat: chatModule, - oauth: oauthModule, - pushNotifications: pushNotificationsModule + oauth: oauthModule }, plugins: [persistedState], 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 }) }) diff --git a/src/modules/pushNotifications.js b/src/modules/pushNotifications.js deleted file mode 100644 index ea92f811..00000000 --- a/src/modules/pushNotifications.js +++ /dev/null @@ -1,36 +0,0 @@ -import registerPushNotifications from '../services/push/push.js' - -const subscribe = { - state: { - token: null, - vapidPublicKey: null - }, - mutations: { - setApiToken (state, user) { - state.token = user.credentials - }, - setVapidPublicKey (state, vapidPublicKey) { - state.vapidPublicKey = vapidPublicKey - } - }, - actions: { - setInstanceOption (store, { name, value }) { - if (name === 'vapidPublicKey') { - store.commit('setVapidPublicKey', value) - - if (store.state.token) { - registerPushNotifications(store.rootState.config.webPushNotifications, value, store.state.token) - } - } - }, - setCurrentUser (store, user) { - store.commit('setApiToken', user.credentials) - - if (store.state.vapidPublicKey) { - registerPushNotifications(store.rootState.config.webPushNotifications, store.state.vapidPublicKey, user.credentials) - } - } - } -} - -export default subscribe diff --git a/src/modules/users.js b/src/modules/users.js index 1256e9df..5e0c087d 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,6 +1,7 @@ import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' import { compact, map, each, merge } from 'lodash' import { set } from 'vue' +import registerPushNotifications from '../services/push/push.js' // TODO: Unify with mergeOrAdd in statuses.js export const mergeOrAdd = (arr, obj, item) => { @@ -65,6 +66,13 @@ const users = { store.rootState.api.backendInteractor.fetchUser({id}) .then((user) => store.commit('addNewUsers', user)) }, + registerPushNotifications (store) { + const token = store.state.currentUser.credentials + const vapidPublicKey = store.rootState.instance.vapidPublicKey + const isEnabled = store.rootState.config.webPushNotifications + + registerPushNotifications(isEnabled, vapidPublicKey, token) + }, addNewStatuses (store, { statuses }) { const users = map(statuses, 'user') const retweetedUsers = compact(map(statuses, 'retweeted_status.user')) @@ -86,9 +94,6 @@ const users = { store.dispatch('stopFetching', 'friends') store.commit('setBackendInteractor', backendInteractorService()) }, - setCurrentUser (store, user) { - store.commit('setCurrentUser', user) - }, loginUser (store, accessToken) { return new Promise((resolve, reject) => { const commit = store.commit @@ -100,7 +105,7 @@ const users = { .then((user) => { // user.credentials = userCredentials user.credentials = accessToken - store.dispatch('setCurrentUser', user) + commit('setCurrentUser', user) commit('addNewUsers', [user]) // Set our new backend interactor