snappymail/dev/Stores/User/Folder.js

206 lines
6 KiB
JavaScript
Raw Normal View History

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';
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 {
constructor() {
2020-10-27 18:09:24 +08:00
ko.addObservablesTo(this, {
/**
* 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
*/
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 = '';
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
this.draftFolderNotEnabled = ko.computed(
() => !this.draftFolder() || UNUSED_OPTION_VALUE === this.draftFolder()
);
this.foldersListWithSingleInboxRootFolder = ko.computed(
() => !this.folderList.find(folder => folder && !folder.isSystemFolder() && folder.visible())
);
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
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
this.foldersChanging = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const loading = this.foldersLoading(),
creating = this.foldersCreating(),
deleting = this.foldersDeleting(),
renaming = this.foldersRenaming();
2015-02-22 06:00:51 +08:00
return loading || creating || deleting || renaming;
2015-02-22 06:00:51 +08:00
});
this.folderListSystemNames = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const list = [getFolderInboxName()],
sentFolder = this.sentFolder(),
draftFolder = this.draftFolder(),
spamFolder = this.spamFolder(),
trashFolder = this.trashFolder(),
archiveFolder = this.archiveFolder();
if (this.folderList.length) {
if (sentFolder && UNUSED_OPTION_VALUE !== sentFolder) {
list.push(sentFolder);
2016-06-30 08:02:45 +08:00
}
if (draftFolder && UNUSED_OPTION_VALUE !== draftFolder) {
list.push(draftFolder);
2016-06-30 08:02:45 +08:00
}
if (spamFolder && UNUSED_OPTION_VALUE !== spamFolder) {
list.push(spamFolder);
}
if (trashFolder && UNUSED_OPTION_VALUE !== trashFolder) {
list.push(trashFolder);
2016-06-30 08:02:45 +08:00
}
if (archiveFolder && UNUSED_OPTION_VALUE !== archiveFolder) {
list.push(archiveFolder);
}
}
return list;
});
2019-07-05 03:19:24 +08:00
this.folderListSystem = ko.computed(() =>
this.folderListSystemNames().map(name => getFolderFromCacheList(name)).filter(v => v)
);
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() : '')
)
);
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() : '')
)
);
this.subscribers();
}
2016-06-30 08:02:45 +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);
}
};
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 = [],
limit = 10,
utc = Date.now(),
timeout = utc - 60000 * 5,
timeouts = [],
inboxFolderName = getFolderInboxName(),
bDisplaySpecSetting = this.displaySpecSetting(),
fSearchFunction = (list) => {
list.forEach(folder => {
2019-07-05 03:19:24 +08:00
if (
folder &&
inboxFolderName !== folder.fullNameRaw &&
folder.selectable &&
folder.exists &&
2019-07-05 03:19:24 +08:00
timeout > folder.interval &&
(folder.isSystemFolder() || (folder.subscribed() && (folder.checkable() || !bDisplaySpecSetting)))
2019-07-05 03:19:24 +08:00
) {
timeouts.push([folder.interval, folder.fullNameRaw]);
}
if (folder && folder.subFolders.length) {
fSearchFunction(folder.subFolders());
}
});
};
fSearchFunction(this.folderList());
timeouts.sort((a, b) => (a[0] < b[0]) ? -1 : (a[0] > b[0] ? 1 : 0));
timeouts.find(aItem => {
const folder = getFolderFromCacheList(aItem[1]);
2019-07-05 03:19:24 +08:00
if (folder) {
folder.interval = utc;
result.push(aItem[1]);
}
return limit <= result.length;
});
return result.filter((value, index, self) => self.indexOf(value) == index);
}
}
2015-02-03 07:58:58 +08:00
export default new FolderUserStore();