Browse Source

ForceUnsubscribe from WebPush on logout

master
Igor Zhukov 7 years ago
parent
commit
0a9a0c4c9d
  1. 8
      app/js/lib/mtproto_wrapper.js
  2. 24
      app/js/lib/ng_utils.js

8
app/js/lib/mtproto_wrapper.js

@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
.factory('MtpApiManager', function (Storage, MtpAuthorizer, MtpNetworkerFactory, MtpSingleInstanceService, AppRuntimeManager, ErrorService, qSync, $rootScope, $q, TelegramMeWebService) {
.factory('MtpApiManager', function (Storage, MtpAuthorizer, MtpNetworkerFactory, MtpSingleInstanceService, AppRuntimeManager, ErrorService, qSync, $rootScope, $q, WebPushApiManager, TelegramMeWebService) {
var cachedNetworkers = {}
var cachedUploadNetworkers = {}
var cachedExportPromise = {}
@ -47,11 +47,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) @@ -47,11 +47,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
for (var dcID = 1; dcID <= 5; dcID++) {
storageKeys.push('dc' + dcID + '_auth_key')
}
WebPushApiManager.forceUnsubscribe()
return Storage.get(storageKeys).then(function (storageResult) {
var logoutPromises = []
for (var i = 0; i < storageResult.length; i++) {
if (storageResult[i]) {
logoutPromises.push(mtpInvokeApi('auth.logOut', {}, {dcID: i + 1}))
logoutPromises.push(mtpInvokeApi('auth.logOut', {}, {dcID: i + 1, ignoreErrors: true}))
}
}
return $q.all(logoutPromises).then(function () {
@ -156,6 +157,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) @@ -156,6 +157,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
error = {message: error}
}
deferred.reject(error)
if (options.ignoreErrors) {
return
}
if (error.code == 406) {
error.handled = true

24
app/js/lib/ng_utils.js

@ -2083,6 +2083,29 @@ angular.module('izhukov.utils', []) @@ -2083,6 +2083,29 @@ angular.module('izhukov.utils', [])
})
}
function forceUnsubscribe() {
if (!isAvailable) {
return
}
navigator.serviceWorker.ready.then(function(reg) {
reg.pushManager.getSubscription().then(function (subscription) {
console.warn('force unsubscribe', subscription)
if (subscription) {
subscription.unsubscribe().then(function(successful) {
console.warn('force unsubscribe successful', successful)
isPushEnabled = false
}).catch(function(e) {
console.error('Unsubscription error: ', e)
})
}
}).catch(function(e) {
console.error('Error thrown while unsubscribing from ' +
'push messaging.', e)
})
})
}
function isAliveNotify() {
if (!isAvailable ||
$rootScope.idle && $rootScope.idle.deactivated) {
@ -2176,6 +2199,7 @@ angular.module('izhukov.utils', []) @@ -2176,6 +2199,7 @@ angular.module('izhukov.utils', [])
isPushEnabled: isPushEnabled,
subscribe: subscribe,
unsubscribe: unsubscribe,
forceUnsubscribe: forceUnsubscribe,
hidePushNotifications: hidePushNotifications,
setLocalNotificationsDisabled: setLocalNotificationsDisabled,
setSettings: setSettings

Loading…
Cancel
Save