From f1c16327b696666f291d5fb2afdf4033cf9ef76d Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Fri, 7 Sep 2018 18:17:17 +0300 Subject: Initial version --- src/lib/persisted_state.js | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src/lib/persisted_state.js') diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js index 60811e65..007515cd 100644 --- a/src/lib/persisted_state.js +++ b/src/lib/persisted_state.js @@ -1,7 +1,7 @@ import merge from 'lodash.merge' import objectPath from 'object-path' import localforage from 'localforage' -import { throttle, each } from 'lodash' +import { each } from 'lodash' let loaded = false @@ -12,18 +12,17 @@ const defaultReducer = (state, paths) => ( }, {}) ) +const saveImmedeatelyActions = [ + 'markNotificationsAsSeen', + 'clearCurrentUser', + 'setCurrentUser', + 'setOption' +] + const defaultStorage = (() => { return localforage })() -const defaultSetState = (key, state, storage) => { - if (!loaded) { - console.log('waiting for old state to be loaded...') - } else { - return storage.setItem(key, state) - } -} - export default function createPersistedState ({ key = 'vuex-lz', paths = [], @@ -31,7 +30,14 @@ export default function createPersistedState ({ let value = storage.getItem(key) return value }, - setState = throttle(defaultSetState, 60000), + setState = (key, state, storage) => { + if (!loaded) { + console.log('waiting for old state to be loaded...') + return Promise.resolve() + } else { + return storage.setItem(key, state) + } + }, reducer = defaultReducer, storage = defaultStorage, subscriber = store => handler => store.subscribe(handler) @@ -72,7 +78,22 @@ export default function createPersistedState ({ subscriber(store)((mutation, state) => { try { - setState(key, reducer(state, paths), storage) + if (saveImmedeatelyActions.includes(mutation.type)) { + setState(key, reducer(state, paths), storage) + .then(success => { + if (typeof success !== 'undefined') { + if (mutation.type === 'setOption') { + store.dispatch('settingsSaved', { success }) + } + } + }, error => { + if (mutation.type === 'setOption') { + store.dispatch('settingsSaved', { error }) + } + }) + } else { + console.warn(`Not saving to localStorage for: ${mutation.type}`) + } } catch (e) { console.log("Couldn't persist state:") console.log(e) -- cgit v1.2.3-70-g09d2 From 580aae1b545331b284724e54cc31c99e52862f57 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 9 Sep 2018 21:51:40 +0300 Subject: Added more stuff that's actually being added to instanceConfig, simplified the whitelist. --- src/lib/persisted_state.js | 1 + src/main.js | 20 +++----------------- src/modules/instance.js | 11 +++++++++++ 3 files changed, 15 insertions(+), 17 deletions(-) (limited to 'src/lib/persisted_state.js') diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js index 007515cd..c9eac91f 100644 --- a/src/lib/persisted_state.js +++ b/src/lib/persisted_state.js @@ -16,6 +16,7 @@ const saveImmedeatelyActions = [ 'markNotificationsAsSeen', 'clearCurrentUser', 'setCurrentUser', + 'setHighlight', 'setOption' ] diff --git a/src/main.js b/src/main.js index 23bd2cd3..4e7b55bb 100644 --- a/src/main.js +++ b/src/main.js @@ -47,23 +47,7 @@ Vue.use(VueChatScroll) const persistedStateOptions = { paths: [ - 'config.collapseMessageWithSubject', - 'config.hideAttachments', - 'config.hideAttachmentsInConv', - 'config.hideNsfw', - 'config.replyVisibility', - 'config.notificationVisibility', - 'config.autoLoad', - 'config.hoverPreview', - 'config.streaming', - 'config.muteWords', - 'config.customTheme', - 'config.highlight', - 'config.loopVideo', - 'config.loopVideoSilentOnly', - 'config.pauseOnUnfocused', - 'config.stopGifs', - 'config.interfaceLanguage', + 'config', 'users.lastLoginName', 'statuses.notifications.maxSavedId' ] @@ -129,6 +113,8 @@ window.fetch('/api/statusnet/config.json') store.dispatch('setInstanceOption', { name: 'logo', value: logo }) store.dispatch('setInstanceOption', { name: 'logoMask', value: logoMask }) store.dispatch('setInstanceOption', { name: 'logoMargin', value: logoMargin }) + store.dispatch('setInstanceOption', { name: 'redirectRootNoLogin', value: redirectRootNoLogin }) + store.dispatch('setInstanceOption', { name: 'redirectRootLogin', value: redirectRootLogin }) store.dispatch('setInstanceOption', { name: 'showInstanceSpecificPanel', value: showInstanceSpecificPanel }) store.dispatch('setInstanceOption', { name: 'scopeOptionsEnabled', value: scopeOptionsEnabled }) store.dispatch('setInstanceOption', { name: 'formattingOptionsEnabled', value: formattingOptionsEnabled }) diff --git a/src/modules/instance.js b/src/modules/instance.js index 228cee4c..a4fc9651 100644 --- a/src/modules/instance.js +++ b/src/modules/instance.js @@ -1,6 +1,7 @@ import { set } from 'vue' const defaultState = { + // Stuff from static/config.json and apiConfig name: 'Pleroma FE', registrationOpen: true, textlimit: 5000, @@ -17,9 +18,19 @@ const defaultState = { formattingOptionsEnabled: false, collapseMessageWithSubject: false, disableChat: false, + // Nasty stuff pleromaBackend: true, + emoji: [], customEmoji: [], + + // Feature-set, apparently, not everything here is reported... + mediaProxyAvailable: false, + chatAvailable: false, + gopherAvailable: false, + suggestionsEnabled: false, + suggestionsWeb: '', + // Html stuff instanceSpecificPanelContent: '', tos: '' -- cgit v1.2.3-70-g09d2 From 40a175389a90dbbc7fc46e2ec532e7e21576f2ff Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 17 Sep 2018 18:00:56 +0300 Subject: Removed warning. Added support for working without static/config.json --- src/lib/persisted_state.js | 2 - src/main.js | 147 +++++++++++++++++++++++---------------------- 2 files changed, 76 insertions(+), 73 deletions(-) (limited to 'src/lib/persisted_state.js') diff --git a/src/lib/persisted_state.js b/src/lib/persisted_state.js index c9eac91f..006107e2 100644 --- a/src/lib/persisted_state.js +++ b/src/lib/persisted_state.js @@ -92,8 +92,6 @@ export default function createPersistedState ({ store.dispatch('settingsSaved', { error }) } }) - } else { - console.warn(`Not saving to localStorage for: ${mutation.type}`) } } catch (e) { console.log("Couldn't persist state:") diff --git a/src/main.js b/src/main.js index 4e7b55bb..5314437b 100644 --- a/src/main.js +++ b/src/main.js @@ -89,80 +89,85 @@ window.fetch('/api/statusnet/config.json') var apiConfig = data.site.pleromafe window.fetch('/static/config.json') - .then((res) => res.json()) - .then((data) => { - var staticConfig = data - // This takes static config and overrides properties that are present in apiConfig - var config = Object.assign({}, staticConfig, apiConfig) - - var theme = (config.theme) - var background = (config.background) - var logo = (config.logo) - var logoMask = (typeof config.logoMask === 'undefined' ? true : config.logoMask) - var logoMargin = (typeof config.logoMargin === 'undefined' ? 0 : config.logoMargin) - var redirectRootNoLogin = (config.redirectRootNoLogin) - var redirectRootLogin = (config.redirectRootLogin) - var chatDisabled = (config.chatDisabled) - var showInstanceSpecificPanel = (config.showInstanceSpecificPanel) - var scopeOptionsEnabled = (config.scopeOptionsEnabled) - var formattingOptionsEnabled = (config.formattingOptionsEnabled) - var defaultCollapseMessageWithSubject = (config.collapseMessageWithSubject) - - store.dispatch('setInstanceOption', { name: 'theme', value: theme }) - store.dispatch('setInstanceOption', { name: 'background', value: background }) - store.dispatch('setInstanceOption', { name: 'logo', value: logo }) - store.dispatch('setInstanceOption', { name: 'logoMask', value: logoMask }) - store.dispatch('setInstanceOption', { name: 'logoMargin', value: logoMargin }) - store.dispatch('setInstanceOption', { name: 'redirectRootNoLogin', value: redirectRootNoLogin }) - store.dispatch('setInstanceOption', { name: 'redirectRootLogin', value: redirectRootLogin }) - store.dispatch('setInstanceOption', { name: 'showInstanceSpecificPanel', value: showInstanceSpecificPanel }) - store.dispatch('setInstanceOption', { name: 'scopeOptionsEnabled', value: scopeOptionsEnabled }) - store.dispatch('setInstanceOption', { name: 'formattingOptionsEnabled', value: formattingOptionsEnabled }) - store.dispatch('setInstanceOption', { name: 'collapseMessageWithSubject', value: defaultCollapseMessageWithSubject }) - if (chatDisabled) { - store.dispatch('disableChat') - } - - const routes = [ - { name: 'root', - path: '/', - redirect: to => { - return (store.state.users.currentUser ? redirectRootLogin : redirectRootNoLogin) || '/main/all' - }}, - { path: '/main/all', component: PublicAndExternalTimeline }, - { path: '/main/public', component: PublicTimeline }, - { path: '/main/friends', component: FriendsTimeline }, - { path: '/tag/:tag', component: TagTimeline }, - { name: 'conversation', path: '/notice/:id', component: ConversationPage, meta: { dontScroll: true } }, - { name: 'user-profile', path: '/users/:id', component: UserProfile }, - { name: 'mentions', path: '/:username/mentions', component: Mentions }, - { name: 'settings', path: '/settings', component: Settings }, - { name: 'registration', path: '/registration', component: Registration }, - { name: 'registration', path: '/registration/:token', component: Registration }, - { name: 'friend-requests', path: '/friend-requests', component: FollowRequests }, - { name: 'user-settings', path: '/user-settings', component: UserSettings } - ] - - const router = new VueRouter({ - mode: 'history', - routes, - scrollBehavior: (to, from, savedPosition) => { - if (to.matched.some(m => m.meta.dontScroll)) { - return false - } - return savedPosition || { x: 0, y: 0 } - } + .then((res) => res.json()) + .catch((err) => { + console.warn('Failed to load static/config.json, continuing without it.') + console.warn('Error was: ') + console.warn(err) + return {} }) + .then((staticConfig) => { + // This takes static config and overrides properties that are present in apiConfig + var config = Object.assign({}, staticConfig, apiConfig) + + var theme = (config.theme) + var background = (config.background) + var logo = (config.logo) + var logoMask = (typeof config.logoMask === 'undefined' ? true : config.logoMask) + var logoMargin = (typeof config.logoMargin === 'undefined' ? 0 : config.logoMargin) + var redirectRootNoLogin = (config.redirectRootNoLogin) + var redirectRootLogin = (config.redirectRootLogin) + var chatDisabled = (config.chatDisabled) + var showInstanceSpecificPanel = (config.showInstanceSpecificPanel) + var scopeOptionsEnabled = (config.scopeOptionsEnabled) + var formattingOptionsEnabled = (config.formattingOptionsEnabled) + var defaultCollapseMessageWithSubject = (config.collapseMessageWithSubject) + + store.dispatch('setInstanceOption', { name: 'theme', value: theme }) + store.dispatch('setInstanceOption', { name: 'background', value: background }) + store.dispatch('setInstanceOption', { name: 'logo', value: logo }) + store.dispatch('setInstanceOption', { name: 'logoMask', value: logoMask }) + store.dispatch('setInstanceOption', { name: 'logoMargin', value: logoMargin }) + store.dispatch('setInstanceOption', { name: 'redirectRootNoLogin', value: redirectRootNoLogin }) + store.dispatch('setInstanceOption', { name: 'redirectRootLogin', value: redirectRootLogin }) + store.dispatch('setInstanceOption', { name: 'showInstanceSpecificPanel', value: showInstanceSpecificPanel }) + store.dispatch('setInstanceOption', { name: 'scopeOptionsEnabled', value: scopeOptionsEnabled }) + store.dispatch('setInstanceOption', { name: 'formattingOptionsEnabled', value: formattingOptionsEnabled }) + store.dispatch('setInstanceOption', { name: 'collapseMessageWithSubject', value: defaultCollapseMessageWithSubject }) + if (chatDisabled) { + store.dispatch('disableChat') + } - /* eslint-disable no-new */ - new Vue({ - router, - store, - i18n, - el: '#app', - render: h => h(App) + const routes = [ + { name: 'root', + path: '/', + redirect: to => { + return (store.state.users.currentUser ? redirectRootLogin : redirectRootNoLogin) || '/main/all' + }}, + { path: '/main/all', component: PublicAndExternalTimeline }, + { path: '/main/public', component: PublicTimeline }, + { path: '/main/friends', component: FriendsTimeline }, + { path: '/tag/:tag', component: TagTimeline }, + { name: 'conversation', path: '/notice/:id', component: ConversationPage, meta: { dontScroll: true } }, + { name: 'user-profile', path: '/users/:id', component: UserProfile }, + { name: 'mentions', path: '/:username/mentions', component: Mentions }, + { name: 'settings', path: '/settings', component: Settings }, + { name: 'registration', path: '/registration', component: Registration }, + { name: 'registration', path: '/registration/:token', component: Registration }, + { name: 'friend-requests', path: '/friend-requests', component: FollowRequests }, + { name: 'user-settings', path: '/user-settings', component: UserSettings } + ] + + const router = new VueRouter({ + mode: 'history', + routes, + scrollBehavior: (to, from, savedPosition) => { + if (to.matched.some(m => m.meta.dontScroll)) { + return false + } + return savedPosition || { x: 0, y: 0 } + } + }) + + /* eslint-disable no-new */ + new Vue({ + router, + store, + i18n, + el: '#app', + render: h => h(App) + }) }) - }) }) window.fetch('/static/terms-of-service.html') -- cgit v1.2.3-70-g09d2