aboutsummaryrefslogtreecommitdiff
path: root/src/services/push/push.js
blob: 58017ed71ab240e220181166f325fc4015ee4bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import runtime from 'serviceworker-webpack-plugin/lib/runtime'

function urlBase64ToUint8Array (base64String) {
  const padding = '='.repeat((4 - base64String.length % 4) % 4)
  const base64 = (base64String + padding)
    .replace(/-/g, '+')
    .replace(/_/g, '/')

  const rawData = window.atob(base64)
  return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)))
}

function isPushSupported () {
  return 'serviceWorker' in navigator && 'PushManager' in window
}

function registerServiceWorker () {
  return runtime.register()
    .catch((err) => console.error('Unable to register service worker.', err))
}

function askPermission () {
  return new Promise((resolve, reject) => {
    const Notification = window.Notification

    if (!Notification) return reject(new Error('Notifications disabled'))
    if (Notification.permission !== 'default') return resolve(Notification.permission)

    const permissionResult = Notification.requestPermission(resolve)

    if (permissionResult) permissionResult.then(resolve, reject)
  }).then((permissionResult) => {
    if (permissionResult !== 'granted') throw new Error('We weren\'t granted permission.')
    return permissionResult
  })
}

function subscribe (registration, isEnabled, vapidPublicKey) {
  if (!isEnabled) return Promise.reject(new Error('Web Push is disabled in config'))
  if (!vapidPublicKey) return Promise.reject(new Error('VAPID public key is not found'))

  const subscribeOptions = {
    userVisibleOnly: true,
    applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
  }
  return registration.pushManager.subscribe(subscribeOptions)
}

function sendSubscriptionToBackEnd (subscription, token) {
  return window.fetch('/api/v1/push/subscription/', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({
      subscription,
      data: {
        alerts: {
          follow: true,
          favourite: true,
          mention: true,
          reblog: true
        }
      }
    })
  })
    .then((response) => {
      if (!response.ok) throw new Error('Bad status code from server.')
      return response.json()
    })
    .then((responseData) => {
      if (!responseData.id) throw new Error('Bad response from server.')
      return responseData
    })
}

export default function registerPushNotifications (isEnabled, vapidPublicKey, token) {
  if (isPushSupported()) {
    registerServiceWorker()
      .then((registration) => {
        return askPermission()
          .then(() => subscribe(registration, isEnabled, vapidPublicKey))
          .then((subscription) => sendSubscriptionToBackEnd(subscription, token))
          .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`))
      })
  }
}