* Move push notifications settings * fix typo `setf` -> `set`
This commit is contained in:
		
				
					committed by
					
						
						Eugen Rochko
					
				
			
			
				
	
			
			
			
						parent
						
							3a52c90de1
						
					
				
				
					commit
					cdae7e4c8b
				
			@@ -1,5 +1,5 @@
 | 
				
			|||||||
import axios from 'axios';
 | 
					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_BROWSER_SUPPORT = 'PUSH_NOTIFICATIONS_SET_BROWSER_SUPPORT';
 | 
				
			||||||
export const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION';
 | 
					export const SET_SUBSCRIPTION = 'PUSH_NOTIFICATIONS_SET_SUBSCRIPTION';
 | 
				
			||||||
@@ -50,7 +50,7 @@ export function saveSettings() {
 | 
				
			|||||||
    }).then(() => {
 | 
					    }).then(() => {
 | 
				
			||||||
      const me = getState().getIn(['meta', 'me']);
 | 
					      const me = getState().getIn(['meta', 'me']);
 | 
				
			||||||
      if (me) {
 | 
					      if (me) {
 | 
				
			||||||
        setSettingsToLocalStorage(me, data);
 | 
					        pushNotificationsSetting.set(me, data);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										46
									
								
								app/javascript/mastodon/settings.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								app/javascript/mastodon/settings.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
				
			|||||||
 | 
					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');
 | 
				
			||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
import axios from 'axios';
 | 
					import axios from 'axios';
 | 
				
			||||||
import { store } from './containers/mastodon';
 | 
					import { store } from './containers/mastodon';
 | 
				
			||||||
import { setBrowserSupport, setSubscription, clearSubscription } from './actions/push_notifications';
 | 
					import { setBrowserSupport, setSubscription, clearSubscription } from './actions/push_notifications';
 | 
				
			||||||
 | 
					import { pushNotificationsSetting } from './settings';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Taken from https://www.npmjs.com/package/web-push
 | 
					// Taken from https://www.npmjs.com/package/web-push
 | 
				
			||||||
const urlBase64ToUint8Array = (base64String) => {
 | 
					const urlBase64ToUint8Array = (base64String) => {
 | 
				
			||||||
@@ -40,7 +41,7 @@ const sendSubscriptionToBackend = (subscription) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  const me = store.getState().getIn(['meta', 'me']);
 | 
					  const me = store.getState().getIn(['meta', 'me']);
 | 
				
			||||||
  if (me) {
 | 
					  if (me) {
 | 
				
			||||||
    const data = getSettingsFromLocalStorage(me);
 | 
					    const data = pushNotificationsSetting.get(me);
 | 
				
			||||||
    if (data) {
 | 
					    if (data) {
 | 
				
			||||||
      params.data = data;
 | 
					      params.data = data;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -52,16 +53,14 @@ const sendSubscriptionToBackend = (subscription) => {
 | 
				
			|||||||
// 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
 | 
				
			||||||
const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype);
 | 
					const supportsPushNotifications = ('serviceWorker' in navigator && 'PushManager' in window && 'getKey' in PushSubscription.prototype);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const SUBSCRIPTION_DATA_STORAGE_KEY = 'mastodon_push_notification_data';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export function register () {
 | 
					export function register () {
 | 
				
			||||||
  store.dispatch(setBrowserSupport(supportsPushNotifications));
 | 
					  store.dispatch(setBrowserSupport(supportsPushNotifications));
 | 
				
			||||||
  const me = store.getState().getIn(['meta', 'me']);
 | 
					  const me = store.getState().getIn(['meta', 'me']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (me && !getSettingsFromLocalStorage(me)) {
 | 
					  if (me && !pushNotificationsSetting.get(me)) {
 | 
				
			||||||
    const alerts = store.getState().getIn(['push_notifications', 'alerts']);
 | 
					    const alerts = store.getState().getIn(['push_notifications', 'alerts']);
 | 
				
			||||||
    if (alerts) {
 | 
					    if (alerts) {
 | 
				
			||||||
      setSettingsToLocalStorage(me, { alerts: alerts });
 | 
					      pushNotificationsSetting.set(me, { alerts: alerts });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -99,7 +98,7 @@ export function register () {
 | 
				
			|||||||
        if (!(subscription instanceof PushSubscription)) {
 | 
					        if (!(subscription instanceof PushSubscription)) {
 | 
				
			||||||
          store.dispatch(setSubscription(subscription));
 | 
					          store.dispatch(setSubscription(subscription));
 | 
				
			||||||
          if (me) {
 | 
					          if (me) {
 | 
				
			||||||
            setSettingsToLocalStorage(me, { alerts: subscription.alerts });
 | 
					            pushNotificationsSetting.set(me, { alerts: subscription.alerts });
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
@@ -113,7 +112,7 @@ export function register () {
 | 
				
			|||||||
        // Clear alerts and hide UI settings
 | 
					        // Clear alerts and hide UI settings
 | 
				
			||||||
        store.dispatch(clearSubscription());
 | 
					        store.dispatch(clearSubscription());
 | 
				
			||||||
        if (me) {
 | 
					        if (me) {
 | 
				
			||||||
          removeSettingsFromLocalStorage(me);
 | 
					          pushNotificationsSetting.remove(me);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
@@ -128,21 +127,3 @@ export function register () {
 | 
				
			|||||||
    console.warn('Your browser does not support Web Push Notifications.');
 | 
					    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}`);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user