|
|
@ -1,4 +1,4 @@ |
|
|
|
import axios from 'axios'; |
|
|
|
import api from 'flavours/glitch/util/api'; |
|
|
|
import { pushNotificationsSetting } from 'flavours/glitch/util/settings'; |
|
|
|
import { pushNotificationsSetting } from 'flavours/glitch/util/settings'; |
|
|
|
import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; |
|
|
|
import { setBrowserSupport, setSubscription, clearSubscription } from './setter'; |
|
|
|
|
|
|
|
|
|
|
@ -35,7 +35,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 = (subscription, me) => { |
|
|
|
const sendSubscriptionToBackend = (getState, subscription, me) => { |
|
|
|
const params = { subscription }; |
|
|
|
const params = { subscription }; |
|
|
|
|
|
|
|
|
|
|
|
if (me) { |
|
|
|
if (me) { |
|
|
@ -45,7 +45,7 @@ const sendSubscriptionToBackend = (subscription, me) => { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return axios.post('/api/web/push_subscriptions', params).then(response => response.data); |
|
|
|
return api(getState).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
|
|
|
@ -85,13 +85,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(subscription, me)); |
|
|
|
subscription => sendSubscriptionToBackend(getState, subscription, me)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// No subscription, try to subscribe
|
|
|
|
// No subscription, try to subscribe
|
|
|
|
return subscribe(registration).then( |
|
|
|
return subscribe(registration).then( |
|
|
|
subscription => sendSubscriptionToBackend(subscription, me)); |
|
|
|
subscription => sendSubscriptionToBackend(getState, subscription, me)); |
|
|
|
}) |
|
|
|
}) |
|
|
|
.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)
|
|
|
@ -137,7 +137,7 @@ export function saveSettings() { |
|
|
|
const alerts = state.get('alerts'); |
|
|
|
const alerts = state.get('alerts'); |
|
|
|
const data = { alerts }; |
|
|
|
const data = { alerts }; |
|
|
|
|
|
|
|
|
|
|
|
axios.put(`/api/web/push_subscriptions/${subscription.get('id')}`, { |
|
|
|
api(getState).put(`/api/web/push_subscriptions/${subscription.get('id')}`, { |
|
|
|
data, |
|
|
|
data, |
|
|
|
}).then(() => { |
|
|
|
}).then(() => { |
|
|
|
const me = getState().getIn(['meta', 'me']); |
|
|
|
const me = getState().getIn(['meta', 'me']); |
|
|
|