2020-09-16 22:33:53 +08:00
|
|
|
import { AbstractCollectionModel } from 'Model/AbstractCollection';
|
|
|
|
|
2020-09-16 15:32:36 +08:00
|
|
|
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
|
|
|
|
import { pInt } from 'Common/Utils';
|
2020-10-15 18:23:11 +08:00
|
|
|
import { ClientSideKeyName, FolderType, ServerFolderType } from 'Common/Enums';
|
2020-09-16 15:32:36 +08:00
|
|
|
import * as Cache from 'Common/Cache';
|
|
|
|
|
|
|
|
import * as Local from 'Storage/Client';
|
|
|
|
|
|
|
|
import AppStore from 'Stores/User/App';
|
|
|
|
import FolderStore from 'Stores/User/Folder';
|
|
|
|
|
|
|
|
import { FolderModel } from 'Model/Folder';
|
|
|
|
|
|
|
|
import Remote from 'Remote/User/Fetch';
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Settings = rl.settings,
|
|
|
|
|
2020-09-16 21:44:36 +08:00
|
|
|
normalizeFolder = sFolderFullNameRaw => ('' === sFolderFullNameRaw
|
2020-09-16 15:32:36 +08:00
|
|
|
|| UNUSED_OPTION_VALUE === sFolderFullNameRaw
|
|
|
|
|| null !== Cache.getFolderFromCacheList(sFolderFullNameRaw))
|
|
|
|
? sFolderFullNameRaw
|
|
|
|
: '';
|
|
|
|
|
2020-09-16 22:33:53 +08:00
|
|
|
export class FolderCollectionModel extends AbstractCollectionModel
|
2020-09-16 15:32:36 +08:00
|
|
|
{
|
2020-09-16 22:33:53 +08:00
|
|
|
/*
|
2020-09-16 15:32:36 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.CountRec
|
2020-09-16 21:44:36 +08:00
|
|
|
this.FoldersHash
|
2020-09-16 15:32:36 +08:00
|
|
|
this.IsThreadsSupported
|
2020-09-16 21:44:36 +08:00
|
|
|
this.Namespace;
|
2020-09-16 15:32:36 +08:00
|
|
|
this.Optimized
|
|
|
|
this.SystemFolders
|
|
|
|
}
|
2020-09-16 22:33:53 +08:00
|
|
|
*/
|
2020-09-16 15:32:36 +08:00
|
|
|
|
|
|
|
/**
|
2020-09-16 21:44:36 +08:00
|
|
|
* @param {?Object} json
|
2020-09-16 15:32:36 +08:00
|
|
|
* @returns {FolderCollectionModel}
|
|
|
|
*/
|
2020-09-16 22:33:53 +08:00
|
|
|
static reviveFromJson(object) {
|
2020-10-18 17:46:22 +08:00
|
|
|
const expandedFolders = Local.get(ClientSideKeyName.ExpandedFolders),
|
|
|
|
bDisplaySpecSetting = FolderStore.displaySpecSetting();
|
|
|
|
return super.reviveFromJson(object, (oFolder, self) => {
|
|
|
|
let oCacheFolder = Cache.getFolderFromCacheList(oFolder.FullNameRaw);
|
2020-10-23 21:15:54 +08:00
|
|
|
/*
|
|
|
|
if (oCacheFolder) {
|
|
|
|
oFolder.SubFolders = FolderCollectionModel.reviveFromJson(oFolder.SubFolders);
|
|
|
|
oFolder.SubFolders && oCacheFolder.subFolders(oFolder.SubFolders);
|
|
|
|
}
|
|
|
|
*/
|
2020-10-19 01:46:47 +08:00
|
|
|
if (!oCacheFolder && (oCacheFolder = FolderModel.reviveFromJson(oFolder))) {
|
2020-10-18 17:46:22 +08:00
|
|
|
if (oFolder.FullNameRaw == self.SystemFolders[ServerFolderType.INBOX]) {
|
|
|
|
oCacheFolder.type(FolderType.Inbox);
|
|
|
|
Cache.setFolderInboxName(oFolder.FullNameRaw);
|
|
|
|
}
|
|
|
|
Cache.setFolder(oCacheFolder.fullNameHash, oFolder.FullNameRaw, oCacheFolder);
|
|
|
|
}
|
2020-09-16 15:32:36 +08:00
|
|
|
|
2020-10-18 17:46:22 +08:00
|
|
|
if (oCacheFolder) {
|
|
|
|
oCacheFolder.checkable(bDisplaySpecSetting ? !!oFolder.Checkable : true);
|
2020-09-16 15:32:36 +08:00
|
|
|
|
2020-10-18 17:46:22 +08:00
|
|
|
oCacheFolder.collapsed(!expandedFolders
|
|
|
|
|| !Array.isArray(expandedFolders)
|
|
|
|
|| !expandedFolders.includes(oCacheFolder.fullNameHash));
|
2020-09-16 15:32:36 +08:00
|
|
|
|
2020-10-18 17:46:22 +08:00
|
|
|
if (oFolder.Extended) {
|
|
|
|
if (oFolder.Extended.Hash) {
|
|
|
|
Cache.setFolderHash(oCacheFolder.fullNameRaw, oFolder.Extended.Hash);
|
|
|
|
}
|
2020-09-16 15:32:36 +08:00
|
|
|
|
2020-10-18 17:46:22 +08:00
|
|
|
if (null != oFolder.Extended.MessageCount) {
|
|
|
|
oCacheFolder.messageCountAll(oFolder.Extended.MessageCount);
|
|
|
|
}
|
2020-09-16 15:32:36 +08:00
|
|
|
|
2020-10-18 17:46:22 +08:00
|
|
|
if (null != oFolder.Extended.MessageUnseenCount) {
|
|
|
|
oCacheFolder.messageCountUnread(oFolder.Extended.MessageUnseenCount);
|
2020-09-16 21:44:36 +08:00
|
|
|
}
|
2020-09-16 15:32:36 +08:00
|
|
|
}
|
2020-10-18 17:46:22 +08:00
|
|
|
}
|
|
|
|
return oCacheFolder;
|
|
|
|
});
|
2020-09-16 15:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
storeIt() {
|
|
|
|
const cnt = pInt(this.CountRec);
|
|
|
|
|
|
|
|
let limit = pInt(Settings.app('folderSpecLimit'));
|
|
|
|
limit = 100 < limit ? 100 : 10 > limit ? 10 : limit;
|
|
|
|
|
|
|
|
FolderStore.displaySpecSetting(0 >= cnt || limit < cnt);
|
|
|
|
|
|
|
|
FolderStore.folderList(this);
|
|
|
|
|
|
|
|
if (undefined !== this.Namespace) {
|
|
|
|
FolderStore.namespace = this.Namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppStore.threadsAllowed(!!(Settings.app('useImapThread') && this.IsThreadsSupported));
|
|
|
|
|
2020-09-16 21:44:36 +08:00
|
|
|
FolderStore.folderList.optimized(!!this.Optimized);
|
2020-09-16 15:32:36 +08:00
|
|
|
|
|
|
|
let update = false;
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.SystemFolders &&
|
|
|
|
!('' +
|
|
|
|
Settings.get('SentFolder') +
|
|
|
|
Settings.get('DraftFolder') +
|
|
|
|
Settings.get('SpamFolder') +
|
|
|
|
Settings.get('TrashFolder') +
|
|
|
|
Settings.get('ArchiveFolder') +
|
|
|
|
Settings.get('NullFolder'))
|
|
|
|
) {
|
|
|
|
Settings.set('SentFolder', this.SystemFolders[ServerFolderType.SENT] || null);
|
|
|
|
Settings.set('DraftFolder', this.SystemFolders[ServerFolderType.DRAFTS] || null);
|
|
|
|
Settings.set('SpamFolder', this.SystemFolders[ServerFolderType.JUNK] || null);
|
|
|
|
Settings.set('TrashFolder', this.SystemFolders[ServerFolderType.TRASH] || null);
|
|
|
|
Settings.set('ArchiveFolder', this.SystemFolders[ServerFolderType.ALL] || null);
|
|
|
|
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderStore.sentFolder(normalizeFolder(Settings.get('SentFolder')));
|
|
|
|
FolderStore.draftFolder(normalizeFolder(Settings.get('DraftFolder')));
|
|
|
|
FolderStore.spamFolder(normalizeFolder(Settings.get('SpamFolder')));
|
|
|
|
FolderStore.trashFolder(normalizeFolder(Settings.get('TrashFolder')));
|
|
|
|
FolderStore.archiveFolder(normalizeFolder(Settings.get('ArchiveFolder')));
|
|
|
|
|
|
|
|
if (update) {
|
|
|
|
Remote.saveSystemFolders(()=>{}, {
|
|
|
|
SentFolder: FolderStore.sentFolder(),
|
|
|
|
DraftFolder: FolderStore.draftFolder(),
|
|
|
|
SpamFolder: FolderStore.spamFolder(),
|
|
|
|
TrashFolder: FolderStore.trashFolder(),
|
|
|
|
ArchiveFolder: FolderStore.archiveFolder(),
|
|
|
|
NullFolder: 'NullFolder'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Local.set(ClientSideKeyName.FoldersLashHash, this.FoldersHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|