diff options
| author | Henry Jameson <me@hjkos.com> | 2018-12-14 17:17:58 +0300 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2018-12-14 17:17:58 +0300 |
| commit | d7973b0b802cdeea42c8ca292574e52b4050525e (patch) | |
| tree | d443d01edb20682d408d57855ee4088c96826dea /src/services/push/push.js | |
| parent | 9dfff107177e4461734c54f60d79448dec34c002 (diff) | |
| parent | 5b6c1aa97c0d13101ebddb4c34b79a2e428ec30b (diff) | |
Merge remote-tracking branch 'upstream/develop' into async_follow
* upstream/develop: (45 commits)
fix chrome
Prevent html-minifier to remove placeholder comment in index.html template
Add placeholder to insert server generated metatags. Related to #430
added condition to check for logined user
fix gradients and minor artifacts
keep track of new instance options
fix old MR
oof
get rid of slots
fix timeago font
added hide_network option, fixed properties naming
Fix fetching new users, add storing local users in usersObjects with their screen_name as well as id, so that they could be fetched zero-state with screen-name link.
improve notification subscription
Refactor arrays to individual options
Reset enableFollowsExport to true after 2 sec when an export file is available to download
Write a unit test for fileSizeFormatService
add checkbox to disable web push
I am dumb
Handle errors from server
Moved upload errors in user_settings to an array. Moved upload error strings to its separate section in i18n
...
Diffstat (limited to 'src/services/push/push.js')
| -rw-r--r-- | src/services/push/push.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/services/push/push.js b/src/services/push/push.js new file mode 100644 index 00000000..1ac304d1 --- /dev/null +++ b/src/services/push/push.js @@ -0,0 +1,69 @@ +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 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) => subscribe(registration, isEnabled, vapidPublicKey)) + .then((subscription) => sendSubscriptionToBackEnd(subscription, token)) + .catch((e) => console.warn(`Failed to setup Web Push Notifications: ${e.message}`)) + } +} |
