aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHenry Jameson <me@hjkos.com>2018-12-20 09:17:59 +0300
committerHenry Jameson <me@hjkos.com>2018-12-20 09:17:59 +0300
commit957b2a6f7ef8ca381fc44b79a17639e400407b67 (patch)
treea0eb359a268b243f34b33babf72ba4f5fc317645 /src
parentd0b47488fb69b2b8449fa73e8d94e2cdcc9adda8 (diff)
simplified some code, made it possible to unregister serviceworker altogether
Diffstat (limited to 'src')
-rw-r--r--src/main.js25
-rw-r--r--src/modules/users.js5
-rw-r--r--src/services/push/push.js17
3 files changed, 31 insertions, 16 deletions
diff --git a/src/main.js b/src/main.js
index bf92e78e..c22a762e 100644
--- a/src/main.js
+++ b/src/main.js
@@ -54,24 +54,21 @@ const persistedStateOptions = {
const registerPushNotifications = store => {
store.subscribe((mutation, state) => {
const vapidPublicKey = state.instance.vapidPublicKey
+ const webPushNotification = state.config.webPushNotifications
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 isUserMutation = mutation.type === 'setCurrentUser'
+ const isVapidMutation = mutation.type === 'setInstanceOption' && mutation.payload.name === 'vapidPublicKey'
const isPermMutation = mutation.type === 'setNotificationPermission' && mutation.payload === 'granted'
-
- if (isPermMutation && user && vapidPublicKey) {
- return store.dispatch('registerPushNotifications')
+ const isUserConfigMutation = mutation.type === 'setOption' && mutation.payload.name === 'webPushNotifications'
+
+ if (isUserMutation || isVapidMutation || isPermMutation || isUserConfigMutation) {
+ if (user && vapidPublicKey && permission && webPushNotification) {
+ return store.dispatch('registerPushNotifications')
+ } else if (isUserConfigMutation && !webPushNotification) {
+ return store.dispatch('unregisterPushNotifications')
+ }
}
})
}
diff --git a/src/modules/users.js b/src/modules/users.js
index 13d3f26e..2ea0919e 100644
--- a/src/modules/users.js
+++ b/src/modules/users.js
@@ -1,7 +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'
+import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js'
import oauthApi from '../services/new_api/oauth'
import { humanizeErrors } from './errors'
@@ -116,6 +116,9 @@ const users = {
registerPushNotifications(isEnabled, vapidPublicKey, token)
},
+ unregisterPushNotifications (store) {
+ unregisterPushNotifications()
+ },
addNewStatuses (store, { statuses }) {
const users = map(statuses, 'user')
const retweetedUsers = compact(map(statuses, 'retweeted_status.user'))
diff --git a/src/services/push/push.js b/src/services/push/push.js
index 1ac304d1..ff67fd5a 100644
--- a/src/services/push/push.js
+++ b/src/services/push/push.js
@@ -19,6 +19,12 @@ function registerServiceWorker () {
.catch((err) => console.error('Unable to register service worker.', err))
}
+function unregisterServiceWorker () {
+ return runtime.register()
+ .then((registration) => registration.unregister())
+ .catch((err) => console.error('Unable to unregister serviceworker', err))
+}
+
function subscribe (registration, isEnabled, vapidPublicKey) {
if (!isEnabled) return Promise.reject(new Error('Web Push is disabled in config'))
if (!vapidPublicKey) return Promise.reject(new Error('VAPID public key is not found'))
@@ -59,7 +65,7 @@ function sendSubscriptionToBackEnd (subscription, token) {
})
}
-export default function registerPushNotifications (isEnabled, vapidPublicKey, token) {
+export function registerPushNotifications (isEnabled, vapidPublicKey, token) {
if (isPushSupported()) {
registerServiceWorker()
.then((registration) => subscribe(registration, isEnabled, vapidPublicKey))
@@ -67,3 +73,12 @@ export default function registerPushNotifications (isEnabled, vapidPublicKey, to
.catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
}
}
+
+export function unregisterPushNotifications (isEnabled, vapidPublicKey, token) {
+ if (isPushSupported()) {
+ unregisterServiceWorker()
+ .then((registration) => subscribe(registration, isEnabled, vapidPublicKey))
+ .then((subscription) => sendSubscriptionToBackEnd(subscription, token))
+ .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
+ }
+}