2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2021-12-31 20:30:05 +08:00
|
|
|
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';
|
2022-02-24 02:26:52 +08:00
|
|
|
//import Remote from 'Remote/User/Fetch'; // Circular dependency
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
export const FolderUserStore = new class {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2021-11-03 09:28:01 +08:00
|
|
|
const self = this;
|
|
|
|
addObservablesTo(self, {
|
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
|
|
|
|
2021-03-26 22:07:14 +08:00
|
|
|
// sortMode: '',
|
|
|
|
|
2021-11-01 23:26:07 +08:00
|
|
|
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
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
self.sortMode = ko.observable('').extend({ limitedList: Object.values(FolderSortMode) });
|
2021-03-26 22:07:14 +08:00
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
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
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
self.capabilities = ko.observableArray();
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
self.currentFolder = ko.observable(null).extend({ toggleSubscribeProperty: [self, 'selected'] });
|
2017-08-14 20:51:10 +08:00
|
|
|
|
2021-11-03 09:28:01 +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
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
currentFolderFullName: () => (self.currentFolder() ? self.currentFolder().fullName : ''),
|
|
|
|
currentFolderFullNameHash: () => (self.currentFolder() ? self.currentFolder().fullNameHash : ''),
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-09-03 22:37:20 +08:00
|
|
|
foldersChanging: () =>
|
2021-11-03 09:28:01 +08:00
|
|
|
self.foldersLoading() | self.foldersCreating() | self.foldersDeleting() | self.foldersRenaming(),
|
2021-08-23 23:23:43 +08:00
|
|
|
|
2021-09-03 22:37:20 +08:00
|
|
|
folderListSystemNames: () => {
|
|
|
|
const list = [getFolderInboxName()],
|
2021-12-01 20:54:35 +08:00
|
|
|
others = [self.sentFolder(), self.draftsFolder(), self.spamFolder(), self.trashFolder(), self.archiveFolder()];
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
self.folderList().length &&
|
2021-09-03 22:37:20 +08:00
|
|
|
others.forEach(name => name && UNUSED_OPTION_VALUE !== name && list.push(name));
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-09-03 22:37:20 +08:00
|
|
|
return list;
|
|
|
|
},
|
|
|
|
|
|
|
|
folderListSystem: () =>
|
2021-11-03 09:28:01 +08:00
|
|
|
self.folderListSystemNames().map(name => getFolderFromCacheList(name)).filter(v => v)
|
2021-09-03 22:37:20 +08:00
|
|
|
});
|
2016-08-17 06:01:20 +08:00
|
|
|
|
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);
|
2021-08-23 23:23:43 +08:00
|
|
|
folder && folder.type(type);
|
2021-03-16 18:38:40 +08:00
|
|
|
};
|
2016-08-17 06:01:20 +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);
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
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)
|
|
|
|
});
|
2021-11-01 23:26:07 +08:00
|
|
|
|
2021-12-31 20:30:05 +08:00
|
|
|
self.quotaPercentage = koComputable(() => {
|
2021-11-03 09:28:01 +08:00
|
|
|
const quota = self.quotaLimit(), usage = self.quotaUsage();
|
2021-11-01 23:26:07 +08:00
|
|
|
return 0 < quota ? Math.ceil((usage / quota) * 100) : 0;
|
|
|
|
});
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
2021-11-03 09:28:01 +08:00
|
|
|
/**
|
|
|
|
* If the IMAP server supports SORT, METADATA
|
|
|
|
*/
|
|
|
|
hasCapability(name) {
|
|
|
|
return this.capabilities().includes(name);
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2021-03-18 20:52:56 +08:00
|
|
|
getNextFolderNames(ttl) {
|
2019-07-05 03:19:24 +08:00
|
|
|
const result = [],
|
2020-12-10 05:30:57 +08:00
|
|
|
limit = 10,
|
|
|
|
utc = Date.now(),
|
2021-03-18 20:52:56 +08:00
|
|
|
timeout = utc - ttl,
|
2016-08-17 06:01:20 +08:00
|
|
|
timeouts = [],
|
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 &&
|
2021-08-25 22:27:17 +08:00
|
|
|
folder.selectable() &&
|
2020-10-26 19:54:03 +08:00
|
|
|
folder.exists &&
|
2021-03-18 20:52:56 +08:00
|
|
|
timeout > folder.expires &&
|
2020-12-10 21:19:03 +08:00
|
|
|
(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]);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-03-18 20:52:56 +08:00
|
|
|
folder.expires = utc;
|
2016-08-17 06:01:20 +08:00
|
|
|
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
|
|
|
}
|
2021-08-24 21:53:39 +08:00
|
|
|
|
|
|
|
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-08-24 21:53:39 +08:00
|
|
|
};
|
2021-12-01 20:54:35 +08:00
|
|
|
forEachObjectEntry(folders, (k,v)=>Settings.set(k+'Folder',v));
|
2021-12-03 06:15:24 +08:00
|
|
|
rl.app.Remote.request('SystemFoldersUpdate', null, folders);
|
2021-08-24 21:53:39 +08:00
|
|
|
}
|
2021-03-11 05:41:35 +08:00
|
|
|
};
|