From e81d3162e2c8fffb9b6decb101711c09d84956e6 Mon Sep 17 00:00:00 2001 From: Jason Henriquez Date: Fri, 17 Nov 2023 08:38:57 -0600 Subject: [PATCH] Implement settings logic change --- src/datastores/handlers/base.js | 4 ++ src/datastores/handlers/electron.js | 7 +++ src/datastores/handlers/web.js | 4 ++ src/main/index.js | 9 +++- src/renderer/store/modules/settings.js | 61 +++++++++++++------------- 5 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/datastores/handlers/base.js b/src/datastores/handlers/base.js index 71e1ca3889682..a99f00763f90a 100644 --- a/src/datastores/handlers/base.js +++ b/src/datastores/handlers/base.js @@ -13,6 +13,10 @@ class Settings { return db.settings.compactDatafileAsync() } + static delete(setting) { + return db.history.removeAsync({ _id: setting }) + } + // ******************** // // Unique Electron main process handlers static _findAppReadyRelatedSettings() { diff --git a/src/datastores/handlers/electron.js b/src/datastores/handlers/electron.js index ddb90ff82434e..b4e8402d35c56 100644 --- a/src/datastores/handlers/electron.js +++ b/src/datastores/handlers/electron.js @@ -15,6 +15,13 @@ class Settings { { action: DBActions.GENERAL.UPSERT, data: { _id, value } } ) } + + static delete(setting) { + return ipcRenderer.invoke( + IpcChannels.DB_SETTINGS, + { action: DBActions.GENERAL.DELETE, data: setting } + ) + } } class History { diff --git a/src/datastores/handlers/web.js b/src/datastores/handlers/web.js index a81eb305d1dd9..d9892b42e154b 100644 --- a/src/datastores/handlers/web.js +++ b/src/datastores/handlers/web.js @@ -18,6 +18,10 @@ class Settings { static upsert(_id, value) { return baseHandlers.settings.upsert(_id, value) } + + static delete(setting) { + return baseHandlers.settings.delete(setting) + } } class History { diff --git a/src/main/index.js b/src/main/index.js index 63e7abb15d789..e1bcdd997ac26 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -805,7 +805,14 @@ function runApp() { // Do nothing for unmatched settings } return null - + case DBActions.GENERAL.DELETE: + await baseHandlers.settings.delete(data) + syncOtherWindows( + IpcChannels.SYNC_SETTINGS, + event, + { event: SyncEvents.GENERAL.DELETE, data } + ) + return null default: // eslint-disable-next-line no-throw-literal throw 'invalid settings db action' diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index 4a250018be88f..74118f683f528 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -162,8 +162,8 @@ const defaultSideEffectsTriggerId = settingId => /*****/ const state = { - autoplayPlaylists: true, - autoplayVideos: true, + enablePlaylistAutoplay: true, + startVideosAutomatically: true, backendFallback: process.env.IS_ELECTRON, backendPreference: !process.env.IS_ELECTRON ? 'invidious' : 'local', barColor: false, @@ -178,7 +178,7 @@ const state = { defaultProfile: MAIN_PROFILE_ID, defaultQuality: '720', defaultSkipInterval: 5, - defaultTheatreMode: false, + defaultTheaterMode: false, defaultVideoFormat: 'dash', disableSmoothScrolling: false, displayVideoPlayButton: true, @@ -218,7 +218,7 @@ const state = { hideSubscriptionsLive: false, hideSubscriptionsCommunity: false, hideTrendingVideos: false, - hideUnsubscribeButton: false, + hideSubscribeButton: false, hideUpcomingPremieres: false, hideVideoLikesAndDislikes: false, hideVideoViews: false, @@ -229,7 +229,7 @@ const state = { landingPage: 'subscriptions', listType: 'grid', maxVideoPlaybackRate: 3, - playNextVideo: false, + enableAutoplay: false, proxyHostname: '127.0.0.1', proxyPort: '9050', proxyProtocol: 'socks5', @@ -299,15 +299,13 @@ const state = { useDeArrowTitles: false, } -// NOTE: when an old setting's variable name is changed, place the new value here as the key -// and keep the original key in the state object above. This preserves users' settings selections -// even after these variable names are altered, and even in older versions of FreeTube. -const aliasToOriginal = { - defaultTheaterMode: 'defaultTheatreMode', - enableAutoplay: 'playNextVideo', - enablePlaylistAutoplay: 'autoplayPlaylists', - hideSubscribeButton: 'hideUnsubscribeButton', - startVideosAutomatically: 'autoplayVideos' +/* Mapping of older settings whose variable names have changed to their newer values */ +const outdatedSettings = { + defaultTheatreMode: 'defaultTheaterMode', + playNextVideo: 'enableAutoplay', + autoplayPlaylists: 'enablePlaylistAutoplay', + hideUnsubscribeButton: 'hideSubscribeButton', + autoplayVideos: 'startVideosAutomatically' } const stateWithSideEffects = { @@ -429,8 +427,7 @@ const customActions = { Object.fromEntries((await DBSettingHandlers.find()).map(({ _id, value }) => { return [_id, value] }))) ) - for (const setting of userSettings) { - const [_id, value] = setting + const loadSetting = (_id, value) => { if (getters.settingHasSideEffects(_id)) { dispatch(defaultSideEffectsTriggerId(_id), value) } @@ -439,6 +436,24 @@ const customActions = { commit(defaultMutationId(_id), value) } } + + for (const setting of userSettings) { + const [_id, value] = setting + loadSetting(_id, value) + } + + // Apply existing values of outdated setting variables in the DB to their newer equivalents, + // then delete those older settings + for (const outdatedSetting of Object.keys(outdatedSettings)) { + const outdatedSettingInDB = userSettings.find((setting) => setting[0] === outdatedSetting) + if (!outdatedSettingInDB) { + return + } + const newSetting = outdatedSettings[outdatedSetting] + const oldValue = outdatedSettingInDB[1] + loadSetting(newSetting, oldValue) + await DBSettingHandlers.delete(outdatedSetting) + } } catch (errMessage) { console.error(errMessage) } @@ -556,20 +571,6 @@ for (const settingId of Object.keys(state)) { buildSettingsStoreMethods(settingId) } -// point alias keys to their original values -for (const alias of Object.keys(aliasToOriginal)) { - const aliasFor = aliasToOriginal[alias] - const originalGetter = getters[defaultGetterId(aliasFor)] - const originalMutation = mutations[defaultMutationId(aliasFor)] - const originalTrigger = actions[defaultSideEffectsTriggerId(aliasFor)] - const originalAction = actions[defaultUpdaterId(aliasFor)] - - if (originalGetter) getters[defaultGetterId(alias)] = originalGetter - if (originalMutation) mutations[defaultMutationId(alias)] = originalMutation - if (originalTrigger) actions[defaultSideEffectsTriggerId(alias)] = originalTrigger - if (originalAction) actions[defaultUpdaterId(alias)] = originalAction -} - function buildSettingsStoreMethods(settingId) { const getterId = defaultGetterId(settingId) const mutationId = defaultMutationId(settingId)