Skip to content

Commit

Permalink
Improve AppUser.bootstart
Browse files Browse the repository at this point in the history
  • Loading branch information
djmaze committed Sep 23, 2021
1 parent 3c8cdcb commit 9158fcd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 117 deletions.
125 changes: 37 additions & 88 deletions dev/App/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand All @@ -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,
Expand All @@ -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);

Expand All @@ -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(() => {
Expand All @@ -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);
}

Expand All @@ -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();
}
Expand All @@ -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)
}

Expand Down
17 changes: 9 additions & 8 deletions dev/Common/Momentor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions dev/Stores/User/Account.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ko from 'ko';
import { SettingsGet } from 'Common/Globals';
import { addObservablesTo } from 'Common/Utils';

export const AccountUserStore = {
Expand All @@ -18,11 +17,6 @@ export const AccountUserStore = {
// });
// return result;
// }),

populate: () => {
AccountUserStore.email(SettingsGet('Email'));
AccountUserStore.parentEmail(SettingsGet('ParentEmail'));
}
};

addObservablesTo(AccountUserStore, {
Expand Down
41 changes: 33 additions & 8 deletions dev/Stores/User/Contact.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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'));
/**
* @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);
});
}
};

this.syncUrl(SettingsGet('ContactsSyncUrl'));
this.syncUser(SettingsGet('ContactsSyncUser'));
this.syncPass(SettingsGet('ContactsSyncPassword'));
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);
}
};
6 changes: 0 additions & 6 deletions dev/Stores/User/Notification.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ko from 'ko';

import { SMAudio } from 'Common/Audio';
import { SettingsGet } from 'Common/Globals';
import * as Links from 'Common/Links';

/**
Expand Down Expand Up @@ -101,9 +100,4 @@ export const NotificationUserStore = new class {
}
}
}

populate() {
this.enableSoundNotification(!!SettingsGet('SoundNotification'));
this.enableDesktopNotification(!!SettingsGet('DesktopNotifications'));
}
};
2 changes: 1 addition & 1 deletion dev/View/Popup/Contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class ContactsPopupView extends AbstractViewPopup {
}

syncCommand() {
rl.app.contactsSync(iError => {
ContactUserStore.sync(iError => {
iError && alert(getNotification(iError));

this.reloadContactList(true);
Expand Down

0 comments on commit 9158fcd

Please sign in to comment.