From 957b2a6f7ef8ca381fc44b79a17639e400407b67 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 20 Dec 2018 09:17:59 +0300 Subject: simplified some code, made it possible to unregister serviceworker altogether --- src/modules/users.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/modules/users.js') 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')) -- cgit v1.2.3-70-g09d2 From a4f09029260100f6d5baea67ac333593c5c4432c Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 25 Dec 2018 03:46:19 +0300 Subject: small refactor, added push unsub notice for BE --- src/modules/users.js | 4 ++- src/services/push/push.js | 77 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 25 deletions(-) (limited to 'src/modules/users.js') diff --git a/src/modules/users.js b/src/modules/users.js index 2ea0919e..f2b59aaa 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -117,7 +117,9 @@ const users = { registerPushNotifications(isEnabled, vapidPublicKey, token) }, unregisterPushNotifications (store) { - unregisterPushNotifications() + const token = store.state.currentUser.credentials + + unregisterPushNotifications(token) }, addNewStatuses (store, { statuses }) { const users = map(statuses, 'user') diff --git a/src/services/push/push.js b/src/services/push/push.js index ff67fd5a..9142895f 100644 --- a/src/services/push/push.js +++ b/src/services/push/push.js @@ -14,18 +14,12 @@ function isPushSupported () { return 'serviceWorker' in navigator && 'PushManager' in window } -function registerServiceWorker () { +function getOrCreateServiceWorker () { return runtime.register() - .catch((err) => console.error('Unable to register service worker.', err)) + .catch((err) => console.error('Unable to get or create a 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) { +function subscribePush (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')) @@ -36,6 +30,30 @@ function subscribe (registration, isEnabled, vapidPublicKey) { return registration.pushManager.subscribe(subscribeOptions) } +function unsubscribePush (registration) { + return registration.pushManager.getSubscription() + .then((subscribtion) => { + if (subscribtion === null) { return } + return subscribtion.unsubscribe() + }) +} + +function deleteSubscriptionFromBackEnd (token) { + return window.fetch('/api/v1/push/subscription/', { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}` + } + }).then((response) => { + if (!response.ok) throw new Error('Bad status code from server.') + return response.json() + }).then((responseData) => { + if (!responseData.id) throw new Error('Bad response from server.') + return responseData + }) +} + function sendSubscriptionToBackEnd (subscription, token) { return window.fetch('/api/v1/push/subscription/', { method: 'POST', @@ -54,31 +72,42 @@ function sendSubscriptionToBackEnd (subscription, token) { } } }) + }).then((response) => { + if (!response.ok) throw new Error('Bad status code from server.') + return response.json() + }).then((responseData) => { + if (!responseData.id) throw new Error('Bad response from server.') + return responseData }) - .then((response) => { - if (!response.ok) throw new Error('Bad status code from server.') - return response.json() - }) - .then((responseData) => { - if (!responseData.id) throw new Error('Bad response from server.') - return responseData - }) } export function registerPushNotifications (isEnabled, vapidPublicKey, token) { if (isPushSupported()) { - registerServiceWorker() - .then((registration) => subscribe(registration, isEnabled, vapidPublicKey)) + getOrCreateServiceWorker() + .then((registration) => subscribePush(registration, isEnabled, vapidPublicKey)) .then((subscription) => sendSubscriptionToBackEnd(subscription, token)) .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`)) } } -export function unregisterPushNotifications (isEnabled, vapidPublicKey, token) { +export function unregisterPushNotifications (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}`)) + Promise.all([ + deleteSubscriptionFromBackEnd(token), + getOrCreateServiceWorker() + .then((registration) => { + return unsubscribePush(registration).then((result) => [registration, result]) + }) + .then(([registration, unsubResult]) => { + if (!unsubResult) { + console.warn('Push subscription cancellation wasn\'t successful, killing SW anyway...') + } + return registration.unregister().then((result) => { + if (!result) { + console.warn('Failed to kill SW') + } + }) + }) + ]).catch((e) => console.warn(`Failed to disable Web Push Notifications: ${e.message}`)) } } -- cgit v1.2.3-70-g09d2 From c5847349e0afb14702d1746fac142b8aeb2020e7 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 25 Dec 2018 20:43:18 +0700 Subject: improve web push notifications --- src/lib/push_notifications_plugin.js | 22 ++++++++++++++++++++++ src/main.js | 25 ++----------------------- src/modules/users.js | 3 ++- src/services/push/push.js | 14 +++++++------- 4 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 src/lib/push_notifications_plugin.js (limited to 'src/modules/users.js') diff --git a/src/lib/push_notifications_plugin.js b/src/lib/push_notifications_plugin.js new file mode 100644 index 00000000..f75bb823 --- /dev/null +++ b/src/lib/push_notifications_plugin.js @@ -0,0 +1,22 @@ +export default (store) => { + store.subscribe((mutation, state) => { + const vapidPublicKey = state.instance.vapidPublicKey + const webPushNotification = state.config.webPushNotifications + const permission = state.interface.notificationPermission === 'granted' + const user = state.users.currentUser + + const isUserMutation = mutation.type === 'setCurrentUser' + 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 (isUserMutation || isVapidMutation || isPermMutation || isUserConfigMutation || isVisibilityMutation) { + if (user && vapidPublicKey && permission && webPushNotification) { + return store.dispatch('registerPushNotifications') + } else if (isUserConfigMutation && !webPushNotification) { + return store.dispatch('unregisterPushNotifications') + } + } + }) +} diff --git a/src/main.js b/src/main.js index c22a762e..f87ef9da 100644 --- a/src/main.js +++ b/src/main.js @@ -15,6 +15,7 @@ import VueTimeago from 'vue-timeago' import VueI18n from 'vue-i18n' import createPersistedState from './lib/persisted_state.js' +import pushNotifications from './lib/push_notifications_plugin.js' import messages from './i18n/messages.js' @@ -51,28 +52,6 @@ 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 user = state.users.currentUser - - const isUserMutation = mutation.type === 'setCurrentUser' - 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' - - if (isUserMutation || isVapidMutation || isPermMutation || isUserConfigMutation) { - if (user && vapidPublicKey && permission && webPushNotification) { - return store.dispatch('registerPushNotifications') - } else if (isUserConfigMutation && !webPushNotification) { - return store.dispatch('unregisterPushNotifications') - } - } - }) -} - createPersistedState(persistedStateOptions).then((persistedState) => { const store = new Vuex.Store({ modules: { @@ -85,7 +64,7 @@ createPersistedState(persistedStateOptions).then((persistedState) => { chat: chatModule, oauth: oauthModule }, - plugins: [persistedState, registerPushNotifications], + plugins: [persistedState, pushNotifications], strict: false // Socket modifies itself, let's ignore this for now. // strict: process.env.NODE_ENV !== 'production' }) diff --git a/src/modules/users.js b/src/modules/users.js index f2b59aaa..cff16c47 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -113,8 +113,9 @@ const users = { const token = store.state.currentUser.credentials const vapidPublicKey = store.rootState.instance.vapidPublicKey const isEnabled = store.rootState.config.webPushNotifications + const notificationVisibility = store.rootState.config.notificationVisibility - registerPushNotifications(isEnabled, vapidPublicKey, token) + registerPushNotifications(isEnabled, vapidPublicKey, token, notificationVisibility) }, unregisterPushNotifications (store) { const token = store.state.currentUser.credentials diff --git a/src/services/push/push.js b/src/services/push/push.js index bf0c9680..1b189a29 100644 --- a/src/services/push/push.js +++ b/src/services/push/push.js @@ -51,7 +51,7 @@ function deleteSubscriptionFromBackEnd (token) { }) } -function sendSubscriptionToBackEnd (subscription, token) { +function sendSubscriptionToBackEnd (subscription, token, notificationVisibility) { return window.fetch('/api/v1/push/subscription/', { method: 'POST', headers: { @@ -62,10 +62,10 @@ function sendSubscriptionToBackEnd (subscription, token) { subscription, data: { alerts: { - follow: true, - favourite: true, - mention: true, - reblog: true + follow: notificationVisibility.follows, + favourite: notificationVisibility.likes, + mention: notificationVisibility.mentions, + reblog: notificationVisibility.repeats } } }) @@ -78,11 +78,11 @@ function sendSubscriptionToBackEnd (subscription, token) { }) } -export function registerPushNotifications (isEnabled, vapidPublicKey, token) { +export function registerPushNotifications (isEnabled, vapidPublicKey, token, notificationVisibility) { if (isPushSupported()) { getOrCreateServiceWorker() .then((registration) => subscribePush(registration, isEnabled, vapidPublicKey)) - .then((subscription) => sendSubscriptionToBackEnd(subscription, token)) + .then((subscription) => sendSubscriptionToBackEnd(subscription, token, notificationVisibility)) .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`)) } } -- cgit v1.2.3-70-g09d2 From fa8ecb1c39d2900de69fef0a609ef31ce0ef9161 Mon Sep 17 00:00:00 2001 From: HJ Date: Wed, 26 Dec 2018 09:19:25 +0000 Subject: User Card Content fixes and updates --- .../user_card_content/user_card_content.vue | 145 +++++++++++++-------- src/modules/statuses.js | 2 + src/modules/users.js | 18 +++ 3 files changed, 109 insertions(+), 56 deletions(-) (limited to 'src/modules/users.js') diff --git a/src/components/user_card_content/user_card_content.vue b/src/components/user_card_content/user_card_content.vue index c5222519..c76c41a9 100644 --- a/src/components/user_card_content/user_card_content.vue +++ b/src/components/user_card_content/user_card_content.vue @@ -2,22 +2,25 @@