snappymail/dev/Stores/User/Folder.js

177 lines
5.2 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
import { koComputable } from 'External/ko';
2016-06-30 08:02:45 +08:00
2021-03-27 23:02:48 +08:00
import { FolderType, FolderSortMode } from 'Common/EnumsUser';
2019-07-05 03:19:24 +08:00
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
2022-02-17 16:36:29 +08:00
import { forEachObjectEntry } from 'Common/Utils';
import { addObservablesTo, addSubscribablesTo, addComputablesTo } from 'External/ko';
2019-07-05 03:19:24 +08:00
import { getFolderInboxName, getFolderFromCacheList } from 'Common/Cache';
2021-11-25 21:21:09 +08:00
import { Settings } from 'Common/Globals';
//import Remote from 'Remote/User/Fetch'; // Circular dependency
2016-06-30 08:02:45 +08:00
export const FolderUserStore = new class {
constructor() {
const self = this;
addObservablesTo(self, {
/**
* 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
// sortMode: '',
quotaLimit: 0,
quotaUsage: 0,
2020-10-27 18:09:24 +08:00
sentFolder: '',
2021-12-01 20:54:35 +08:00
draftsFolder: '',
2020-10-27 18:09:24 +08:00
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
self.sortMode = ko.observable('').extend({ limitedList: Object.values(FolderSortMode) });
self.namespace = '';
2020-10-27 18:09:24 +08:00
2021-12-01 20:54:35 +08:00
self.folderList = ko.observableArray(/*new FolderCollectionModel*/);
2016-06-30 08:02:45 +08:00
self.capabilities = ko.observableArray();
2016-06-30 08:02:45 +08:00
self.currentFolder = ko.observable(null).extend({ toggleSubscribeProperty: [self, 'selected'] });
2017-08-14 20:51:10 +08:00
addComputablesTo(self, {
2015-02-22 06:00:51 +08:00
2021-12-01 20:54:35 +08:00
draftsFolderNotEnabled: () => !self.draftsFolder() || UNUSED_OPTION_VALUE === self.draftsFolder(),
2015-02-22 06:00:51 +08:00
currentFolderFullName: () => (self.currentFolder() ? self.currentFolder().fullName : ''),
currentFolderFullNameHash: () => (self.currentFolder() ? self.currentFolder().fullNameHash : ''),
2015-02-22 06:00:51 +08:00
foldersChanging: () =>
self.foldersLoading() | self.foldersCreating() | self.foldersDeleting() | self.foldersRenaming(),
folderListSystemNames: () => {
const list = [getFolderInboxName()],
2021-12-01 20:54:35 +08:00
others = [self.sentFolder(), self.draftsFolder(), self.spamFolder(), self.trashFolder(), self.archiveFolder()];
self.folderList().length &&
others.forEach(name => name && UNUSED_OPTION_VALUE !== name && list.push(name));
return list;
},
folderListSystem: () =>
self.folderListSystemNames().map(name => getFolderFromCacheList(name)).filter(v => v)
});
2021-03-16 18:38:40 +08:00
const
2022-02-17 16:36:29 +08:00
subscribeRemoveSystemFolder = observable => {
observable.subscribe(() => {
const folder = getFolderFromCacheList(observable());
folder && folder.type(FolderType.User);
}, self, 'beforeChange');
2021-03-16 18:38:40 +08:00
},
fSetSystemFolderType = type => value => {
const folder = getFolderFromCacheList(value);
folder && folder.type(type);
2021-03-16 18:38:40 +08:00
};
2022-02-17 16:36:29 +08:00
subscribeRemoveSystemFolder(self.sentFolder);
subscribeRemoveSystemFolder(self.draftsFolder);
subscribeRemoveSystemFolder(self.spamFolder);
subscribeRemoveSystemFolder(self.trashFolder);
subscribeRemoveSystemFolder(self.archiveFolder);
addSubscribablesTo(self, {
2021-08-20 21:40:07 +08:00
sentFolder: fSetSystemFolderType(FolderType.Sent),
2021-12-01 20:54:35 +08:00
draftsFolder: fSetSystemFolderType(FolderType.Drafts),
2021-03-16 18:38:40 +08:00
spamFolder: fSetSystemFolderType(FolderType.Spam),
trashFolder: fSetSystemFolderType(FolderType.Trash),
archiveFolder: fSetSystemFolderType(FolderType.Archive)
});
self.quotaPercentage = koComputable(() => {
const quota = self.quotaLimit(), usage = self.quotaUsage();
return 0 < quota ? Math.ceil((usage / quota) * 100) : 0;
});
}
/**
* If the IMAP server supports SORT, METADATA
*/
hasCapability(name) {
return this.capabilities().includes(name);
}
/**
* @returns {Array}
*/
2021-03-18 20:52:56 +08:00
getNextFolderNames(ttl) {
2019-07-05 03:19:24 +08:00
const result = [],
limit = 10,
utc = Date.now(),
2021-03-18 20:52:56 +08:00
timeout = utc - ttl,
timeouts = [],
bDisplaySpecSetting = this.displaySpecSetting(),
fSearchFunction = (list) => {
list.forEach(folder => {
2019-07-05 03:19:24 +08:00
if (
folder &&
2021-08-25 22:27:17 +08:00
folder.selectable() &&
folder.exists &&
2021-03-18 20:52:56 +08:00
timeout > folder.expires &&
(folder.isSystemFolder() || (folder.subscribed() && (folder.checkable() || !bDisplaySpecSetting)))
2019-07-05 03:19:24 +08:00
) {
2021-11-23 04:01:30 +08:00
timeouts.push([folder.expires, folder.fullName]);
}
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) {
2021-03-18 20:52:56 +08:00
folder.expires = utc;
result.push(aItem[1]);
}
return limit <= result.length;
});
return result.filter((value, index, self) => self.indexOf(value) == index);
}
saveSystemFolders(folders) {
folders = folders || {
2021-12-01 20:54:35 +08:00
Sent: FolderUserStore.sentFolder(),
Drafts: FolderUserStore.draftsFolder(),
Spam: FolderUserStore.spamFolder(),
Trash: FolderUserStore.trashFolder(),
Archive: FolderUserStore.archiveFolder()
};
2021-12-01 20:54:35 +08:00
forEachObjectEntry(folders, (k,v)=>Settings.set(k+'Folder',v));
rl.app.Remote.request('SystemFoldersUpdate', null, folders);
}
};