Improved service worker code
This commit is contained in:
parent
8cf24cdf7e
commit
e9770d2746
@ -2088,10 +2088,11 @@ angular.module('izhukov.utils', [])
|
|||||||
$rootScope.idle && $rootScope.idle.deactivated) {
|
$rootScope.idle && $rootScope.idle.deactivated) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
settings.baseUrl = (location.href || '').replace(/#.*$/, '') + '#/im'
|
||||||
|
|
||||||
var eventData = {
|
var eventData = {
|
||||||
type: 'ping',
|
type: 'ping',
|
||||||
localNotifications: localNotificationsAvailable,
|
localNotifications: localNotificationsAvailable,
|
||||||
baseUrl: (location.href || '').replace(/#.*$/, '') + '#/im',
|
|
||||||
lang: {
|
lang: {
|
||||||
push_action_mute1d: _(Config.Mobile
|
push_action_mute1d: _(Config.Mobile
|
||||||
? 'push_action_mute1d_mobile_raw'
|
? 'push_action_mute1d_mobile_raw'
|
||||||
@ -2112,7 +2113,7 @@ angular.module('izhukov.utils', [])
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setSettings(newSettings) {
|
function setSettings(newSettings) {
|
||||||
settings = newSettings
|
settings = angular.copy(newSettings)
|
||||||
clearTimeout(isAliveTO)
|
clearTimeout(isAliveTO)
|
||||||
isAliveNotify()
|
isAliveNotify()
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,21 @@
|
|||||||
console.log('[SW] Push worker started')
|
console.log('[SW] Push worker started')
|
||||||
|
|
||||||
|
|
||||||
var port
|
|
||||||
var lastAliveTime = false
|
|
||||||
var pendingNotification = false
|
|
||||||
var muteUntil = false
|
|
||||||
var baseUrl
|
|
||||||
var settings
|
|
||||||
var lang = {}
|
|
||||||
var userInvisibleSupported = false
|
|
||||||
|
|
||||||
|
|
||||||
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
|
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1
|
||||||
|
var userInvisibleSupported = isFirefox ? true : false
|
||||||
|
|
||||||
if (isFirefox) {
|
var pendingNotification = false
|
||||||
userInvisibleSupported = true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
var defaultBaseUrl
|
||||||
switch (location.hostname) {
|
switch (location.hostname) {
|
||||||
case 'localhost':
|
case 'localhost':
|
||||||
baseUrl = 'http://localhost:8000/app/index.html#/im'
|
defaultBaseUrl = 'http://localhost:8000/app/index.html#/im'
|
||||||
break
|
break
|
||||||
case 'zhukov.github.io':
|
case 'zhukov.github.io':
|
||||||
baseUrl = 'https://zhukov.github.io/webogram/#/im'
|
defaultBaseUrl = 'https://zhukov.github.io/webogram/#/im'
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
case 'web.telegram.org':
|
case 'web.telegram.org':
|
||||||
baseUrl = 'https://' + location.hostname + '/#/im'
|
defaultBaseUrl = 'https://' + location.hostname + '/#/im'
|
||||||
}
|
}
|
||||||
|
|
||||||
self.addEventListener('push', function(event) {
|
self.addEventListener('push', function(event) {
|
||||||
@ -39,27 +27,34 @@ self.addEventListener('push', function(event) {
|
|||||||
return reject()
|
return reject()
|
||||||
}
|
}
|
||||||
var nowTime = +(new Date())
|
var nowTime = +(new Date())
|
||||||
if (userInvisibleSupported &&
|
Promise.all([getMuteUntil(), getLastAliveTime()]).then(function (result) {
|
||||||
muteUntil &&
|
var muteUntil = result[0]
|
||||||
nowTime < muteUntil) {
|
var lastAliveTime = result[1]
|
||||||
console.log('Supress notification because mute for ', (muteUntil - nowTime) / 60000, 'min')
|
if (userInvisibleSupported &&
|
||||||
return reject()
|
muteUntil &&
|
||||||
}
|
nowTime < muteUntil) {
|
||||||
if (lastAliveTime &&
|
console.log('Supress notification because mute for ', Math.ceil((muteUntil - nowTime) / 60000), 'min')
|
||||||
nowTime - lastAliveTime < 60000) {
|
return reject()
|
||||||
return clients.matchAll({type: 'window'}).then(function(clientList) {
|
}
|
||||||
if (clientList.length) {
|
if (lastAliveTime &&
|
||||||
console.log('Supress notification because some instance is alive')
|
nowTime - lastAliveTime < 60000) {
|
||||||
return reject()
|
return clients.matchAll({type: 'window'}).then(function(clientList) {
|
||||||
}
|
console.log('matched clients', clientList)
|
||||||
return resolve()
|
if (clientList.length) {
|
||||||
})
|
console.log('Supress notification because some instance is alive')
|
||||||
}
|
return reject()
|
||||||
return resolve()
|
}
|
||||||
|
return resolve()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return resolve()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
var notificationPromise = checksPromise.then(function () {
|
var notificationPromise = checksPromise.then(function () {
|
||||||
fireNotification(obj)
|
return Promise.all([getSettings(), getLang()]).then(function (result) {
|
||||||
|
return fireNotification(obj, result[0], result[1])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
var closePromise = notificationPromise.catch(function () {
|
var closePromise = notificationPromise.catch(function () {
|
||||||
@ -75,11 +70,17 @@ self.addEventListener('push', function(event) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
if ('waitUntil' in event) {
|
event.waitUntil(closePromise)
|
||||||
event.waitUntil(closePromise)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
self.addEventListener('install', function(event) {
|
||||||
|
event.waitUntil(self.skipWaiting());
|
||||||
|
});
|
||||||
|
self.addEventListener('activate', function(event) {
|
||||||
|
console.log('[SW] on activate')
|
||||||
|
event.waitUntil(self.clients.claim());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
self.addEventListener('message', function(event) {
|
self.addEventListener('message', function(event) {
|
||||||
console.log('[SW] on message', event.data)
|
console.log('[SW] on message', event.data)
|
||||||
@ -87,6 +88,7 @@ self.addEventListener('message', function(event) {
|
|||||||
if (event.data.type == 'ping') {
|
if (event.data.type == 'ping') {
|
||||||
if (event.data.localNotifications) {
|
if (event.data.localNotifications) {
|
||||||
lastAliveTime = +(new Date())
|
lastAliveTime = +(new Date())
|
||||||
|
IDBManager.setItem('push_last_alive', lastAliveTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pendingNotification &&
|
if (pendingNotification &&
|
||||||
@ -108,12 +110,9 @@ self.addEventListener('message', function(event) {
|
|||||||
if (event.data.type == 'notifications_clear') {
|
if (event.data.type == 'notifications_clear') {
|
||||||
closeAllNotifications()
|
closeAllNotifications()
|
||||||
}
|
}
|
||||||
if (event.data.baseUrl) {
|
|
||||||
baseUrl = event.data.baseUrl
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
function fireNotification(obj) {
|
function fireNotification(obj, settings, lang) {
|
||||||
var title = obj.title || 'Telegram'
|
var title = obj.title || 'Telegram'
|
||||||
var body = obj.description || ''
|
var body = obj.description || ''
|
||||||
var icon = 'img/logo_share.png'
|
var icon = 'img/logo_share.png'
|
||||||
@ -221,7 +220,7 @@ self.addEventListener('notificationclick', function(event) {
|
|||||||
if (action == 'mute1d' && userInvisibleSupported) {
|
if (action == 'mute1d' && userInvisibleSupported) {
|
||||||
console.log('[SW] mute for 1d')
|
console.log('[SW] mute for 1d')
|
||||||
muteUntil = +(new Date()) + 86400000
|
muteUntil = +(new Date()) + 86400000
|
||||||
IDBManager.setItem('push_mute_until', muteUntil.toString())
|
IDBManager.setItem('push_mute_until', muteUntil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (!notification.data) {
|
if (!notification.data) {
|
||||||
@ -243,15 +242,15 @@ self.addEventListener('notificationclick', function(event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (clients.openWindow) {
|
if (clients.openWindow) {
|
||||||
return clients.openWindow(baseUrl)
|
return getSettings().then(function (settings) {
|
||||||
|
return clients.openWindow(settings.baseUrl || defaultBaseUrl)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
console.error('Clients.matchAll error', error)
|
console.error('Clients.matchAll error', error)
|
||||||
})
|
})
|
||||||
|
|
||||||
if ('waitUntil' in event) {
|
event.waitUntil(promise)
|
||||||
event.waitUntil(promise)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('notificationclose', onCloseNotification)
|
self.addEventListener('notificationclose', onCloseNotification)
|
||||||
@ -386,21 +385,52 @@ self.addEventListener('notificationclose', onCloseNotification)
|
|||||||
})()
|
})()
|
||||||
|
|
||||||
|
|
||||||
|
var lastAliveTime, muteUntil, settings, lang
|
||||||
|
|
||||||
IDBManager.getItem('push_mute_until').then(function (newMuteUntil) {
|
function getMuteUntil() {
|
||||||
muteUntil = Math.max(muteUntil || 0, newMuteUntil || 0) || false
|
if (muteUntil !== undefined) {
|
||||||
}).catch(function (error) {
|
return Promise.resolve(muteUntil)
|
||||||
console.error('IDB error', error)
|
}
|
||||||
})
|
return IDBManager.getItem('push_mute_until').then(function (newMuteUntil) {
|
||||||
|
return muteUntil = Math.max(muteUntil || 0, newMuteUntil || 0) || false
|
||||||
|
}).catch(function (error) {
|
||||||
|
console.error('IDB error', error)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
IDBManager.getItem('push_lang').then(function (newLang) {
|
function getLastAliveTime() {
|
||||||
lang = newLang || {}
|
if (lastAliveTime !== undefined) {
|
||||||
}).catch(function (error) {
|
return Promise.resolve(lastAliveTime)
|
||||||
console.error('IDB error', error)
|
}
|
||||||
})
|
return IDBManager.getItem('push_last_alive').then(function (newLastAliveTime) {
|
||||||
|
return lastAliveTime = Math.max(lastAliveTime || 0, newLastAliveTime || 0) || false
|
||||||
|
}).catch(function (error) {
|
||||||
|
console.error('IDB error', error)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
IDBManager.getItem('push_settings').then(function (newSettings) {
|
function getLang() {
|
||||||
settings = newSettings || {}
|
if (lang !== undefined) {
|
||||||
}).catch(function (error) {
|
return Promise.resolve(lang)
|
||||||
console.error('IDB error', error)
|
}
|
||||||
})
|
return IDBManager.getItem('push_lang').then(function (newLang) {
|
||||||
|
return lang = newLang || {}
|
||||||
|
}).catch(function (error) {
|
||||||
|
console.error('IDB error', error)
|
||||||
|
return {}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSettings() {
|
||||||
|
if (settings !== undefined) {
|
||||||
|
return Promise.resolve(settings)
|
||||||
|
}
|
||||||
|
return IDBManager.getItem('push_settings').then(function (newSettings) {
|
||||||
|
return settings = newSettings || {}
|
||||||
|
}).catch(function (error) {
|
||||||
|
console.error('IDB error', error)
|
||||||
|
return {}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user