From b697c160e64ecb614d96bd89c5572b93d91189a3 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Sun, 15 May 2022 20:51:02 -0500 Subject: Use DEFAULT_SERVICE_SETTINGS for default values (remove duplication) Added TODO comments, notes, debug logs. --- .../app/Controllers/Http/ServiceController.js | 47 +++++++++++++--------- src/models/Service.js | 1 + src/stores/SettingsStore.js | 9 +++-- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/internal-server/app/Controllers/Http/ServiceController.js b/src/internal-server/app/Controllers/Http/ServiceController.js index 81c03e5ff..26571323d 100644 --- a/src/internal-server/app/Controllers/Http/ServiceController.js +++ b/src/internal-server/app/Controllers/Http/ServiceController.js @@ -3,7 +3,7 @@ const { validateAll } = use('Validator'); const Env = use('Env'); const { v4: uuid } = require('uuid'); -const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER } = require('../../../../config'); +const { LOCAL_HOSTNAME, DEFAULT_SERVICE_ORDER, DEFAULT_SERVICE_SETTINGS } = require('../../../../config'); const { convertToJSON } = require('../../../../jsUtils'); const { API_VERSION } = require('../../../../environment-remote'); const moveIcon = require('../../ImageHelper'); @@ -14,8 +14,10 @@ const port = Env.get('PORT'); class ServiceController { // Create a new service for user async create({ request, response }) { + const data = request.all(); + // Validate user input - const validation = await validateAll(request.all(), { + const validation = await validateAll(data, { name: 'required|string', recipeId: 'required', }); @@ -27,16 +29,13 @@ class ServiceController { }); } - const data = request.all(); - // Get new, unused uuid let serviceId; do { serviceId = uuid(); } while ( // eslint-disable-next-line no-await-in-loop, unicorn/no-await-expression-member - (await Service.query().where('serviceId', serviceId).fetch()).rows - .length > 0 + (await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0 ); await Service.create({ @@ -46,21 +45,23 @@ class ServiceController { settings: JSON.stringify(data), }); + // TODO: Remove duplication return response.send({ data: { userId: 1, id: serviceId, - isEnabled: true, - isNotificationEnabled: true, - isBadgeEnabled: true, - isMuted: false, + isEnabled: DEFAULT_SERVICE_SETTINGS.isEnabled, + isNotificationEnabled: DEFAULT_SERVICE_SETTINGS.isNotificationEnabled, + isBadgeEnabled: DEFAULT_SERVICE_SETTINGS.isBadgeEnabled, + isMuted: DEFAULT_SERVICE_SETTINGS.isMuted, isDarkModeEnabled: '', // TODO: This should ideally be a boolean (false). But, changing it caused the sidebar toggle to not work. spellcheckerLanguage: '', order: DEFAULT_SERVICE_ORDER, customRecipe: false, - hasCustomIcon: false, + hasCustomIcon: DEFAULT_SERVICE_SETTINGS.customIcon, workspaces: [], iconUrl: null, + // Overwrite previous default settings with what's obtained from the request ...data, }, status: ['created'], @@ -75,18 +76,21 @@ class ServiceController { const servicesArray = services.map(service => { const settings = convertToJSON(service.settings); + // TODO: Remove duplication return { customRecipe: false, hasCustomIcon: false, - isBadgeEnabled: true, + isBadgeEnabled: DEFAULT_SERVICE_SETTINGS.isBadgeEnabled, isDarkModeEnabled: '', // TODO: This should ideally be a boolean (false). But, changing it caused the sidebar toggle to not work. - isEnabled: true, - isMuted: false, - isNotificationEnabled: true, + isEnabled: DEFAULT_SERVICE_SETTINGS.isEnabled, + isMuted: DEFAULT_SERVICE_SETTINGS.isMuted, + isNotificationEnabled: DEFAULT_SERVICE_SETTINGS.isNotificationEnabled, order: DEFAULT_SERVICE_ORDER, spellcheckerLanguage: '', workspaces: [], + // Overwrite previous default settings with what's obtained from the db ...settings, + // Overwrite even after the spread operator with specific values iconUrl: settings.iconId ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` : null, @@ -217,18 +221,21 @@ class ServiceController { const servicesArray = services.map(service => { const settings = convertToJSON(service.settings); + // TODO: Remove duplication return { customRecipe: false, - hasCustomIcon: false, - isBadgeEnabled: true, + hasCustomIcon: DEFAULT_SERVICE_SETTINGS.customIcon, + isBadgeEnabled: DEFAULT_SERVICE_SETTINGS.isBadgeEnabled, isDarkModeEnabled: '', // TODO: This should ideally be a boolean (false). But, changing it caused the sidebar toggle to not work. - isEnabled: true, - isMuted: false, - isNotificationEnabled: true, + isEnabled: DEFAULT_SERVICE_SETTINGS.isEnabled, + isMuted: DEFAULT_SERVICE_SETTINGS.isMuted, + isNotificationEnabled: DEFAULT_SERVICE_SETTINGS.isNotificationEnabled, order: DEFAULT_SERVICE_ORDER, spellcheckerLanguage: '', workspaces: [], + // Overwrite previous default settings with what's obtained from the db ...settings, + // Overwrite even after the spread operator with specific values iconUrl: settings.iconId ? `http://${hostname}:${port}/${API_VERSION}/icon/${settings.iconId}` : null, diff --git a/src/models/Service.js b/src/models/Service.js index 1fca034bc..af42f9548 100644 --- a/src/models/Service.js +++ b/src/models/Service.js @@ -16,6 +16,7 @@ import { const debug = require('../preload-safe-debug')('Ferdium:Service'); +// TODO: Shouldn't most of these values default to what's defined in DEFAULT_SERVICE_SETTINGS? export default class Service { id = ''; diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index 6b6b77454..7afecd9df 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js @@ -188,10 +188,9 @@ export default class SettingsStore extends Store { // Helper async _migrate() { - const legacySettings = localStorage.getItem('app') || {}; - this._ensureMigrationAndMarkDone('password-hashing', () => { if (this.stores.settings.app.lockedPassword !== '') { + const legacySettings = localStorage.getItem('app') || {}; this.actions.settings.update({ type: 'app', data: { @@ -200,7 +199,7 @@ export default class SettingsStore extends Store { }); } - debug('Migrated updates settings'); + debug('Migrated password-hashing settings'); }); this._ensureMigrationAndMarkDone('5.6.0-beta.6-settings', () => { @@ -210,6 +209,8 @@ export default class SettingsStore extends Store { searchEngine: DEFAULT_APP_SETTINGS.searchEngine, }, }); + + debug('Migrated default search engine settings'); }); this._ensureMigrationAndMarkDone('user-agent-settings', () => { @@ -219,6 +220,8 @@ export default class SettingsStore extends Store { userAgentPref: DEFAULT_APP_SETTINGS.userAgentPref, }, }); + + debug('Migrated default user-agent settings'); }); } } -- cgit v1.2.3-70-g09d2