2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-01-25 05:58:06 +08:00
|
|
|
import { FolderType } from 'Common/EnumsUser';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
|
2020-10-15 01:16:37 +08:00
|
|
|
import { folderListOptionsBuilder } from 'Common/UtilsUser';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { getFolderInboxName, getFolderFromCacheList } from 'Common/Cache';
|
2021-03-10 18:44:48 +08:00
|
|
|
import { SettingsGet } from 'Common/Globals';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class FolderUserStore {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2020-10-27 18:09:24 +08:00
|
|
|
ko.addObservablesTo(this, {
|
2020-12-10 21:22:46 +08:00
|
|
|
/**
|
|
|
|
* To use "checkable" option in /#/settings/folders
|
|
|
|
* When true, getNextFolderNames only lists system and "checkable" folders
|
|
|
|
* and affects the update of unseen count
|
|
|
|
* Auto set to true when amount of folders > folderSpecLimit to prevent requests overload,
|
|
|
|
* see application.ini [labs] folders_spec_limit
|
|
|
|
*/
|
2020-12-10 21:19:03 +08:00
|
|
|
displaySpecSetting: false,
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-27 18:09:24 +08:00
|
|
|
sentFolder: '',
|
|
|
|
draftFolder: '',
|
|
|
|
spamFolder: '',
|
|
|
|
trashFolder: '',
|
|
|
|
archiveFolder: '',
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-27 18:09:24 +08:00
|
|
|
folderListOptimized: false,
|
|
|
|
folderListError: '',
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-27 18:09:24 +08:00
|
|
|
foldersLoading: false,
|
|
|
|
foldersCreating: false,
|
|
|
|
foldersDeleting: false,
|
|
|
|
foldersRenaming: false,
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-27 18:09:24 +08:00
|
|
|
foldersInboxUnreadCount: 0
|
|
|
|
});
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-27 18:09:24 +08:00
|
|
|
this.namespace = '';
|
|
|
|
|
2021-01-22 19:23:20 +08:00
|
|
|
this.folderList = ko.observableArray();
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.currentFolder = ko.observable(null).extend({ toggleSubscribeProperty: [this, 'selected'] });
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-03-10 18:44:48 +08:00
|
|
|
this.sieveAllowFileintoInbox = !!SettingsGet('SieveAllowFileintoInbox');
|
2017-08-14 20:51:10 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.draftFolderNotEnabled = ko.computed(
|
2020-07-28 23:20:14 +08:00
|
|
|
() => !this.draftFolder() || UNUSED_OPTION_VALUE === this.draftFolder()
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
this.foldersListWithSingleInboxRootFolder = ko.computed(
|
2021-01-22 19:23:20 +08:00
|
|
|
() => !this.folderList.find(folder => folder && !folder.isSystemFolder() && folder.visible())
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.currentFolderFullNameRaw = ko.computed(() => (this.currentFolder() ? this.currentFolder().fullNameRaw : ''));
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.currentFolderFullName = ko.computed(() => (this.currentFolder() ? this.currentFolder().fullName : ''));
|
|
|
|
this.currentFolderFullNameHash = ko.computed(() => (this.currentFolder() ? this.currentFolder().fullNameHash : ''));
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.foldersChanging = ko.computed(() => {
|
2019-07-05 03:19:24 +08:00
|
|
|
const loading = this.foldersLoading(),
|
2016-08-17 06:01:20 +08:00
|
|
|
creating = this.foldersCreating(),
|
|
|
|
deleting = this.foldersDeleting(),
|
|
|
|
renaming = this.foldersRenaming();
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return loading || creating || deleting || renaming;
|
2015-02-22 06:00:51 +08:00
|
|
|
});
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.folderListSystemNames = ko.computed(() => {
|
2019-07-05 03:19:24 +08:00
|
|
|
const list = [getFolderInboxName()],
|
2016-08-17 06:01:20 +08:00
|
|
|
sentFolder = this.sentFolder(),
|
|
|
|
draftFolder = this.draftFolder(),
|
|
|
|
spamFolder = this.spamFolder(),
|
|
|
|
trashFolder = this.trashFolder(),
|
|
|
|
archiveFolder = this.archiveFolder();
|
|
|
|
|
2021-01-22 19:23:20 +08:00
|
|
|
if (this.folderList.length) {
|
2020-07-28 23:20:14 +08:00
|
|
|
if (sentFolder && UNUSED_OPTION_VALUE !== sentFolder) {
|
2016-08-17 06:01:20 +08:00
|
|
|
list.push(sentFolder);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2020-07-28 23:20:14 +08:00
|
|
|
if (draftFolder && UNUSED_OPTION_VALUE !== draftFolder) {
|
2016-08-17 06:01:20 +08:00
|
|
|
list.push(draftFolder);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2020-07-28 23:20:14 +08:00
|
|
|
if (spamFolder && UNUSED_OPTION_VALUE !== spamFolder) {
|
2016-08-17 06:01:20 +08:00
|
|
|
list.push(spamFolder);
|
|
|
|
}
|
2020-07-28 23:20:14 +08:00
|
|
|
if (trashFolder && UNUSED_OPTION_VALUE !== trashFolder) {
|
2016-08-17 06:01:20 +08:00
|
|
|
list.push(trashFolder);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2020-07-28 23:20:14 +08:00
|
|
|
if (archiveFolder && UNUSED_OPTION_VALUE !== archiveFolder) {
|
2016-08-17 06:01:20 +08:00
|
|
|
list.push(archiveFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
});
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.folderListSystem = ko.computed(() =>
|
2020-10-03 05:54:15 +08:00
|
|
|
this.folderListSystemNames().map(name => getFolderFromCacheList(name)).filter(v => v)
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.folderMenuForMove = ko.computed(() =>
|
|
|
|
folderListOptionsBuilder(
|
|
|
|
this.folderListSystem(),
|
|
|
|
this.folderList(),
|
|
|
|
[this.currentFolderFullNameRaw()],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
(item) => (item ? item.localName() : '')
|
|
|
|
)
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.folderMenuForFilters = ko.computed(() =>
|
|
|
|
folderListOptionsBuilder(
|
|
|
|
this.folderListSystem(),
|
|
|
|
this.folderList(),
|
|
|
|
[this.sieveAllowFileintoInbox ? '' : 'INBOX'],
|
|
|
|
[['', '']],
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
(item) => (item ? item.localName() : '')
|
|
|
|
)
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
2020-09-24 21:08:57 +08:00
|
|
|
|
|
|
|
this.subscribers();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
subscribers() {
|
2019-07-05 03:19:24 +08:00
|
|
|
const fRemoveSystemFolderType = (observable) => () => {
|
|
|
|
const folder = getFolderFromCacheList(observable());
|
|
|
|
if (folder) {
|
|
|
|
folder.type(FolderType.User);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const fSetSystemFolderType = (type) => (value) => {
|
|
|
|
const folder = getFolderFromCacheList(value);
|
|
|
|
if (folder) {
|
|
|
|
folder.type(type);
|
|
|
|
}
|
|
|
|
};
|
2016-08-17 06:01:20 +08:00
|
|
|
|
|
|
|
this.sentFolder.subscribe(fRemoveSystemFolderType(this.sentFolder), this, 'beforeChange');
|
|
|
|
this.draftFolder.subscribe(fRemoveSystemFolderType(this.draftFolder), this, 'beforeChange');
|
|
|
|
this.spamFolder.subscribe(fRemoveSystemFolderType(this.spamFolder), this, 'beforeChange');
|
|
|
|
this.trashFolder.subscribe(fRemoveSystemFolderType(this.trashFolder), this, 'beforeChange');
|
|
|
|
this.archiveFolder.subscribe(fRemoveSystemFolderType(this.archiveFolder), this, 'beforeChange');
|
|
|
|
|
|
|
|
this.sentFolder.subscribe(fSetSystemFolderType(FolderType.SentItems), this);
|
|
|
|
this.draftFolder.subscribe(fSetSystemFolderType(FolderType.Draft), this);
|
|
|
|
this.spamFolder.subscribe(fSetSystemFolderType(FolderType.Spam), this);
|
|
|
|
this.trashFolder.subscribe(fSetSystemFolderType(FolderType.Trash), this);
|
|
|
|
this.archiveFolder.subscribe(fSetSystemFolderType(FolderType.Archive), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
getNextFolderNames() {
|
2019-07-05 03:19:24 +08:00
|
|
|
const result = [],
|
2020-12-10 05:30:57 +08:00
|
|
|
limit = 10,
|
|
|
|
utc = Date.now(),
|
|
|
|
timeout = utc - 60000 * 5,
|
2016-08-17 06:01:20 +08:00
|
|
|
timeouts = [],
|
|
|
|
inboxFolderName = getFolderInboxName(),
|
2020-12-10 21:19:03 +08:00
|
|
|
bDisplaySpecSetting = this.displaySpecSetting(),
|
2016-08-17 06:01:20 +08:00
|
|
|
fSearchFunction = (list) => {
|
2020-07-22 20:49:18 +08:00
|
|
|
list.forEach(folder => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (
|
|
|
|
folder &&
|
|
|
|
inboxFolderName !== folder.fullNameRaw &&
|
|
|
|
folder.selectable &&
|
2020-10-26 19:54:03 +08:00
|
|
|
folder.exists &&
|
2019-07-05 03:19:24 +08:00
|
|
|
timeout > folder.interval &&
|
2020-12-10 21:19:03 +08:00
|
|
|
(folder.isSystemFolder() || (folder.subscribed() && (folder.checkable() || !bDisplaySpecSetting)))
|
2019-07-05 03:19:24 +08:00
|
|
|
) {
|
2016-08-17 06:01:20 +08:00
|
|
|
timeouts.push([folder.interval, folder.fullNameRaw]);
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:23:20 +08:00
|
|
|
if (folder && folder.subFolders.length) {
|
2016-08-17 06:01:20 +08:00
|
|
|
fSearchFunction(folder.subFolders());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
fSearchFunction(this.folderList());
|
|
|
|
|
2020-08-15 02:35:39 +08:00
|
|
|
timeouts.sort((a, b) => (a[0] < b[0]) ? -1 : (a[0] > b[0] ? 1 : 0));
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2020-07-22 16:43:19 +08:00
|
|
|
timeouts.find(aItem => {
|
2016-08-17 06:01:20 +08:00
|
|
|
const folder = getFolderFromCacheList(aItem[1]);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (folder) {
|
2016-08-17 06:01:20 +08:00
|
|
|
folder.interval = utc;
|
|
|
|
result.push(aItem[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit <= result.length;
|
|
|
|
});
|
|
|
|
|
2020-07-23 22:06:16 +08:00
|
|
|
return result.filter((value, index, self) => self.indexOf(value) == index);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 07:58:58 +08:00
|
|
|
|
2016-09-13 04:50:21 +08:00
|
|
|
export default new FolderUserStore();
|