import axios from 'axios';
-import { setSettingsToLocalStorage } from '../web_push_subscription';
+import { pushNotificationsSetting } from '../settings';
export const SET_BROWSER_SUPPORT = 'PUSH_NOTIFICATIONS_SET_BROWSER_SUPPORT';
export const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION';
}).then(() => {
const me = getState().getIn(['meta', 'me']);
if (me) {
- setSettingsToLocalStorage(me, data);
+ pushNotificationsSetting.set(me, data);
}
});
};
--- /dev/null
+export default class Settings {
+
+ constructor(keyBase = null) {
+ this.keyBase = keyBase;
+ }
+
+ generateKey(id) {
+ return this.keyBase ? [this.keyBase, `id${id}`].join('.') : id;
+ }
+
+ set(id, data) {
+ const key = this.generateKey(id);
+ try {
+ const encodedData = JSON.stringify(data);
+ localStorage.setItem(key, encodedData);
+ return data;
+ } catch (e) {
+ return null;
+ }
+ }
+
+ get(id) {
+ const key = this.generateKey(id);
+ try {
+ const rawData = localStorage.getItem(key);
+ return JSON.parse(rawData);
+ } catch (e) {
+ return null;
+ }
+ }
+
+ remove(id) {
+ const data = this.get(id);
+ if (data) {
+ const key = this.generateKey(id);
+ try {
+ localStorage.removeItem(key);
+ } catch (e) {
+ }
+ }
+ return data;
+ }
+
+}
+
+export const pushNotificationsSetting = new Settings('mastodon_push_notification_data');
import axios from 'axios';
import { store } from './containers/mastodon';
import { setBrowserSupport, setSubscription, clearSubscription } from './actions/push_notifications';
+import { pushNotificationsSetting } from './settings';
// Taken from https://www.npmjs.com/package/web-push
const urlBase64ToUint8Array = (base64String) => {
const me = store.getState().getIn(['meta', 'me']);
if (me) {
- const data = getSettingsFromLocalStorage(me);
+ const data = pushNotificationsSetting.get(me);
if (data) {
params.data = data;
}
// Last one checks for payload support: https://web-push-book.gauntface.com/chapter-06/01-non-standards-browsers/#no-payload
const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype);
-const SUBSCRIPTION_DATA_STORAGE_KEY = 'mastodon_push_notification_data';
-
export function register () {
store.dispatch(setBrowserSupport(supportsPushNotifications));
const me = store.getState().getIn(['meta', 'me']);
- if (me && !getSettingsFromLocalStorage(me)) {
+ if (me && !pushNotificationsSetting.get(me)) {
const alerts = store.getState().getIn(['push_notifications', 'alerts']);
if (alerts) {
- setSettingsToLocalStorage(me, { alerts: alerts });
+ pushNotificationsSetting.set(me, { alerts: alerts });
}
}
if (!(subscription instanceof PushSubscription)) {
store.dispatch(setSubscription(subscription));
if (me) {
- setSettingsToLocalStorage(me, { alerts: subscription.alerts });
+ pushNotificationsSetting.set(me, { alerts: subscription.alerts });
}
}
})
// Clear alerts and hide UI settings
store.dispatch(clearSubscription());
if (me) {
- removeSettingsFromLocalStorage(me);
+ pushNotificationsSetting.remove(me);
}
try {
console.warn('Your browser does not support Web Push Notifications.');
}
}
-
-export function setSettingsToLocalStorage(id, data) {
- try {
- localStorage.setItem(`${SUBSCRIPTION_DATA_STORAGE_KEY}_${id}`, JSON.stringify(data));
- } catch (e) {}
-}
-
-export function getSettingsFromLocalStorage(id) {
- try {
- return JSON.parse(localStorage.getItem(`${SUBSCRIPTION_DATA_STORAGE_KEY}_${id}`));
- } catch (e) {}
-
- return null;
-}
-
-export function removeSettingsFromLocalStorage(id) {
- localStorage.removeItem(`${SUBSCRIPTION_DATA_STORAGE_KEY}_${id}`);
-}