Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 0a9a0c4

Browse files
committed
ForceUnsubscribe from WebPush on logout
1 parent a86c249 commit 0a9a0c4

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

app/js/lib/mtproto_wrapper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
99

10-
.factory('MtpApiManager', function (Storage, MtpAuthorizer, MtpNetworkerFactory, MtpSingleInstanceService, AppRuntimeManager, ErrorService, qSync, $rootScope, $q, TelegramMeWebService) {
10+
.factory('MtpApiManager', function (Storage, MtpAuthorizer, MtpNetworkerFactory, MtpSingleInstanceService, AppRuntimeManager, ErrorService, qSync, $rootScope, $q, WebPushApiManager, TelegramMeWebService) {
1111
var cachedNetworkers = {}
1212
var cachedUploadNetworkers = {}
1313
var cachedExportPromise = {}
@@ -47,11 +47,12 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
4747
for (var dcID = 1; dcID <= 5; dcID++) {
4848
storageKeys.push('dc' + dcID + '_auth_key')
4949
}
50+
WebPushApiManager.forceUnsubscribe()
5051
return Storage.get(storageKeys).then(function (storageResult) {
5152
var logoutPromises = []
5253
for (var i = 0; i < storageResult.length; i++) {
5354
if (storageResult[i]) {
54-
logoutPromises.push(mtpInvokeApi('auth.logOut', {}, {dcID: i + 1}))
55+
logoutPromises.push(mtpInvokeApi('auth.logOut', {}, {dcID: i + 1, ignoreErrors: true}))
5556
}
5657
}
5758
return $q.all(logoutPromises).then(function () {
@@ -156,6 +157,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
156157
error = {message: error}
157158
}
158159
deferred.reject(error)
160+
if (options.ignoreErrors) {
161+
return
162+
}
159163

160164
if (error.code == 406) {
161165
error.handled = true

app/js/lib/ng_utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,29 @@ angular.module('izhukov.utils', [])
20832083
})
20842084
}
20852085

2086+
function forceUnsubscribe() {
2087+
if (!isAvailable) {
2088+
return
2089+
}
2090+
navigator.serviceWorker.ready.then(function(reg) {
2091+
reg.pushManager.getSubscription().then(function (subscription) {
2092+
console.warn('force unsubscribe', subscription)
2093+
if (subscription) {
2094+
subscription.unsubscribe().then(function(successful) {
2095+
console.warn('force unsubscribe successful', successful)
2096+
isPushEnabled = false
2097+
}).catch(function(e) {
2098+
console.error('Unsubscription error: ', e)
2099+
})
2100+
}
2101+
2102+
}).catch(function(e) {
2103+
console.error('Error thrown while unsubscribing from ' +
2104+
'push messaging.', e)
2105+
})
2106+
})
2107+
}
2108+
20862109
function isAliveNotify() {
20872110
if (!isAvailable ||
20882111
$rootScope.idle && $rootScope.idle.deactivated) {
@@ -2176,6 +2199,7 @@ angular.module('izhukov.utils', [])
21762199
isPushEnabled: isPushEnabled,
21772200
subscribe: subscribe,
21782201
unsubscribe: unsubscribe,
2202+
forceUnsubscribe: forceUnsubscribe,
21792203
hidePushNotifications: hidePushNotifications,
21802204
setLocalNotificationsDisabled: setLocalNotificationsDisabled,
21812205
setSettings: setSettings

0 commit comments

Comments
 (0)