aboutsummaryrefslogtreecommitdiff
path: root/src/sw.js
diff options
context:
space:
mode:
authorHJ <spam@hjkos.com>2018-12-13 13:46:57 +0000
committerHJ <spam@hjkos.com>2018-12-13 13:46:57 +0000
commit8e4777ccc6bf72b56a0905ca491c8e0e97fb73cf (patch)
tree53e98662ef34b8bccc845f627c125528c1c1436c /src/sw.js
parente443716bcd616ad61efae161624dd970841a935c (diff)
parenta8521fc8d99ee7ee5142e2c7c642eee0fc14ed93 (diff)
Merge branch 'feature/push-subscriptions' into 'develop'
add service worker and push notifications See merge request pleroma/pleroma-fe!404
Diffstat (limited to 'src/sw.js')
-rw-r--r--src/sw.js38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/sw.js b/src/sw.js
new file mode 100644
index 00000000..6cecb3f3
--- /dev/null
+++ b/src/sw.js
@@ -0,0 +1,38 @@
+/* eslint-env serviceworker */
+
+import localForage from 'localforage'
+
+function isEnabled () {
+ return localForage.getItem('vuex-lz')
+ .then(data => data.config.webPushNotifications)
+}
+
+function getWindowClients () {
+ return clients.matchAll({ includeUncontrolled: true })
+ .then((clientList) => clientList.filter(({ type }) => type === 'window'))
+}
+
+self.addEventListener('push', (event) => {
+ if (event.data) {
+ event.waitUntil(isEnabled().then((isEnabled) => {
+ return isEnabled && getWindowClients().then((list) => {
+ const data = event.data.json()
+
+ if (list.length === 0) return self.registration.showNotification(data.title, data)
+ })
+ }))
+ }
+})
+
+self.addEventListener('notificationclick', (event) => {
+ event.notification.close()
+
+ event.waitUntil(getWindowClients().then((list) => {
+ for (var i = 0; i < list.length; i++) {
+ var client = list[i]
+ if (client.url === '/' && 'focus' in client) { return client.focus() }
+ }
+
+ if (clients.openWindow) return clients.openWindow('/')
+ }))
+})