From f4d98130a5e0ecfa3629ddce798727f91068795b Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 1 Mar 2022 10:18:12 +0100 Subject: [PATCH] Simplify save settings --- dev/Knoin/AbstractViews.js | 9 ++++ dev/Remote/Admin/Fetch.js | 10 +--- dev/Settings/Admin/Contacts.js | 19 ++----- dev/Settings/Admin/General.js | 14 ++--- dev/Settings/Admin/Login.js | 34 +----------- dev/Settings/Admin/Packages.js | 17 +++--- dev/Settings/Admin/Security.js | 52 ++++++------------- dev/Settings/User/Contacts.js | 4 +- dev/Settings/User/General.js | 20 +++---- .../app/libraries/RainLoop/Actions/Admin.php | 43 ++++++--------- .../Views/Admin/AdminSettingsContacts.html | 2 +- .../templates/Views/User/SettingsGeneral.html | 4 +- 12 files changed, 66 insertions(+), 162 deletions(-) diff --git a/dev/Knoin/AbstractViews.js b/dev/Knoin/AbstractViews.js index 761df0991..5c7d56088 100644 --- a/dev/Knoin/AbstractViews.js +++ b/dev/Knoin/AbstractViews.js @@ -150,6 +150,15 @@ export class AbstractViewSettings }).debounce(999), }); } + + addSettings(names) + { + names.forEach(name => { + let prop = name[0].toLowerCase() + name.slice(1); + this[prop] || (this[prop] = ko.observable(SettingsGet(name))); + this[prop].subscribe(value => rl.app.Remote.saveSetting(name, value)); + }); + } } export class AbstractViewLogin extends AbstractViewCenter { diff --git a/dev/Remote/Admin/Fetch.js b/dev/Remote/Admin/Fetch.js index 624d05bf4..fa37da351 100644 --- a/dev/Remote/Admin/Fetch.js +++ b/dev/Remote/Admin/Fetch.js @@ -2,21 +2,13 @@ import { AbstractFetchRemote } from 'Remote/AbstractFetch'; class RemoteAdminFetch extends AbstractFetchRemote { - /** - * @param {?Function} fCallback - * @param {?} oData - */ - saveConfig(oData, fCallback) { - this.request('AdminSettingsUpdate', fCallback, oData); - } - /** * @param {string} key * @param {?scalar} value * @param {?Function} fCallback */ saveSetting(key, value, fCallback) { - this.saveConfig({[key]: value}, fCallback); + this.request('AdminSettingsUpdate', fCallback, {[key]: value}); } } diff --git a/dev/Settings/Admin/Contacts.js b/dev/Settings/Admin/Contacts.js index 6c02e830e..3373ab6de 100644 --- a/dev/Settings/Admin/Contacts.js +++ b/dev/Settings/Admin/Contacts.js @@ -2,7 +2,7 @@ import ko from 'ko'; import { SettingsGet } from 'Common/Globals'; import { defaultOptionsAfterRender } from 'Common/Utils'; -import { addObservablesTo, addSubscribablesTo } from 'External/ko'; +import { addObservablesTo } from 'External/ko'; import Remote from 'Remote/Admin/Fetch'; import { decorateKoCommands } from 'Knoin/Knoin'; @@ -22,10 +22,9 @@ export class ContactsAdminSettings extends AbstractViewSettings { this.testContactsErrorMessage(''); }); - addObservablesTo(this, { - enableContacts: !!SettingsGet('ContactsEnable'), - contactsSync: !!SettingsGet('ContactsSync'), + this.addSettings(['ContactsEnable','ContactsSync']); + addObservablesTo(this, { testing: false, testContactsSuccess: false, testContactsError: false, @@ -65,18 +64,6 @@ export class ContactsAdminSettings extends AbstractViewSettings { }) .extend({ notify: 'always' }); - addSubscribablesTo(this, { - enableContacts: value => - Remote.saveConfig({ - ContactsEnable: value ? 1 : 0 - }), - - contactsSync: value => - Remote.saveConfig({ - ContactsSync: value ? 1 : 0 - }) - }) - decorateKoCommands(this, { testContactsCommand: self => self.contactsPdoDsn() && self.contactsPdoUser() }); diff --git a/dev/Settings/Admin/General.js b/dev/Settings/Admin/General.js index 46dc6fb9a..f0c01f62d 100644 --- a/dev/Settings/Admin/General.js +++ b/dev/Settings/Admin/General.js @@ -37,9 +37,9 @@ export class GeneralAdminSettings extends AbstractViewSettings { this.theme = ThemeStore.theme; this.themes = ThemeStore.themes; + this.addSettings(['AllowLanguagesOnSettings','NewMoveToFolder']); + addObservablesTo(this, { - allowLanguagesOnSettings: SettingsGet('AllowLanguagesOnSettings'), - newMoveToFolder: SettingsGet('NewMoveToFolder'), attachmentLimitTrigger: SaveSettingsStep.Idle, themeTrigger: SaveSettingsStep.Idle, capaThemes: SettingsCapa(Capa.Themes), @@ -97,9 +97,7 @@ export class GeneralAdminSettings extends AbstractViewSettings { this.languageAdminTrigger(SaveSettingsStep.Animate); translatorReload(true, value) .then(fReloadLanguageHelper(SaveSettingsStep.TrueResult), fReloadLanguageHelper(SaveSettingsStep.FalseResult)) - .then(() => Remote.saveConfig({ - LanguageAdmin: value.trim() - })); + .then(() => Remote.saveSetting('LanguageAdmin', value)); }, capaAdditionalAccounts: fSaveHelper('CapaAdditionalAccounts'), @@ -110,11 +108,7 @@ export class GeneralAdminSettings extends AbstractViewSettings { capaThemes: fSaveHelper('CapaThemes'), - capaUserBackground: fSaveHelper('CapaUserBackground'), - - allowLanguagesOnSettings: fSaveHelper('AllowLanguagesOnSettings'), - - newMoveToFolder: fSaveHelper('NewMoveToFolder') + capaUserBackground: fSaveHelper('CapaUserBackground') }); } diff --git a/dev/Settings/Admin/Login.js b/dev/Settings/Admin/Login.js index fba0b4598..c48335583 100644 --- a/dev/Settings/Admin/Login.js +++ b/dev/Settings/Admin/Login.js @@ -1,41 +1,9 @@ -import { Settings, SettingsGet } from 'Common/Globals'; -import { addObservablesTo, addSubscribablesTo } from 'External/ko'; import { AbstractViewSettings } from 'Knoin/AbstractViews'; -import Remote from 'Remote/Admin/Fetch'; export class LoginAdminSettings extends AbstractViewSettings { constructor() { super(); - this.addSetting('LoginDefaultDomain'); - - addObservablesTo(this, { - determineUserLanguage: !!SettingsGet('DetermineUserLanguage'), - determineUserDomain: !!SettingsGet('DetermineUserDomain'), - allowLanguagesOnLogin: !!SettingsGet('AllowLanguagesOnLogin'), - hideSubmitButton: !!Settings.app('hideSubmitButton'), - }); - - addSubscribablesTo(this, { - determineUserLanguage: value => - Remote.saveConfig({ - DetermineUserLanguage: value ? 1 : 0 - }), - - determineUserDomain: value => - Remote.saveConfig({ - DetermineUserDomain: value ? 1 : 0 - }), - - allowLanguagesOnLogin: value => - Remote.saveConfig({ - AllowLanguagesOnLogin: value ? 1 : 0 - }), - - hideSubmitButton: value => - Remote.saveConfig({ - hideSubmitButton: value ? 1 : 0 - }) - }); + this.addSettings(['DetermineUserLanguage','DetermineUserDomain','AllowLanguagesOnLogin','hideSubmitButton']); } } diff --git a/dev/Settings/Admin/Packages.js b/dev/Settings/Admin/Packages.js index 6c1d9b58c..e6d139fce 100644 --- a/dev/Settings/Admin/Packages.js +++ b/dev/Settings/Admin/Packages.js @@ -8,14 +8,17 @@ import Remote from 'Remote/Admin/Fetch'; import { showScreenPopup } from 'Knoin/Knoin'; import { PluginPopupView } from 'View/Popup/Plugin'; -import { SettingsGet } from 'Common/Globals'; import { addObservablesTo, addComputablesTo } from 'External/ko'; +import { AbstractViewSettings } from 'Knoin/AbstractViews'; -export class PackagesAdminSettings /*extends AbstractViewSettings*/ { +export class PackagesAdminSettings extends AbstractViewSettings { constructor() { + super(); + + this.addSettings(['EnabledPlugins']); + addObservablesTo(this, { - packagesError: '', - enabledPlugins: !!SettingsGet('EnabledPlugins') + packagesError: '' }); this.packages = PackageAdminStore; @@ -27,12 +30,6 @@ export class PackagesAdminSettings /*extends AbstractViewSettings*/ { visibility: () => (PackageAdminStore.loading() ? 'visible' : 'hidden') }); - - this.enabledPlugins.subscribe(value => - Remote.saveConfig({ - EnabledPlugins: value ? 1 : 0 - }) - ); } onShow() { diff --git a/dev/Settings/Admin/Security.js b/dev/Settings/Admin/Security.js index 50880c7ca..b3b7427f3 100644 --- a/dev/Settings/Admin/Security.js +++ b/dev/Settings/Admin/Security.js @@ -5,17 +5,17 @@ import { addObservablesTo, addSubscribablesTo } from 'External/ko'; import Remote from 'Remote/Admin/Fetch'; import { decorateKoCommands } from 'Knoin/Knoin'; +import { AbstractViewSettings } from 'Knoin/AbstractViews'; -export class SecurityAdminSettings /*extends AbstractViewSettings*/ { +export class SecurityAdminSettings extends AbstractViewSettings { constructor() { + super(); + + this.addSettings(['UseLocalProxyForExternalImages','VerifySslCertificate','AllowSelfSigned']); + this.weakPassword = rl.app.weakPassword; addObservablesTo(this, { - useLocalProxyForExternalImages: !!SettingsGet('UseLocalProxyForExternalImages'), - - verifySslCertificate: !!SettingsGet('VerifySslCertificate'), - allowSelfSigned: !!SettingsGet('AllowSelfSigned'), - adminLogin: SettingsGet('AdminLogin'), adminLoginError: false, adminPassword: '', @@ -30,6 +30,12 @@ export class SecurityAdminSettings /*extends AbstractViewSettings*/ { capaOpenPGP: SettingsCapa(Capa.OpenPGP) }); + const reset = () => { + this.adminPasswordUpdateError(false); + this.adminPasswordUpdateSuccess(false); + this.adminPasswordNewError(false); + }; + addSubscribablesTo(this, { adminPassword: () => { this.adminPasswordUpdateError(false); @@ -38,39 +44,11 @@ export class SecurityAdminSettings /*extends AbstractViewSettings*/ { adminLogin: () => this.adminLoginError(false), - adminPasswordNew: () => { - this.adminPasswordUpdateError(false); - this.adminPasswordUpdateSuccess(false); - this.adminPasswordNewError(false); - }, + adminPasswordNew: reset, - adminPasswordNew2: () => { - this.adminPasswordUpdateError(false); - this.adminPasswordUpdateSuccess(false); - this.adminPasswordNewError(false); - }, + adminPasswordNew2: reset, - capaOpenPGP: value => - Remote.saveConfig({ - CapaOpenPGP: value ? 1 : 0 - }), - - useLocalProxyForExternalImages: value => - Remote.saveConfig({ - UseLocalProxyForExternalImages: value ? 1 : 0 - }), - - verifySslCertificate: value => { - value || this.allowSelfSigned(true); - Remote.saveConfig({ - VerifySslCertificate: value ? 1 : 0 - }); - }, - - allowSelfSigned: value => - Remote.saveConfig({ - AllowSelfSigned: value ? 1 : 0 - }) + capaOpenPGP: value => Remote.saveSetting('CapaOpenPGP', value) }); decorateKoCommands(this, { diff --git a/dev/Settings/User/Contacts.js b/dev/Settings/User/Contacts.js index b3a7dd71f..74cdc1f93 100644 --- a/dev/Settings/User/Contacts.js +++ b/dev/Settings/User/Contacts.js @@ -26,9 +26,7 @@ export class ContactsUserSettings /*extends AbstractViewSettings*/ { .extend({ debounce: 500 }); this.contactsAutosave.subscribe(value => - Remote.saveSettings(null, { - ContactsAutosave: value ? 1 : 0 - }) + Remote.saveSettings(null, { ContactsAutosave: value }) ); this.saveTrigger.subscribe(() => diff --git a/dev/Settings/User/General.js b/dev/Settings/User/General.js index 879cb245d..26207fe62 100644 --- a/dev/Settings/User/General.js +++ b/dev/Settings/User/General.js @@ -35,11 +35,11 @@ export class GeneralUserSettings extends AbstractViewSettings { this.editorDefaultType = SettingsUserStore.editorDefaultType; this.layout = SettingsUserStore.layout; - this.enableSoundNotification = NotificationUserStore.enableSoundNotification; + this.soundNotification = NotificationUserStore.enableSoundNotification; this.notificationSound = ko.observable(SettingsGet('NotificationSound')); this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds')); - this.enableDesktopNotification = NotificationUserStore.enableDesktopNotification; + this.desktopNotification = NotificationUserStore.enableDesktopNotification; this.isDesktopNotificationAllowed = NotificationUserStore.isDesktopNotificationAllowed; this.viewHTML = SettingsUserStore.viewHTML; @@ -93,11 +93,13 @@ export class GeneralUserSettings extends AbstractViewSettings { this.addSetting('MessagesPerPage'); this.addSetting('Layout', () => MessagelistUserStore([])); + this.addSettings(['ViewHTML', 'ShowImages', 'UseCheckboxesInList', 'ReplySameFolder', + 'DesktopNotifications', 'SoundNotification']); + const fReloadLanguageHelper = (saveSettingsStep) => () => { this.languageTrigger(saveSettingsStep); setTimeout(() => this.languageTrigger(SaveSettingsStep.Idle), 1000); - }, - fSaveHelper = key => value => Remote.saveSetting(key, value); + }; addSubscribablesTo(this, { language: value => { @@ -107,9 +109,6 @@ export class GeneralUserSettings extends AbstractViewSettings { .then(() => Remote.saveSetting('Language', value)); }, - viewHTML: fSaveHelper('ViewHTML'), - showImages: fSaveHelper('ShowImages'), - removeColors: value => { let dom = MessageUserStore.bodiesDom(); if (dom) { @@ -118,18 +117,11 @@ export class GeneralUserSettings extends AbstractViewSettings { Remote.saveSetting('RemoveColors', value); }, - useCheckboxesInList: fSaveHelper('UseCheckboxesInList'), - - enableDesktopNotification: fSaveHelper('DesktopNotifications'), - - enableSoundNotification: fSaveHelper('SoundNotification'), notificationSound: value => { Remote.saveSetting('NotificationSound', value); Settings.set('NotificationSound', value); }, - replySameFolder: fSaveHelper('ReplySameFolder'), - useThreads: value => { MessagelistUserStore([]); Remote.saveSetting('UseThreads', value); diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php index d69bf12e6..d8dabeb19 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Actions/Admin.php @@ -72,29 +72,18 @@ trait Admin return Utils::EncodeKeyValuesQ(array('token', $sRand)); } - private function setCapaFromParams(\RainLoop\Config\Application $oConfig, string $sParamName, string $sCapa) : void + public function DoAdminClearCache() : array { - switch ($sCapa) - { - case Capa::ADDITIONAL_ACCOUNTS: - $this->setConfigFromParams($oConfig, $sParamName, 'webmail', 'allow_additional_accounts', 'bool'); - break; - case Capa::IDENTITIES: - $this->setConfigFromParams($oConfig, $sParamName, 'webmail', 'allow_additional_identities', 'bool'); - break; - case Capa::ATTACHMENT_THUMBNAILS: - $this->setConfigFromParams($oConfig, $sParamName, 'interface', 'show_attachment_thumbnail', 'bool'); - break; - case Capa::THEMES: - $this->setConfigFromParams($oConfig, $sParamName, 'webmail', 'allow_themes', 'bool'); - break; - case Capa::USER_BACKGROUND: - $this->setConfigFromParams($oConfig, $sParamName, 'webmail', 'allow_user_background', 'bool'); - break; - case Capa::OPEN_PGP: - $this->setConfigFromParams($oConfig, $sParamName, 'security', 'openpgp', 'bool'); - break; + $this->Cacher()->GC(1); + if (\is_dir(APP_PRIVATE_DATA . 'cache')) { + foreach (new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator(APP_PRIVATE_DATA.'cache', \FilesystemIterator::SKIP_DOTS), + \RecursiveIteratorIterator::CHILD_FIRST) as $sName) { + $sName->isDir() ? \rmdir($sName) : \unlink($sName); + } + \clearstatcache(); } + return $this->TrueResponse(__FUNCTION__); } public function DoAdminSettingsGet() : array @@ -173,12 +162,12 @@ trait Admin return \RainLoop\Providers\AddressBook\PdoAddressBook::validPdoType($sType); }); - $this->setCapaFromParams($oConfig, 'CapaAdditionalAccounts', Capa::ADDITIONAL_ACCOUNTS); - $this->setCapaFromParams($oConfig, 'CapaIdentities', Capa::IDENTITIES); - $this->setCapaFromParams($oConfig, 'CapaOpenPGP', Capa::OPEN_PGP); - $this->setCapaFromParams($oConfig, 'CapaThemes', Capa::THEMES); - $this->setCapaFromParams($oConfig, 'CapaUserBackground', Capa::USER_BACKGROUND); - $this->setCapaFromParams($oConfig, 'CapaAttachmentThumbnails', Capa::ATTACHMENT_THUMBNAILS); + $this->setConfigFromParams($oConfig, 'CapaAdditionalAccounts', 'webmail', 'allow_additional_accounts', 'bool'); + $this->setConfigFromParams($oConfig, 'CapaIdentities', 'webmail', 'allow_additional_identities', 'bool'); + $this->setConfigFromParams($oConfig, 'CapaAttachmentThumbnails', 'interface', 'show_attachment_thumbnail', 'bool'); + $this->setConfigFromParams($oConfig, 'CapaThemes', 'webmail', 'allow_themes', 'bool'); + $this->setConfigFromParams($oConfig, 'CapaUserBackground', 'webmail', 'allow_user_background', 'bool'); + $this->setConfigFromParams($oConfig, 'CapaOpenPGP', 'security', 'openpgp', 'bool'); $this->setConfigFromParams($oConfig, 'DetermineUserLanguage', 'login', 'determine_user_language', 'bool'); $this->setConfigFromParams($oConfig, 'DetermineUserDomain', 'login', 'determine_user_domain', 'bool'); diff --git a/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsContacts.html b/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsContacts.html index 12b3ff55c..a040100a9 100644 --- a/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsContacts.html +++ b/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsContacts.html @@ -10,7 +10,7 @@