Skip to content

Commit

Permalink
Implement settings logic change
Browse files Browse the repository at this point in the history
  • Loading branch information
kommunarr committed Nov 18, 2023
1 parent 8dfe1a5 commit e81d316
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
4 changes: 4 additions & 0 deletions src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions src/datastores/handlers/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/datastores/handlers/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
61 changes: 31 additions & 30 deletions src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -178,7 +178,7 @@ const state = {
defaultProfile: MAIN_PROFILE_ID,
defaultQuality: '720',
defaultSkipInterval: 5,
defaultTheatreMode: false,
defaultTheaterMode: false,
defaultVideoFormat: 'dash',
disableSmoothScrolling: false,
displayVideoPlayButton: true,
Expand Down Expand Up @@ -218,7 +218,7 @@ const state = {
hideSubscriptionsLive: false,
hideSubscriptionsCommunity: false,
hideTrendingVideos: false,
hideUnsubscribeButton: false,
hideSubscribeButton: false,
hideUpcomingPremieres: false,
hideVideoLikesAndDislikes: false,
hideVideoViews: false,
Expand All @@ -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',
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e81d316

Please sign in to comment.