Skip to content

Commit 475cddb

Browse files
committed
Implement settings logic change
1 parent 8dfe1a5 commit 475cddb

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

src/datastores/handlers/base.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class Settings {
1313
return db.settings.compactDatafileAsync()
1414
}
1515

16+
static delete(setting) {
17+
return db.settings.removeAsync({ _id: setting })
18+
}
19+
1620
// ******************** //
1721
// Unique Electron main process handlers
1822
static _findAppReadyRelatedSettings() {

src/datastores/handlers/electron.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class Settings {
1515
{ action: DBActions.GENERAL.UPSERT, data: { _id, value } }
1616
)
1717
}
18+
19+
static delete(setting) {
20+
return ipcRenderer.invoke(
21+
IpcChannels.DB_SETTINGS,
22+
{ action: DBActions.GENERAL.DELETE, data: setting }
23+
)
24+
}
1825
}
1926

2027
class History {

src/datastores/handlers/web.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class Settings {
1818
static upsert(_id, value) {
1919
return baseHandlers.settings.upsert(_id, value)
2020
}
21+
22+
static delete(setting) {
23+
return baseHandlers.settings.delete(setting)
24+
}
2125
}
2226

2327
class History {

src/main/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,14 @@ function runApp() {
805805
// Do nothing for unmatched settings
806806
}
807807
return null
808-
808+
case DBActions.GENERAL.DELETE:
809+
await baseHandlers.settings.delete(data)
810+
syncOtherWindows(
811+
IpcChannels.SYNC_SETTINGS,
812+
event,
813+
{ event: SyncEvents.GENERAL.DELETE, data }
814+
)
815+
return null
809816
default:
810817
// eslint-disable-next-line no-throw-literal
811818
throw 'invalid settings db action'

src/renderer/store/modules/settings.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ const defaultSideEffectsTriggerId = settingId =>
162162
/*****/
163163

164164
const state = {
165-
autoplayPlaylists: true,
166-
autoplayVideos: true,
165+
enablePlaylistAutoplay: true,
166+
startVideosAutomatically: true,
167167
backendFallback: process.env.IS_ELECTRON,
168168
backendPreference: !process.env.IS_ELECTRON ? 'invidious' : 'local',
169169
barColor: false,
@@ -178,7 +178,7 @@ const state = {
178178
defaultProfile: MAIN_PROFILE_ID,
179179
defaultQuality: '720',
180180
defaultSkipInterval: 5,
181-
defaultTheatreMode: false,
181+
defaultTheaterMode: false,
182182
defaultVideoFormat: 'dash',
183183
disableSmoothScrolling: false,
184184
displayVideoPlayButton: true,
@@ -218,7 +218,7 @@ const state = {
218218
hideSubscriptionsLive: false,
219219
hideSubscriptionsCommunity: false,
220220
hideTrendingVideos: false,
221-
hideUnsubscribeButton: false,
221+
hideSubscribeButton: false,
222222
hideUpcomingPremieres: false,
223223
hideVideoLikesAndDislikes: false,
224224
hideVideoViews: false,
@@ -229,7 +229,7 @@ const state = {
229229
landingPage: 'subscriptions',
230230
listType: 'grid',
231231
maxVideoPlaybackRate: 3,
232-
playNextVideo: false,
232+
enableAutoplay: false,
233233
proxyHostname: '127.0.0.1',
234234
proxyPort: '9050',
235235
proxyProtocol: 'socks5',
@@ -299,15 +299,13 @@ const state = {
299299
useDeArrowTitles: false,
300300
}
301301

302-
// NOTE: when an old setting's variable name is changed, place the new value here as the key
303-
// and keep the original key in the state object above. This preserves users' settings selections
304-
// even after these variable names are altered, and even in older versions of FreeTube.
305-
const aliasToOriginal = {
306-
defaultTheaterMode: 'defaultTheatreMode',
307-
enableAutoplay: 'playNextVideo',
308-
enablePlaylistAutoplay: 'autoplayPlaylists',
309-
hideSubscribeButton: 'hideUnsubscribeButton',
310-
startVideosAutomatically: 'autoplayVideos'
302+
/* Mapping of older settings whose variable names have changed to their newer values */
303+
const outdatedSettings = {
304+
defaultTheatreMode: 'defaultTheaterMode',
305+
playNextVideo: 'enableAutoplay',
306+
autoplayPlaylists: 'enablePlaylistAutoplay',
307+
hideUnsubscribeButton: 'hideSubscribeButton',
308+
autoplayVideos: 'startVideosAutomatically'
311309
}
312310

313311
const stateWithSideEffects = {
@@ -429,8 +427,7 @@ const customActions = {
429427
Object.fromEntries((await DBSettingHandlers.find()).map(({ _id, value }) => { return [_id, value] })))
430428
)
431429

432-
for (const setting of userSettings) {
433-
const [_id, value] = setting
430+
const loadSetting = (_id, value) => {
434431
if (getters.settingHasSideEffects(_id)) {
435432
dispatch(defaultSideEffectsTriggerId(_id), value)
436433
}
@@ -439,6 +436,24 @@ const customActions = {
439436
commit(defaultMutationId(_id), value)
440437
}
441438
}
439+
440+
for (const setting of userSettings) {
441+
const [_id, value] = setting
442+
loadSetting(_id, value)
443+
}
444+
445+
// Apply existing values of outdated setting variables in the DB to their newer equivalents,
446+
// then delete those older settings
447+
for (const outdatedSetting of Object.keys(outdatedSettings)) {
448+
const outdatedSettingInDB = userSettings.find((setting) => setting[0] === outdatedSetting)
449+
if (!outdatedSettingInDB) {
450+
return
451+
}
452+
const newSetting = outdatedSettings[outdatedSetting]
453+
const oldValue = outdatedSettingInDB[1]
454+
loadSetting(newSetting, oldValue)
455+
await DBSettingHandlers.delete(outdatedSetting)
456+
}
442457
} catch (errMessage) {
443458
console.error(errMessage)
444459
}
@@ -556,20 +571,6 @@ for (const settingId of Object.keys(state)) {
556571
buildSettingsStoreMethods(settingId)
557572
}
558573

559-
// point alias keys to their original values
560-
for (const alias of Object.keys(aliasToOriginal)) {
561-
const aliasFor = aliasToOriginal[alias]
562-
const originalGetter = getters[defaultGetterId(aliasFor)]
563-
const originalMutation = mutations[defaultMutationId(aliasFor)]
564-
const originalTrigger = actions[defaultSideEffectsTriggerId(aliasFor)]
565-
const originalAction = actions[defaultUpdaterId(aliasFor)]
566-
567-
if (originalGetter) getters[defaultGetterId(alias)] = originalGetter
568-
if (originalMutation) mutations[defaultMutationId(alias)] = originalMutation
569-
if (originalTrigger) actions[defaultSideEffectsTriggerId(alias)] = originalTrigger
570-
if (originalAction) actions[defaultUpdaterId(alias)] = originalAction
571-
}
572-
573574
function buildSettingsStoreMethods(settingId) {
574575
const getterId = defaultGetterId(settingId)
575576
const mutationId = defaultMutationId(settingId)

0 commit comments

Comments
 (0)