From 9158fcd0eb16187b23306d667627a98c72dccf13 Mon Sep 17 00:00:00 2001 From: djmaze Date: Thu, 23 Sep 2021 14:24:06 +0200 Subject: [PATCH] Improve AppUser.bootstart --- dev/App/User.js | 125 ++++++++++---------------------- dev/Common/Momentor.js | 17 +++-- dev/Stores/User/Account.js | 6 -- dev/Stores/User/Contact.js | 43 ++++++++--- dev/Stores/User/Notification.js | 6 -- dev/View/Popup/Contacts.js | 2 +- 6 files changed, 81 insertions(+), 118 deletions(-) diff --git a/dev/App/User.js b/dev/App/User.js index 884245681..a07425f92 100644 --- a/dev/App/User.js +++ b/dev/App/User.js @@ -183,33 +183,6 @@ class AppUser extends AbstractApp { Remote.messageList(null, {Folder: getFolderInboxName()}, true); } - /** - * @param {Function} fResultFunc - * @returns {boolean} - */ - contactsSync(fResultFunc) { - if ( - ContactUserStore.importing() || - ContactUserStore.syncing() || - !ContactUserStore.enableSync() || - !ContactUserStore.allowSync() - ) { - return false; - } - - ContactUserStore.syncing(true); - - Remote.contactsSync((sResult, oData) => { - ContactUserStore.syncing(false); - - if (fResultFunc) { - fResultFunc(sResult, oData); - } - }); - - return true; - } - messagesMoveTrigger() { const sTrashFolder = FolderUserStore.trashFolder(), sSpamFolder = FolderUserStore.spamFolder(); @@ -824,7 +797,7 @@ class AppUser extends AbstractApp { } logout() { - Remote.logout(() => rl.logoutReload((SettingsGet('ParentEmail')||{length:0}).length)); + Remote.logout(() => rl.logoutReload(!!SettingsGet('ParentEmail'))); } bootstart() { @@ -838,57 +811,20 @@ class AppUser extends AbstractApp { } }, {capture: true}); - NotificationUserStore.populate(); - AccountUserStore.populate(); - ContactUserStore.populate(); - - let contactsSyncInterval = pInt(SettingsGet('ContactsSyncInterval')); - - const startupUrl = pString(SettingsGet('StartupUrl')); - - rl.setWindowTitle(); if (SettingsGet('Auth')) { rl.setWindowTitle(i18n('GLOBAL/LOADING')); + NotificationUserStore.enableSoundNotification(!!SettingsGet('SoundNotification')); + NotificationUserStore.enableDesktopNotification(!!SettingsGet('DesktopNotifications')); + + AccountUserStore.email(SettingsGet('Email')); + AccountUserStore.parentEmail(SettingsGet('ParentEmail')); + this.foldersReload(value => { try { if (value) { - if (startupUrl) { - rl.route.setHash(root(startupUrl), true); - } - - if (window.crypto && crypto.getRandomValues && Settings.capa(Capa.OpenPGP)) { - const openpgpCallback = () => { - if (!window.openpgp) { - return false; - } - PgpUserStore.openpgp = openpgp; - - if (window.Worker) { - try { - PgpUserStore.openpgp.initWorker({ path: openPgpWorkerJs() }); - } catch (e) { - console.error(e); - } - } - - PgpUserStore.openpgpKeyring = new openpgp.Keyring(); - PgpUserStore.capaOpenPGP(true); - - this.reloadOpenPgpKeys(); - - return true; - }; - - if (!openpgpCallback()) { - const script = createElement('script', {src:openPgpJs()}); - script.onload = openpgpCallback; - script.onerror = () => console.error(script.src); - doc.head.append(script); - } - } else { - PgpUserStore.capaOpenPGP(false); - } + value = pString(SettingsGet('StartupUrl')); + value && rl.route.setHash(root(value), true); startScreens([ MailBoxUserScreen, @@ -907,14 +843,9 @@ class AppUser extends AbstractApp { }, refreshFolders); // Every 15 minutes - setInterval(this.quota, 900000); - // Every 20 minutes - setInterval(this.foldersReload, 1200000); + setInterval(()=>this.quota() | this.foldersReload(), 900000); - setTimeout(this.contactsSync, 10000); - contactsSyncInterval = 5 <= contactsSyncInterval ? contactsSyncInterval : 20; - contactsSyncInterval = 320 >= contactsSyncInterval ? contactsSyncInterval : 320; - setInterval(this.contactsSync, contactsSyncInterval * 60000 + 5000); + ContactUserStore.init(); this.accountsAndIdentities(true); @@ -923,15 +854,15 @@ class AppUser extends AbstractApp { if (getFolderInboxName() !== cF) { this.folderInformation(cF); } + this.quota(); this.folderInformationMultiply(true); }, 1000); - setTimeout(this.quota, 5000); setTimeout(() => Remote.appDelayStart(()=>0), 35000); // When auto-login is active if ( - !!SettingsGet('AccountSignMe') && + SettingsGet('AccountSignMe') && navigator.registerProtocolHandler ) { setTimeout(() => { @@ -943,9 +874,8 @@ class AppUser extends AbstractApp { ); } catch (e) {} // eslint-disable-line no-empty - if (SettingsGet('MailToEmail')) { - mailToHelper(SettingsGet('MailToEmail')); - } + value = SettingsGet('MailToEmail'); + value && mailToHelper(value); }, 500); } @@ -955,6 +885,27 @@ class AppUser extends AbstractApp { SettingsUserStore.delayLogout(); setTimeout(() => this.initVerticalLayoutResizer(), 1); + + setInterval(this.reloadTime(), 60000); + + if (window.crypto && crypto.getRandomValues && Settings.capa(Capa.OpenPGP)) { + const script = createElement('script', {src:openPgpJs()}); + script.onload = () => { + PgpUserStore.openpgp = openpgp; + if (window.Worker) { + try { + PgpUserStore.openpgp.initWorker({ path: openPgpWorkerJs() }); + } catch (e) { + console.error(e); + } + } + PgpUserStore.openpgpKeyring = new openpgp.Keyring(); + PgpUserStore.capaOpenPGP(true); + this.reloadOpenPgpKeys(); + }; + script.onerror = () => console.error(script.src); + doc.head.append(script); + } } else { this.logout(); } @@ -967,14 +918,12 @@ class AppUser extends AbstractApp { startScreens([LoginUserScreen]); this.hideLoading(); } - - setInterval(this.reloadTime(), 60000); } reloadTime() { setTimeout(() => - doc.querySelectorAll('[data-bind*="moment:"]').forEach(element => timeToNode(element)) + doc.querySelectorAll('time').forEach(element => timeToNode(element)) , 1) } diff --git a/dev/Common/Momentor.js b/dev/Common/Momentor.js index aa6efed35..a6c970130 100644 --- a/dev/Common/Momentor.js +++ b/dev/Common/Momentor.js @@ -34,18 +34,19 @@ export function timestampToString(timeStampInUTC, formatStr) { export function timeToNode(element, time) { try { - time = time || (Date.parse(element.dateTime) / 1000); if (time) { element.dateTime = (new Date(time * 1000)).format('Y-m-d\\TH:i:s'); + } else { + time = Date.parse(element.dateTime) / 1000; + } - let key = element.dataset.momentFormat; - if (key) { - element.textContent = timestampToString(time, key); - } + let key = element.dataset.momentFormat; + if (key) { + element.textContent = timestampToString(time, key); + } - if ((key = element.dataset.momentFormatTitle)) { - element.title = timestampToString(time, key); - } + if ((key = element.dataset.momentFormatTitle)) { + element.title = timestampToString(time, key); } } catch (e) { // prevent knockout crashes diff --git a/dev/Stores/User/Account.js b/dev/Stores/User/Account.js index d1f0d3913..a8ce79381 100644 --- a/dev/Stores/User/Account.js +++ b/dev/Stores/User/Account.js @@ -1,5 +1,4 @@ import ko from 'ko'; -import { SettingsGet } from 'Common/Globals'; import { addObservablesTo } from 'Common/Utils'; export const AccountUserStore = { @@ -18,11 +17,6 @@ export const AccountUserStore = { // }); // return result; // }), - - populate: () => { - AccountUserStore.email(SettingsGet('Email')); - AccountUserStore.parentEmail(SettingsGet('ParentEmail')); - } }; addObservablesTo(AccountUserStore, { diff --git a/dev/Stores/User/Contact.js b/dev/Stores/User/Contact.js index e5f0fedfc..1a929db93 100644 --- a/dev/Stores/User/Contact.js +++ b/dev/Stores/User/Contact.js @@ -1,6 +1,7 @@ import ko from 'ko'; import { SettingsGet } from 'Common/Globals'; -import { addObservablesTo } from 'Common/Utils'; +import { pInt, addObservablesTo } from 'Common/Utils'; +import Remote from 'Remote/User/Fetch'; export const ContactUserStore = ko.observableArray(); @@ -9,18 +10,42 @@ ContactUserStore.importing = ko.observable(false).extend({ debounce: 200 }); ContactUserStore.syncing = ko.observable(false).extend({ debounce: 200 }); addObservablesTo(ContactUserStore, { - allowSync: false, + allowSync: false, // Admin setting enableSync: false, syncUrl: '', syncUser: '', syncPass: '' }); -ContactUserStore.populate = function() { - this.allowSync(!!SettingsGet('ContactsSyncIsAllowed')); - this.enableSync(!!SettingsGet('EnableContactsSync')); - - this.syncUrl(SettingsGet('ContactsSyncUrl')); - this.syncUser(SettingsGet('ContactsSyncUser')); - this.syncPass(SettingsGet('ContactsSyncPassword')); +/** + * @param {Function} fResultFunc + * @returns {void} + */ +ContactUserStore.sync = fResultFunc => { + if (ContactUserStore.enableSync() + && !ContactUserStore.importing() + && !ContactUserStore.syncing() + ) { + ContactUserStore.syncing(true); + Remote.contactsSync((sResult, oData) => { + ContactUserStore.syncing(false); + fResultFunc && fResultFunc(sResult, oData); + }); + } +}; + +ContactUserStore.init = () => { + let value = !!SettingsGet('ContactsSyncIsAllowed'); + ContactUserStore.allowSync(value); + if (value) { + ContactUserStore.enableSync(!!SettingsGet('EnableContactsSync')); + ContactUserStore.syncUrl(SettingsGet('ContactsSyncUrl')); + ContactUserStore.syncUser(SettingsGet('ContactsSyncUser')); + ContactUserStore.syncPass(SettingsGet('ContactsSyncPassword')); + setTimeout(ContactUserStore.sync, 10000); + value = pInt(SettingsGet('ContactsSyncInterval')); + value = 5 <= value ? value : 20; + value = 320 >= value ? value : 320; + setInterval(ContactUserStore.sync, value * 60000 + 5000); + } }; diff --git a/dev/Stores/User/Notification.js b/dev/Stores/User/Notification.js index 3a0b79811..051c336c4 100644 --- a/dev/Stores/User/Notification.js +++ b/dev/Stores/User/Notification.js @@ -1,7 +1,6 @@ import ko from 'ko'; import { SMAudio } from 'Common/Audio'; -import { SettingsGet } from 'Common/Globals'; import * as Links from 'Common/Links'; /** @@ -101,9 +100,4 @@ export const NotificationUserStore = new class { } } } - - populate() { - this.enableSoundNotification(!!SettingsGet('SoundNotification')); - this.enableDesktopNotification(!!SettingsGet('DesktopNotifications')); - } }; diff --git a/dev/View/Popup/Contacts.js b/dev/View/Popup/Contacts.js index 427ba6f41..62f8bdd64 100644 --- a/dev/View/Popup/Contacts.js +++ b/dev/View/Popup/Contacts.js @@ -251,7 +251,7 @@ class ContactsPopupView extends AbstractViewPopup { } syncCommand() { - rl.app.contactsSync(iError => { + ContactUserStore.sync(iError => { iError && alert(getNotification(iError)); this.reloadContactList(true);