Reduce unwarranted differences with upstream
Ports part of0758b00bfdand897199910f
This commit is contained in:
		
							parent
							
								
									2f61b65b1e
								
							
						
					
					
						commit
						7b922c2d90
					
				|  | @ -1,5 +1,7 @@ | ||||||
| import api from '../../api'; | import api from '../../api'; | ||||||
|  | import { me } from '../../initial_state'; | ||||||
| import { pushNotificationsSetting } from '../../settings'; | import { pushNotificationsSetting } from '../../settings'; | ||||||
|  | import { decode as decodeBase64 } from '../../utils/base64'; | ||||||
| 
 | 
 | ||||||
| import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; | import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; | ||||||
| 
 | 
 | ||||||
|  | @ -10,13 +12,7 @@ const urlBase64ToUint8Array = (base64String) => { | ||||||
|     .replace(/-/g, '+') |     .replace(/-/g, '+') | ||||||
|     .replace(/_/g, '/'); |     .replace(/_/g, '/'); | ||||||
| 
 | 
 | ||||||
|   const rawData = window.atob(base64); |   return decodeBase64(base64); | ||||||
|   const outputArray = new Uint8Array(rawData.length); |  | ||||||
| 
 |  | ||||||
|   for (let i = 0; i < rawData.length; ++i) { |  | ||||||
|     outputArray[i] = rawData.charCodeAt(i); |  | ||||||
|   } |  | ||||||
|   return outputArray; |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const getApplicationServerKey = () => document.querySelector('[name="applicationServerKey"]').getAttribute('content'); | const getApplicationServerKey = () => document.querySelector('[name="applicationServerKey"]').getAttribute('content'); | ||||||
|  | @ -36,7 +32,7 @@ const subscribe = (registration) => | ||||||
| const unsubscribe = ({ registration, subscription }) => | const unsubscribe = ({ registration, subscription }) => | ||||||
|   subscription ? subscription.unsubscribe().then(() => registration) : registration; |   subscription ? subscription.unsubscribe().then(() => registration) : registration; | ||||||
| 
 | 
 | ||||||
| const sendSubscriptionToBackend = (getState, subscription, me) => { | const sendSubscriptionToBackend = (subscription) => { | ||||||
|   const params = { subscription }; |   const params = { subscription }; | ||||||
| 
 | 
 | ||||||
|   if (me) { |   if (me) { | ||||||
|  | @ -46,7 +42,7 @@ const sendSubscriptionToBackend = (getState, subscription, me) => { | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   return api(getState).post('/api/web/push_subscriptions', params).then(response => response.data); |   return api().post('/api/web/push_subscriptions', params).then(response => response.data); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| // Last one checks for payload support: https://web-push-book.gauntface.com/chapter-06/01-non-standards-browsers/#no-payload
 | // Last one checks for payload support: https://web-push-book.gauntface.com/chapter-06/01-non-standards-browsers/#no-payload
 | ||||||
|  | @ -55,7 +51,6 @@ const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' | ||||||
| export function register () { | export function register () { | ||||||
|   return (dispatch, getState) => { |   return (dispatch, getState) => { | ||||||
|     dispatch(setBrowserSupport(supportsPushNotifications)); |     dispatch(setBrowserSupport(supportsPushNotifications)); | ||||||
|     const me = getState().getIn(['meta', 'me']); |  | ||||||
| 
 | 
 | ||||||
|     if (supportsPushNotifications) { |     if (supportsPushNotifications) { | ||||||
|       if (!getApplicationServerKey()) { |       if (!getApplicationServerKey()) { | ||||||
|  | @ -79,13 +74,13 @@ export function register () { | ||||||
|             } else { |             } else { | ||||||
|               // Something went wrong, try to subscribe again
 |               // Something went wrong, try to subscribe again
 | ||||||
|               return unsubscribe({ registration, subscription }).then(subscribe).then( |               return unsubscribe({ registration, subscription }).then(subscribe).then( | ||||||
|                 subscription => sendSubscriptionToBackend(getState, subscription, me)); |                 subscription => sendSubscriptionToBackend(subscription)); | ||||||
|             } |             } | ||||||
|           } |           } | ||||||
| 
 | 
 | ||||||
|           // No subscription, try to subscribe
 |           // No subscription, try to subscribe
 | ||||||
|           return subscribe(registration).then( |           return subscribe(registration).then( | ||||||
|             subscription => sendSubscriptionToBackend(getState, subscription, me)); |             subscription => sendSubscriptionToBackend(subscription)); | ||||||
|         }) |         }) | ||||||
|         .then(subscription => { |         .then(subscription => { | ||||||
|           // If we got a PushSubscription (and not a subscription object from the backend)
 |           // If we got a PushSubscription (and not a subscription object from the backend)
 | ||||||
|  | @ -128,10 +123,9 @@ export function saveSettings() { | ||||||
|     const alerts = state.get('alerts'); |     const alerts = state.get('alerts'); | ||||||
|     const data = { alerts }; |     const data = { alerts }; | ||||||
| 
 | 
 | ||||||
|     api(getState).put(`/api/web/push_subscriptions/${subscription.get('id')}`, { |     api().put(`/api/web/push_subscriptions/${subscription.get('id')}`, { | ||||||
|       data, |       data, | ||||||
|     }).then(() => { |     }).then(() => { | ||||||
|       const me = getState().getIn(['meta', 'me']); |  | ||||||
|       if (me) { |       if (me) { | ||||||
|         pushNotificationsSetting.set(me, data); |         pushNotificationsSetting.set(me, data); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|  | @ -26,7 +26,7 @@ const debouncedSave = debounce((dispatch, getState) => { | ||||||
| 
 | 
 | ||||||
|   const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS(); |   const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS(); | ||||||
| 
 | 
 | ||||||
|   api(getState).put('/api/web/settings', { data }) |   api().put('/api/web/settings', { data }) | ||||||
|     .then(() => dispatch({ type: SETTING_SAVE })) |     .then(() => dispatch({ type: SETTING_SAVE })) | ||||||
|     .catch(error => dispatch(showAlertForError(error))); |     .catch(error => dispatch(showAlertForError(error))); | ||||||
| }, 5000, { trailing: true }); | }, 5000, { trailing: true }); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue