snappymail/dev/App/User.js

300 lines
8.1 KiB
JavaScript
Raw Permalink Normal View History

2021-01-26 18:46:30 +08:00
import 'External/User/ko';
2022-06-08 00:46:06 +08:00
import { SMAudio } from 'Common/Audio';
import { pInt } from 'Common/Utils';
import { mailToHelper, setLayoutResizer, dropdownsDetectVisibility, loadAccountsAndIdentities } from 'Common/UtilsUser';
2016-06-07 05:57:52 +08:00
2021-01-25 05:58:06 +08:00
import {
2019-07-05 03:19:24 +08:00
FolderType,
2022-03-14 22:42:05 +08:00
ClientSideKeyNameFolderListSize
2021-01-25 05:58:06 +08:00
} from 'Common/EnumsUser';
2016-06-07 05:57:52 +08:00
import {
doc,
elementById,
$htmlCL,
Settings,
2021-03-10 18:44:48 +08:00
SettingsGet,
leftPanelDisabled,
addEventsListener,
addShortcut
} from 'Common/Globals';
2016-06-07 05:57:52 +08:00
2019-07-05 03:19:24 +08:00
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
2016-08-22 05:30:34 +08:00
import {
2019-07-05 03:19:24 +08:00
getFolderInboxName,
getFolderFromCacheList
2016-08-22 05:30:34 +08:00
} from 'Common/Cache';
import { i18n, reloadTime } from 'Common/Translator';
2015-11-19 01:32:29 +08:00
import { SettingsUserStore } from 'Stores/User/Settings';
import { NotificationUserStore } from 'Stores/User/Notification';
import { AccountUserStore } from 'Stores/User/Account';
import { ContactUserStore } from 'Stores/User/Contact';
import { FolderUserStore } from 'Stores/User/Folder';
import { PgpUserStore } from 'Stores/User/Pgp';
2024-02-21 01:29:35 +08:00
import { SMimeUserStore } from 'Stores/User/SMime';
import { MessagelistUserStore } from 'Stores/User/Messagelist';
2023-03-18 06:20:42 +08:00
import { ThemeStore, initThemes } from 'Stores/Theme';
2022-03-31 23:39:53 +08:00
import { LanguageStore } from 'Stores/Language';
import { MessageUserStore } from 'Stores/User/Message';
2015-11-19 01:32:29 +08:00
import Remote from 'Remote/User/Fetch';
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
import { LoginUserScreen } from 'Screen/User/Login';
import { MailBoxUserScreen } from 'Screen/User/MailBox';
import { SettingsUserScreen } from 'Screen/User/Settings';
2016-07-07 07:11:13 +08:00
import { startScreens, showScreenPopup, arePopupsVisible } from 'Knoin/Knoin';
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
import { AbstractApp } from 'App/Abstract';
2015-11-19 01:32:29 +08:00
import { ComposePopupView } from 'View/Popup/Compose';
import { FolderSystemPopupView } from 'View/Popup/FolderSystem';
import { AskPopupView } from 'View/Popup/Ask';
2022-02-24 06:11:12 +08:00
import {
2022-08-23 03:52:00 +08:00
folderInformation,
2022-02-24 06:11:12 +08:00
folderInformationMultiply,
setRefreshFoldersInterval
2022-02-24 06:11:12 +08:00
} from 'Common/Folders';
import { loadFolders } from 'Model/FolderCollection';
2022-02-08 00:27:25 +08:00
2022-03-31 23:39:53 +08:00
export class AppUser extends AbstractApp {
2016-07-16 05:29:42 +08:00
constructor() {
2015-11-19 01:32:29 +08:00
super(Remote);
// wakeUp
2020-08-14 04:58:41 +08:00
const interval = 3600000; // 60m
let lastTime = Date.now();
setInterval(() => {
2020-09-04 23:07:35 +08:00
const currentTime = Date.now();
2022-10-10 19:52:56 +08:00
(currentTime > (lastTime + interval + 1000))
&& Remote.request('Version',
iError => (100 < iError) && location.reload(),
{ version: Settings.app('version') }
);
lastTime = currentTime;
}, interval);
2014-08-22 23:08:56 +08:00
2022-09-28 05:55:29 +08:00
addEventsListener(doc, ['keydown','keyup'], (ev=>$htmlCL.toggle('rl-ctrl-key-pressed', ev.ctrlKey)).debounce(500));
addShortcut('escape,enter', '', dropdownsDetectVisibility);
2022-03-08 17:52:08 +08:00
addEventListener('click', dropdownsDetectVisibility);
2022-05-18 23:15:31 +08:00
this.folderList = FolderUserStore.folderList;
2023-04-04 17:57:52 +08:00
this.messageList = MessagelistUserStore;
this.ask = AskPopupView;
}
2014-08-20 23:03:12 +08:00
/**
* @param {number} iFolderType
2021-11-30 17:19:43 +08:00
* @param {string} sFromFolderFullName
* @param {Set} oUids
* @param {boolean=} bDelete = false
2014-08-20 23:03:12 +08:00
*/
moveMessagesToFolderType(iFolderType, sFromFolderFullName, oUids, bDelete) {
2019-07-05 03:19:24 +08:00
let oMoveFolder = null,
nSetSystemFoldersNotification = 0;
2014-03-20 00:18:28 +08:00
switch (iFolderType) {
case FolderType.Junk:
oMoveFolder = getFolderFromCacheList(FolderUserStore.spamFolder());
nSetSystemFoldersNotification = iFolderType;
2022-09-28 05:55:29 +08:00
bDelete = bDelete || UNUSED_OPTION_VALUE === FolderUserStore.spamFolder();
2014-08-20 23:03:12 +08:00
break;
case FolderType.Inbox:
2016-08-22 05:30:34 +08:00
oMoveFolder = getFolderFromCacheList(getFolderInboxName());
2014-08-20 23:03:12 +08:00
break;
2016-06-07 05:57:52 +08:00
case FolderType.Trash:
oMoveFolder = getFolderFromCacheList(FolderUserStore.trashFolder());
nSetSystemFoldersNotification = iFolderType;
2022-09-28 05:55:29 +08:00
bDelete = bDelete || UNUSED_OPTION_VALUE === FolderUserStore.trashFolder()
|| sFromFolderFullName === FolderUserStore.spamFolder()
|| sFromFolderFullName === FolderUserStore.trashFolder();
2014-08-20 23:03:12 +08:00
break;
2016-06-07 05:57:52 +08:00
case FolderType.Archive:
oMoveFolder = getFolderFromCacheList(FolderUserStore.archiveFolder());
nSetSystemFoldersNotification = iFolderType;
2022-09-28 05:55:29 +08:00
bDelete = bDelete || UNUSED_OPTION_VALUE === FolderUserStore.archiveFolder();
2014-08-20 23:03:12 +08:00
break;
2016-06-30 08:02:45 +08:00
// no default
2014-08-20 23:03:12 +08:00
}
if (bDelete) {
showScreenPopup(AskPopupView, [
2019-07-05 03:19:24 +08:00
i18n('POPUPS_ASK/DESC_WANT_DELETE_MESSAGES'),
() => {
MessagelistUserStore.moveMessages(sFromFolderFullName, oUids);
2019-07-05 03:19:24 +08:00
}
]);
} else if (oMoveFolder) {
MessagelistUserStore.moveMessages(sFromFolderFullName, oUids, oMoveFolder.fullName);
} else {
showScreenPopup(FolderSystemPopupView, [nSetSystemFoldersNotification]);
2014-08-20 23:03:12 +08:00
}
2015-11-19 01:32:29 +08:00
}
2014-08-20 23:03:12 +08:00
/**
2015-11-19 01:32:29 +08:00
* @param {string} folder
* @param {Array=} list = []
2014-08-20 23:03:12 +08:00
*/
2015-11-19 01:32:29 +08:00
folderInformation(folder, list) {
2022-08-23 03:52:00 +08:00
folderInformation(folder, list);
2016-04-21 01:12:51 +08:00
}
2015-11-19 01:32:29 +08:00
logout() {
localStorage.removeItem('register_protocol_offered');
2022-10-19 05:32:47 +08:00
Remote.request('Logout', () => rl.logoutReload(Settings.app('customLogoutLink')));
2016-04-21 01:12:51 +08:00
}
2015-02-19 03:52:52 +08:00
2015-11-19 01:32:29 +08:00
bootstart() {
super.bootstart();
2021-08-31 22:21:40 +08:00
addEventListener('beforeunload', event => {
if (arePopupsVisible() || (!SettingsUserStore.usePreviewPane() && MessageUserStore.message())) {
2021-08-31 22:21:40 +08:00
event.preventDefault();
return event.returnValue = i18n('POPUPS_ASK/EXIT_ARE_YOU_SURE');
2021-08-31 22:21:40 +08:00
}
}, {capture: true});
}
refresh() {
2023-03-18 06:20:42 +08:00
initThemes();
LanguageStore.language(SettingsGet('language'));
this.start();
}
start() {
2021-03-10 18:44:48 +08:00
if (SettingsGet('Auth')) {
2022-11-14 16:15:25 +08:00
rl.setTitle(i18n('GLOBAL/LOADING'));
2014-08-20 23:03:12 +08:00
2022-06-08 00:46:06 +08:00
SMAudio.notifications(!!SettingsGet('SoundNotification'));
NotificationUserStore.enabled(!!SettingsGet('DesktopNotifications'));
2021-09-23 20:24:06 +08:00
AccountUserStore.email(SettingsGet('Email'));
SettingsUserStore.init();
ContactUserStore.init();
loadFolders(value => {
2021-04-13 01:15:33 +08:00
try {
if (value) {
startScreens([
MailBoxUserScreen,
2021-09-23 17:05:19 +08:00
SettingsUserScreen
2021-04-13 01:15:33 +08:00
]);
2023-02-01 19:21:31 +08:00
setRefreshFoldersInterval(pInt(SettingsGet('CheckMailInterval')));
2014-10-29 06:05:50 +08:00
loadAccountsAndIdentities();
2015-02-19 03:52:52 +08:00
2021-04-13 01:15:33 +08:00
setTimeout(() => {
2021-11-30 17:19:43 +08:00
const cF = FolderUserStore.currentFolderFullName();
2022-10-10 19:52:56 +08:00
getFolderInboxName() === cF || folderInformation(cF);
FolderUserStore.hasCapability('LIST-STATUS') || folderInformationMultiply(true);
2021-04-13 01:15:33 +08:00
}, 1000);
setTimeout(() => Remote.request('AppDelayStart'), 35000);
2021-04-13 01:15:33 +08:00
2022-01-12 19:11:37 +08:00
// add pointermove ?
addEventsListener(doc, ['touchstart','mousemove','keydown'], SettingsUserStore.delayLogout, {passive:true});
2021-04-13 01:15:33 +08:00
SettingsUserStore.delayLogout();
2021-03-18 23:12:24 +08:00
// initLeftSideLayoutResizer
setTimeout(() => {
const left = elementById('rl-left'),
fToggle = () =>
setLayoutResizer(left, ClientSideKeyNameFolderListSize,
(ThemeStore.isMobile() || leftPanelDisabled()) ? 0 : 'Width');
if (left) {
fToggle();
leftPanelDisabled.subscribe(fToggle);
}
}, 1);
2021-09-23 20:24:06 +08:00
setInterval(reloadTime, 60000);
2021-09-23 20:24:06 +08:00
PgpUserStore.init();
2024-02-21 01:29:35 +08:00
SMimeUserStore.loadCertificates();
setTimeout(() => mailToHelper(SettingsGet('mailToEmail')), 500);
if (!localStorage.getItem('register_protocol_offered')) {
// When auto-login is active
navigator.registerProtocolHandler?.(
'mailto',
location.protocol + '//' + location.host + location.pathname + '?mailto&to=%s',
(SettingsGet('title') || 'SnappyMail')
);
localStorage.setItem('register_protocol_offered', '1');
}
2022-10-10 19:52:56 +08:00
2021-04-13 01:15:33 +08:00
} else {
this.logout();
}
2021-04-13 01:15:33 +08:00
} catch (e) {
console.error(e);
}
});
2019-07-05 03:19:24 +08:00
} else {
startScreens([LoginUserScreen]);
2014-08-20 23:03:12 +08:00
}
2021-07-19 20:26:32 +08:00
}
showMessageComposer(params = [])
{
showScreenPopup(ComposePopupView, params);
}
2015-11-19 01:32:29 +08:00
}
AskPopupView.password = function(sAskDesc, btnText, ask) {
return new Promise(resolve => {
this.showModal([
sAskDesc,
view => resolve({
password:view.passphrase(),
username:/*ask & 2 ? */view.username(),
remember:/*ask & 4 ? */view.remember()
}),
() => resolve(null),
true,
ask || 1,
btnText
]);
});
};
AskPopupView.cryptkey = () => new Promise(resolve => {
const fn = () => AskPopupView.showModal([
i18n('CRYPTO/ASK_CRYPTKEY_PASS'),
view => {
let pass = view.passphrase();
if (pass) {
Remote.post('ResealCryptKey', null, {
passphrase: pass
}).then(response => {
resolve(response?.Result);
}).catch(e => {
if (111 === e.code) {
fn();
} else {
console.error(e);
resolve(null);
}
});
} else {
resolve(null);
}
},
() => resolve(null),
true,
1,
i18n('CRYPTO/DECRYPT')
]);
fn();
});