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 @@
angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto']) 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 cachedNetworkers = {}
var cachedUploadNetworkers = {} var cachedUploadNetworkers = {}
var cachedExportPromise = {} var cachedExportPromise = {}
@ -47,11 +47,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
for (var dcID = 1; dcID <= 5; dcID++) { for (var dcID = 1; dcID <= 5; dcID++) {
storageKeys.push('dc' + dcID + '_auth_key') storageKeys.push('dc' + dcID + '_auth_key')
} }
WebPushApiManager.forceUnsubscribe()
return Storage.get(storageKeys).then(function (storageResult) { return Storage.get(storageKeys).then(function (storageResult) {
var logoutPromises = [] var logoutPromises = []
for (var i = 0; i < storageResult.length; i++) { for (var i = 0; i < storageResult.length; i++) {
if (storageResult[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 () { return $q.all(logoutPromises).then(function () {
@ -156,6 +157,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
error = {message: error} error = {message: error}
} }
deferred.reject(error) deferred.reject(error)
if (options.ignoreErrors) {
return
}
if (error.code == 406) { if (error.code == 406) {
error.handled = true error.handled = true

24
app/js/lib/ng_utils.js

@ -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() { function isAliveNotify() {
if (!isAvailable || if (!isAvailable ||
$rootScope.idle && $rootScope.idle.deactivated) { $rootScope.idle && $rootScope.idle.deactivated) {
@ -2176,6 +2199,7 @@ angular.module('izhukov.utils', [])
isPushEnabled: isPushEnabled, isPushEnabled: isPushEnabled,
subscribe: subscribe, subscribe: subscribe,
unsubscribe: unsubscribe, unsubscribe: unsubscribe,
forceUnsubscribe: forceUnsubscribe,
hidePushNotifications: hidePushNotifications, hidePushNotifications: hidePushNotifications,
setLocalNotificationsDisabled: setLocalNotificationsDisabled, setLocalNotificationsDisabled: setLocalNotificationsDisabled,
setSettings: setSettings setSettings: setSettings

Loading…
Cancel
Save