Simplify save settings

This commit is contained in:
the-djmaze 2022-03-01 10:18:12 +01:00
parent fc60dc94fc
commit f4d98130a5
12 changed files with 66 additions and 162 deletions

View file

@ -150,6 +150,15 @@ export class AbstractViewSettings
}).debounce(999), }).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 { export class AbstractViewLogin extends AbstractViewCenter {

View file

@ -2,21 +2,13 @@ import { AbstractFetchRemote } from 'Remote/AbstractFetch';
class RemoteAdminFetch extends AbstractFetchRemote { class RemoteAdminFetch extends AbstractFetchRemote {
/**
* @param {?Function} fCallback
* @param {?} oData
*/
saveConfig(oData, fCallback) {
this.request('AdminSettingsUpdate', fCallback, oData);
}
/** /**
* @param {string} key * @param {string} key
* @param {?scalar} value * @param {?scalar} value
* @param {?Function} fCallback * @param {?Function} fCallback
*/ */
saveSetting(key, value, fCallback) { saveSetting(key, value, fCallback) {
this.saveConfig({[key]: value}, fCallback); this.request('AdminSettingsUpdate', fCallback, {[key]: value});
} }
} }

View file

@ -2,7 +2,7 @@ import ko from 'ko';
import { SettingsGet } from 'Common/Globals'; import { SettingsGet } from 'Common/Globals';
import { defaultOptionsAfterRender } from 'Common/Utils'; import { defaultOptionsAfterRender } from 'Common/Utils';
import { addObservablesTo, addSubscribablesTo } from 'External/ko'; import { addObservablesTo } from 'External/ko';
import Remote from 'Remote/Admin/Fetch'; import Remote from 'Remote/Admin/Fetch';
import { decorateKoCommands } from 'Knoin/Knoin'; import { decorateKoCommands } from 'Knoin/Knoin';
@ -22,10 +22,9 @@ export class ContactsAdminSettings extends AbstractViewSettings {
this.testContactsErrorMessage(''); this.testContactsErrorMessage('');
}); });
addObservablesTo(this, { this.addSettings(['ContactsEnable','ContactsSync']);
enableContacts: !!SettingsGet('ContactsEnable'),
contactsSync: !!SettingsGet('ContactsSync'),
addObservablesTo(this, {
testing: false, testing: false,
testContactsSuccess: false, testContactsSuccess: false,
testContactsError: false, testContactsError: false,
@ -65,18 +64,6 @@ export class ContactsAdminSettings extends AbstractViewSettings {
}) })
.extend({ notify: 'always' }); .extend({ notify: 'always' });
addSubscribablesTo(this, {
enableContacts: value =>
Remote.saveConfig({
ContactsEnable: value ? 1 : 0
}),
contactsSync: value =>
Remote.saveConfig({
ContactsSync: value ? 1 : 0
})
})
decorateKoCommands(this, { decorateKoCommands(this, {
testContactsCommand: self => self.contactsPdoDsn() && self.contactsPdoUser() testContactsCommand: self => self.contactsPdoDsn() && self.contactsPdoUser()
}); });

View file

@ -37,9 +37,9 @@ export class GeneralAdminSettings extends AbstractViewSettings {
this.theme = ThemeStore.theme; this.theme = ThemeStore.theme;
this.themes = ThemeStore.themes; this.themes = ThemeStore.themes;
this.addSettings(['AllowLanguagesOnSettings','NewMoveToFolder']);
addObservablesTo(this, { addObservablesTo(this, {
allowLanguagesOnSettings: SettingsGet('AllowLanguagesOnSettings'),
newMoveToFolder: SettingsGet('NewMoveToFolder'),
attachmentLimitTrigger: SaveSettingsStep.Idle, attachmentLimitTrigger: SaveSettingsStep.Idle,
themeTrigger: SaveSettingsStep.Idle, themeTrigger: SaveSettingsStep.Idle,
capaThemes: SettingsCapa(Capa.Themes), capaThemes: SettingsCapa(Capa.Themes),
@ -97,9 +97,7 @@ export class GeneralAdminSettings extends AbstractViewSettings {
this.languageAdminTrigger(SaveSettingsStep.Animate); this.languageAdminTrigger(SaveSettingsStep.Animate);
translatorReload(true, value) translatorReload(true, value)
.then(fReloadLanguageHelper(SaveSettingsStep.TrueResult), fReloadLanguageHelper(SaveSettingsStep.FalseResult)) .then(fReloadLanguageHelper(SaveSettingsStep.TrueResult), fReloadLanguageHelper(SaveSettingsStep.FalseResult))
.then(() => Remote.saveConfig({ .then(() => Remote.saveSetting('LanguageAdmin', value));
LanguageAdmin: value.trim()
}));
}, },
capaAdditionalAccounts: fSaveHelper('CapaAdditionalAccounts'), capaAdditionalAccounts: fSaveHelper('CapaAdditionalAccounts'),
@ -110,11 +108,7 @@ export class GeneralAdminSettings extends AbstractViewSettings {
capaThemes: fSaveHelper('CapaThemes'), capaThemes: fSaveHelper('CapaThemes'),
capaUserBackground: fSaveHelper('CapaUserBackground'), capaUserBackground: fSaveHelper('CapaUserBackground')
allowLanguagesOnSettings: fSaveHelper('AllowLanguagesOnSettings'),
newMoveToFolder: fSaveHelper('NewMoveToFolder')
}); });
} }

View file

@ -1,41 +1,9 @@
import { Settings, SettingsGet } from 'Common/Globals';
import { addObservablesTo, addSubscribablesTo } from 'External/ko';
import { AbstractViewSettings } from 'Knoin/AbstractViews'; import { AbstractViewSettings } from 'Knoin/AbstractViews';
import Remote from 'Remote/Admin/Fetch';
export class LoginAdminSettings extends AbstractViewSettings { export class LoginAdminSettings extends AbstractViewSettings {
constructor() { constructor() {
super(); super();
this.addSetting('LoginDefaultDomain'); this.addSetting('LoginDefaultDomain');
this.addSettings(['DetermineUserLanguage','DetermineUserDomain','AllowLanguagesOnLogin','hideSubmitButton']);
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
})
});
} }
} }

View file

@ -8,14 +8,17 @@ import Remote from 'Remote/Admin/Fetch';
import { showScreenPopup } from 'Knoin/Knoin'; import { showScreenPopup } from 'Knoin/Knoin';
import { PluginPopupView } from 'View/Popup/Plugin'; import { PluginPopupView } from 'View/Popup/Plugin';
import { SettingsGet } from 'Common/Globals';
import { addObservablesTo, addComputablesTo } from 'External/ko'; import { addObservablesTo, addComputablesTo } from 'External/ko';
import { AbstractViewSettings } from 'Knoin/AbstractViews';
export class PackagesAdminSettings /*extends AbstractViewSettings*/ { export class PackagesAdminSettings extends AbstractViewSettings {
constructor() { constructor() {
super();
this.addSettings(['EnabledPlugins']);
addObservablesTo(this, { addObservablesTo(this, {
packagesError: '', packagesError: ''
enabledPlugins: !!SettingsGet('EnabledPlugins')
}); });
this.packages = PackageAdminStore; this.packages = PackageAdminStore;
@ -27,12 +30,6 @@ export class PackagesAdminSettings /*extends AbstractViewSettings*/ {
visibility: () => (PackageAdminStore.loading() ? 'visible' : 'hidden') visibility: () => (PackageAdminStore.loading() ? 'visible' : 'hidden')
}); });
this.enabledPlugins.subscribe(value =>
Remote.saveConfig({
EnabledPlugins: value ? 1 : 0
})
);
} }
onShow() { onShow() {

View file

@ -5,17 +5,17 @@ import { addObservablesTo, addSubscribablesTo } from 'External/ko';
import Remote from 'Remote/Admin/Fetch'; import Remote from 'Remote/Admin/Fetch';
import { decorateKoCommands } from 'Knoin/Knoin'; import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewSettings } from 'Knoin/AbstractViews';
export class SecurityAdminSettings /*extends AbstractViewSettings*/ { export class SecurityAdminSettings extends AbstractViewSettings {
constructor() { constructor() {
super();
this.addSettings(['UseLocalProxyForExternalImages','VerifySslCertificate','AllowSelfSigned']);
this.weakPassword = rl.app.weakPassword; this.weakPassword = rl.app.weakPassword;
addObservablesTo(this, { addObservablesTo(this, {
useLocalProxyForExternalImages: !!SettingsGet('UseLocalProxyForExternalImages'),
verifySslCertificate: !!SettingsGet('VerifySslCertificate'),
allowSelfSigned: !!SettingsGet('AllowSelfSigned'),
adminLogin: SettingsGet('AdminLogin'), adminLogin: SettingsGet('AdminLogin'),
adminLoginError: false, adminLoginError: false,
adminPassword: '', adminPassword: '',
@ -30,6 +30,12 @@ export class SecurityAdminSettings /*extends AbstractViewSettings*/ {
capaOpenPGP: SettingsCapa(Capa.OpenPGP) capaOpenPGP: SettingsCapa(Capa.OpenPGP)
}); });
const reset = () => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
};
addSubscribablesTo(this, { addSubscribablesTo(this, {
adminPassword: () => { adminPassword: () => {
this.adminPasswordUpdateError(false); this.adminPasswordUpdateError(false);
@ -38,39 +44,11 @@ export class SecurityAdminSettings /*extends AbstractViewSettings*/ {
adminLogin: () => this.adminLoginError(false), adminLogin: () => this.adminLoginError(false),
adminPasswordNew: () => { adminPasswordNew: reset,
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
},
adminPasswordNew2: () => { adminPasswordNew2: reset,
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
},
capaOpenPGP: value => capaOpenPGP: value => Remote.saveSetting('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
})
}); });
decorateKoCommands(this, { decorateKoCommands(this, {

View file

@ -26,9 +26,7 @@ export class ContactsUserSettings /*extends AbstractViewSettings*/ {
.extend({ debounce: 500 }); .extend({ debounce: 500 });
this.contactsAutosave.subscribe(value => this.contactsAutosave.subscribe(value =>
Remote.saveSettings(null, { Remote.saveSettings(null, { ContactsAutosave: value })
ContactsAutosave: value ? 1 : 0
})
); );
this.saveTrigger.subscribe(() => this.saveTrigger.subscribe(() =>

View file

@ -35,11 +35,11 @@ export class GeneralUserSettings extends AbstractViewSettings {
this.editorDefaultType = SettingsUserStore.editorDefaultType; this.editorDefaultType = SettingsUserStore.editorDefaultType;
this.layout = SettingsUserStore.layout; this.layout = SettingsUserStore.layout;
this.enableSoundNotification = NotificationUserStore.enableSoundNotification; this.soundNotification = NotificationUserStore.enableSoundNotification;
this.notificationSound = ko.observable(SettingsGet('NotificationSound')); this.notificationSound = ko.observable(SettingsGet('NotificationSound'));
this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds')); this.notificationSounds = ko.observableArray(SettingsGet('NewMailSounds'));
this.enableDesktopNotification = NotificationUserStore.enableDesktopNotification; this.desktopNotification = NotificationUserStore.enableDesktopNotification;
this.isDesktopNotificationAllowed = NotificationUserStore.isDesktopNotificationAllowed; this.isDesktopNotificationAllowed = NotificationUserStore.isDesktopNotificationAllowed;
this.viewHTML = SettingsUserStore.viewHTML; this.viewHTML = SettingsUserStore.viewHTML;
@ -93,11 +93,13 @@ export class GeneralUserSettings extends AbstractViewSettings {
this.addSetting('MessagesPerPage'); this.addSetting('MessagesPerPage');
this.addSetting('Layout', () => MessagelistUserStore([])); this.addSetting('Layout', () => MessagelistUserStore([]));
this.addSettings(['ViewHTML', 'ShowImages', 'UseCheckboxesInList', 'ReplySameFolder',
'DesktopNotifications', 'SoundNotification']);
const fReloadLanguageHelper = (saveSettingsStep) => () => { const fReloadLanguageHelper = (saveSettingsStep) => () => {
this.languageTrigger(saveSettingsStep); this.languageTrigger(saveSettingsStep);
setTimeout(() => this.languageTrigger(SaveSettingsStep.Idle), 1000); setTimeout(() => this.languageTrigger(SaveSettingsStep.Idle), 1000);
}, };
fSaveHelper = key => value => Remote.saveSetting(key, value);
addSubscribablesTo(this, { addSubscribablesTo(this, {
language: value => { language: value => {
@ -107,9 +109,6 @@ export class GeneralUserSettings extends AbstractViewSettings {
.then(() => Remote.saveSetting('Language', value)); .then(() => Remote.saveSetting('Language', value));
}, },
viewHTML: fSaveHelper('ViewHTML'),
showImages: fSaveHelper('ShowImages'),
removeColors: value => { removeColors: value => {
let dom = MessageUserStore.bodiesDom(); let dom = MessageUserStore.bodiesDom();
if (dom) { if (dom) {
@ -118,18 +117,11 @@ export class GeneralUserSettings extends AbstractViewSettings {
Remote.saveSetting('RemoveColors', value); Remote.saveSetting('RemoveColors', value);
}, },
useCheckboxesInList: fSaveHelper('UseCheckboxesInList'),
enableDesktopNotification: fSaveHelper('DesktopNotifications'),
enableSoundNotification: fSaveHelper('SoundNotification'),
notificationSound: value => { notificationSound: value => {
Remote.saveSetting('NotificationSound', value); Remote.saveSetting('NotificationSound', value);
Settings.set('NotificationSound', value); Settings.set('NotificationSound', value);
}, },
replySameFolder: fSaveHelper('ReplySameFolder'),
useThreads: value => { useThreads: value => {
MessagelistUserStore([]); MessagelistUserStore([]);
Remote.saveSetting('UseThreads', value); Remote.saveSetting('UseThreads', value);

View file

@ -72,29 +72,18 @@ trait Admin
return Utils::EncodeKeyValuesQ(array('token', $sRand)); return Utils::EncodeKeyValuesQ(array('token', $sRand));
} }
private function setCapaFromParams(\RainLoop\Config\Application $oConfig, string $sParamName, string $sCapa) : void public function DoAdminClearCache() : array
{ {
switch ($sCapa) $this->Cacher()->GC(1);
{ if (\is_dir(APP_PRIVATE_DATA . 'cache')) {
case Capa::ADDITIONAL_ACCOUNTS: foreach (new \RecursiveIteratorIterator(
$this->setConfigFromParams($oConfig, $sParamName, 'webmail', 'allow_additional_accounts', 'bool'); new \RecursiveDirectoryIterator(APP_PRIVATE_DATA.'cache', \FilesystemIterator::SKIP_DOTS),
break; \RecursiveIteratorIterator::CHILD_FIRST) as $sName) {
case Capa::IDENTITIES: $sName->isDir() ? \rmdir($sName) : \unlink($sName);
$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;
} }
\clearstatcache();
}
return $this->TrueResponse(__FUNCTION__);
} }
public function DoAdminSettingsGet() : array public function DoAdminSettingsGet() : array
@ -173,12 +162,12 @@ trait Admin
return \RainLoop\Providers\AddressBook\PdoAddressBook::validPdoType($sType); return \RainLoop\Providers\AddressBook\PdoAddressBook::validPdoType($sType);
}); });
$this->setCapaFromParams($oConfig, 'CapaAdditionalAccounts', Capa::ADDITIONAL_ACCOUNTS); $this->setConfigFromParams($oConfig, 'CapaAdditionalAccounts', 'webmail', 'allow_additional_accounts', 'bool');
$this->setCapaFromParams($oConfig, 'CapaIdentities', Capa::IDENTITIES); $this->setConfigFromParams($oConfig, 'CapaIdentities', 'webmail', 'allow_additional_identities', 'bool');
$this->setCapaFromParams($oConfig, 'CapaOpenPGP', Capa::OPEN_PGP); $this->setConfigFromParams($oConfig, 'CapaAttachmentThumbnails', 'interface', 'show_attachment_thumbnail', 'bool');
$this->setCapaFromParams($oConfig, 'CapaThemes', Capa::THEMES); $this->setConfigFromParams($oConfig, 'CapaThemes', 'webmail', 'allow_themes', 'bool');
$this->setCapaFromParams($oConfig, 'CapaUserBackground', Capa::USER_BACKGROUND); $this->setConfigFromParams($oConfig, 'CapaUserBackground', 'webmail', 'allow_user_background', 'bool');
$this->setCapaFromParams($oConfig, 'CapaAttachmentThumbnails', Capa::ATTACHMENT_THUMBNAILS); $this->setConfigFromParams($oConfig, 'CapaOpenPGP', 'security', 'openpgp', 'bool');
$this->setConfigFromParams($oConfig, 'DetermineUserLanguage', 'login', 'determine_user_language', 'bool'); $this->setConfigFromParams($oConfig, 'DetermineUserLanguage', 'login', 'determine_user_language', 'bool');
$this->setConfigFromParams($oConfig, 'DetermineUserDomain', 'login', 'determine_user_domain', 'bool'); $this->setConfigFromParams($oConfig, 'DetermineUserDomain', 'login', 'determine_user_domain', 'bool');

View file

@ -10,7 +10,7 @@
<div> <div>
<div data-bind="component: { <div data-bind="component: {
name: 'Checkbox', name: 'Checkbox',
params: { value: enableContacts, label: 'TAB_CONTACTS/LABEL_ENABLE_CONTACTS' } params: { value: contactsEnable, label: 'TAB_CONTACTS/LABEL_ENABLE_CONTACTS' }
}"></div> }"></div>
<div data-bind="component: { <div data-bind="component: {
name: 'Checkbox', name: 'Checkbox',

View file

@ -116,7 +116,7 @@
name: 'Checkbox', name: 'Checkbox',
params: { params: {
label: 'SETTINGS_GENERAL/LABEL_CHROME_NOTIFICATION_DESC', label: 'SETTINGS_GENERAL/LABEL_CHROME_NOTIFICATION_DESC',
value: enableDesktopNotification, value: desktopNotification,
enable: isDesktopNotificationAllowed, enable: isDesktopNotificationAllowed,
inline: true inline: true
} }
@ -130,7 +130,7 @@
name: 'Checkbox', name: 'Checkbox',
params: { params: {
label: 'SETTINGS_GENERAL/LABEL_SOUND_NOTIFICATION', label: 'SETTINGS_GENERAL/LABEL_SOUND_NOTIFICATION',
value: enableSoundNotification, value: soundNotification,
inline: true inline: true
} }
}"></div> }"></div>