snappymail/dev/App/User.js

381 lines
11 KiB
JavaScript
Raw Normal View History

2021-01-26 18:46:30 +08:00
import 'External/User/ko';
import { isArray, pString } from 'Common/Utils';
2022-03-08 17:52:08 +08:00
import { mailToHelper, setLayoutResizer, dropdownsDetectVisibility } 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,
SetSystemFoldersNotification,
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 {
MessageFlagsCache,
2019-07-05 03:19:24 +08:00
setFolderHash,
getFolderHash,
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 { IdentityUserStore } from 'Stores/User/Identity';
import { FolderUserStore } from 'Stores/User/Folder';
import { PgpUserStore } from 'Stores/User/Pgp';
import { MessagelistUserStore } from 'Stores/User/Messagelist';
import { ThemeStore } from 'Stores/Theme';
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 { AccountModel } from 'Model/Account';
import { IdentityModel } from 'Model/Identity';
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 {
folderInformationMultiply,
refreshFoldersInterval,
messagesMoveHelper,
messagesDeleteHelper,
fetchFolderInformation
} from 'Common/Folders';
import { loadFolders } from 'Model/FolderCollection';
2022-02-08 00:27:25 +08:00
2019-07-05 03:19:24 +08:00
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();
if (currentTime > (lastTime + interval + 1000)) {
Remote.request('Version',
2022-03-03 23:28:05 +08:00
iError => (100 < iError) && (Settings.app('inIframe') ? parent : window).location.reload(),
{ Version: Settings.app('version') }
);
}
lastTime = currentTime;
}, interval);
2014-08-22 23:08:56 +08:00
const fn = (ev=>$htmlCL.toggle('rl-ctrl-key-pressed', ev.ctrlKey)).debounce(500);
addEventsListener(doc, ['keydown','keyup'], fn);
addShortcut('escape,enter', '', dropdownsDetectVisibility);
2022-03-08 17:52:08 +08:00
addEventListener('click', dropdownsDetectVisibility);
}
2014-08-20 23:03:12 +08:00
/**
* @param {number} iDeleteType
2021-11-30 17:19:43 +08:00
* @param {string} sFromFolderFullName
2014-08-20 23:03:12 +08:00
* @param {Array} aUidForRemove
* @param {boolean=} bUseFolder = true
*/
2021-11-30 17:19:43 +08:00
deleteMessagesFromFolder(iDeleteType, sFromFolderFullName, aUidForRemove, bUseFolder) {
2019-07-05 03:19:24 +08:00
let oMoveFolder = null,
2016-06-30 08:02:45 +08:00
nSetSystemFoldersNotification = null;
2014-03-20 00:18:28 +08:00
2019-07-05 03:19:24 +08:00
switch (iDeleteType) {
2016-06-07 05:57:52 +08:00
case FolderType.Spam:
oMoveFolder = getFolderFromCacheList(FolderUserStore.spamFolder());
2016-06-07 05:57:52 +08:00
nSetSystemFoldersNotification = SetSystemFoldersNotification.Spam;
2014-08-20 23:03:12 +08:00
break;
2016-06-07 05:57:52 +08:00
case FolderType.NotSpam:
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());
2016-06-07 05:57:52 +08:00
nSetSystemFoldersNotification = SetSystemFoldersNotification.Trash;
2014-08-20 23:03:12 +08:00
break;
2016-06-07 05:57:52 +08:00
case FolderType.Archive:
oMoveFolder = getFolderFromCacheList(FolderUserStore.archiveFolder());
2016-06-07 05:57:52 +08:00
nSetSystemFoldersNotification = SetSystemFoldersNotification.Archive;
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
}
bUseFolder = undefined === bUseFolder ? true : !!bUseFolder;
2019-07-05 03:19:24 +08:00
if (bUseFolder) {
if (
(FolderType.Spam === iDeleteType && UNUSED_OPTION_VALUE === FolderUserStore.spamFolder()) ||
(FolderType.Trash === iDeleteType && UNUSED_OPTION_VALUE === FolderUserStore.trashFolder()) ||
(FolderType.Archive === iDeleteType && UNUSED_OPTION_VALUE === FolderUserStore.archiveFolder())
2019-07-05 03:19:24 +08:00
) {
2014-08-20 23:03:12 +08:00
bUseFolder = false;
}
}
2014-08-20 23:03:12 +08:00
2019-07-05 03:19:24 +08:00
if (!oMoveFolder && bUseFolder) {
showScreenPopup(FolderSystemPopupView, [nSetSystemFoldersNotification]);
2019-07-05 03:19:24 +08:00
} else if (
!bUseFolder ||
(FolderType.Trash === iDeleteType &&
2021-11-30 17:19:43 +08:00
(sFromFolderFullName === FolderUserStore.spamFolder()
|| sFromFolderFullName === FolderUserStore.trashFolder()))
2019-07-05 03:19:24 +08:00
) {
showScreenPopup(AskPopupView, [
2019-07-05 03:19:24 +08:00
i18n('POPUPS_ASK/DESC_WANT_DELETE_MESSAGES'),
() => {
messagesDeleteHelper(sFromFolderFullName, aUidForRemove);
MessagelistUserStore.removeMessagesFromList(sFromFolderFullName, aUidForRemove);
2019-07-05 03:19:24 +08:00
}
]);
} else if (oMoveFolder) {
messagesMoveHelper(sFromFolderFullName, oMoveFolder.fullName, aUidForRemove);
MessagelistUserStore.removeMessagesFromList(sFromFolderFullName, aUidForRemove, oMoveFolder.fullName);
2014-08-20 23:03:12 +08:00
}
2015-11-19 01:32:29 +08:00
}
accountsAndIdentities() {
AccountUserStore.loading(true);
IdentityUserStore.loading(true);
Remote.request('AccountsAndIdentities', (iError, oData) => {
AccountUserStore.loading(false);
IdentityUserStore.loading(false);
2021-03-18 21:48:21 +08:00
if (!iError) {
const
// counts = {},
2021-11-15 17:56:52 +08:00
accounts = oData.Result.Accounts,
mainEmail = SettingsGet('MainEmail');
2021-11-15 17:56:52 +08:00
if (isArray(accounts)) {
// AccountUserStore.accounts.forEach(oAccount => counts[oAccount.email] = oAccount.count());
AccountUserStore.accounts(
2021-11-15 17:56:52 +08:00
accounts.map(
sValue => new AccountModel(sValue/*, counts[sValue]*/)
2019-07-05 03:19:24 +08:00
)
);
2021-11-15 17:56:52 +08:00
// accounts.length &&
AccountUserStore.accounts.unshift(new AccountModel(mainEmail/*, counts[mainEmail]*/, false));
2014-08-20 23:03:12 +08:00
}
if (isArray(oData.Result.Identities)) {
IdentityUserStore(
oData.Result.Identities.map(identityData => {
2021-11-15 17:56:52 +08:00
const identity = new IdentityModel(
pString(identityData.Id),
pString(identityData.Email)
);
2013-12-07 06:45:46 +08:00
2019-07-05 03:19:24 +08:00
identity.name(pString(identityData.Name));
identity.replyTo(pString(identityData.ReplyTo));
identity.bcc(pString(identityData.Bcc));
identity.signature(pString(identityData.Signature));
identity.signatureInsertBefore(!!identityData.SignatureInsertBefore);
2013-12-07 06:45:46 +08:00
2019-07-05 03:19:24 +08:00
return identity;
})
);
}
}
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) {
if (folder && folder.trim()) {
2022-02-24 06:11:12 +08:00
fetchFolderInformation(
(iError, data) => {
if (!iError && data.Result) {
const result = data.Result,
hash = getFolderHash(result.Folder),
folderFromCache = getFolderFromCacheList(result.Folder);
if (folderFromCache) {
2021-03-18 20:52:56 +08:00
folderFromCache.expires = Date.now();
setFolderHash(result.Folder, result.Hash);
folderFromCache.messageCountAll(result.MessageCount);
2019-07-05 03:19:24 +08:00
let unreadCountChange = (folderFromCache.messageCountUnread() !== result.MessageUnseenCount);
2014-08-20 23:03:12 +08:00
folderFromCache.messageCountUnread(result.MessageUnseenCount);
2014-08-20 23:03:12 +08:00
if (unreadCountChange) {
2021-11-23 04:01:30 +08:00
MessageFlagsCache.clearFolder(folderFromCache.fullName);
}
2014-08-20 23:03:12 +08:00
if (result.Flags.length) {
result.Flags.forEach(message =>
2021-12-07 02:25:28 +08:00
MessageFlagsCache.setFor(folderFromCache.fullName, message.Uid.toString(), message.Flags)
);
2019-07-05 03:19:24 +08:00
MessagelistUserStore.reloadFlagsAndCachedMessage();
}
MessagelistUserStore.initUidNextAndNewMessages(
2021-11-23 04:01:30 +08:00
folderFromCache.fullName,
result.UidNext,
result.NewMessages
);
if (!hash || unreadCountChange || result.Hash !== hash) {
2021-11-30 17:19:43 +08:00
if (folderFromCache.fullName === FolderUserStore.currentFolderFullName()) {
MessagelistUserStore.reload();
2021-11-23 04:01:30 +08:00
} else if (getFolderInboxName() === folderFromCache.fullName) {
2021-11-13 16:45:06 +08:00
Remote.messageList(null, {Folder: getFolderInboxName()}, true);
2014-08-20 23:03:12 +08:00
}
}
}
}
2019-07-05 03:19:24 +08:00
},
folder,
list
);
2014-08-20 23:03:12 +08:00
}
2016-04-21 01:12:51 +08:00
}
2015-11-19 01:32:29 +08:00
logout() {
2022-03-09 20:11:28 +08:00
Remote.request('Logout', () => {
const customLogoutLink = Settings.app('customLogoutLink');
if (customLogoutLink) {
((window.parent && Settings.app('inIframe')) ? window.parent : window).location.href = customLogoutLink;
} else {
rl.logoutReload()
}
});
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();
addEventListener('resize', () => leftPanelDisabled(ThemeStore.isMobile() || 1000 > innerWidth));
2021-08-31 22:21:40 +08:00
addEventListener('beforeunload', event => {
if (arePopupsVisible()) {
event.preventDefault();
return event.returnValue = "Are you sure you want to exit?";
}
}, {capture: true});
}
start() {
2021-03-10 18:44:48 +08:00
if (SettingsGet('Auth')) {
2021-04-13 01:15:33 +08:00
rl.setWindowTitle(i18n('GLOBAL/LOADING'));
2014-08-20 23:03:12 +08:00
2021-09-23 20:24:06 +08:00
NotificationUserStore.enableSoundNotification(!!SettingsGet('SoundNotification'));
NotificationUserStore.enableDesktopNotification(!!SettingsGet('DesktopNotifications'));
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
]);
setInterval(() => {
2021-11-30 17:19:43 +08:00
const cF = FolderUserStore.currentFolderFullName(),
2021-04-13 01:15:33 +08:00
iF = getFolderInboxName();
this.folderInformation(iF);
if (iF !== cF) {
this.folderInformation(cF);
}
folderInformationMultiply();
}, refreshFoldersInterval);
2014-10-29 06:05:50 +08:00
2021-09-23 20:24:06 +08:00
ContactUserStore.init();
2014-10-29 06:05:50 +08:00
this.accountsAndIdentities();
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();
2021-04-13 01:15:33 +08:00
if (getFolderInboxName() !== cF) {
this.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'),
right = elementById('rl-right'),
fToggle = () =>
2022-03-14 22:42:05 +08:00
setLayoutResizer(left, right, ClientSideKeyNameFolderListSize,
(ThemeStore.isMobile() || leftPanelDisabled()) ? 0 : 'Width');
if (left && right) {
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();
// When auto-login is active
if (navigator.registerProtocolHandler) {
try {
navigator.registerProtocolHandler(
'mailto',
location.protocol + '//' + location.host + location.pathname + '?mailto&to=%s',
(SettingsGet('Title') || 'SnappyMail')
);
} catch (e) {} // eslint-disable-line no-empty
}
2022-03-08 17:05:24 +08:00
setTimeout(() => mailToHelper(SettingsGet('MailToEmail')), 500);
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
}
2015-11-19 01:32:29 +08:00
export default new AppUser();