2021-01-25 05:58:06 +08:00
|
|
|
import { MessageSetAction } from 'Common/EnumsUser';
|
2021-12-07 02:25:28 +08:00
|
|
|
import { isArray } from 'Common/Utils';
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
let FOLDERS_CACHE = {},
|
|
|
|
FOLDERS_NAME_CACHE = {},
|
|
|
|
MESSAGE_FLAGS_CACHE = {},
|
|
|
|
NEW_MESSAGE_CACHE = {},
|
2021-11-13 16:46:59 +08:00
|
|
|
REQUESTED_MESSAGE_CACHE = {},
|
2020-10-15 18:23:11 +08:00
|
|
|
inboxFolderName = 'INBOX';
|
2016-06-17 07:23:49 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
export const
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-11-13 16:46:59 +08:00
|
|
|
clearCache = () => {
|
2021-08-20 21:40:07 +08:00
|
|
|
FOLDERS_CACHE = {};
|
|
|
|
FOLDERS_NAME_CACHE = {};
|
|
|
|
MESSAGE_FLAGS_CACHE = {};
|
2021-11-13 16:46:59 +08:00
|
|
|
NEW_MESSAGE_CACHE = {};
|
|
|
|
REQUESTED_MESSAGE_CACHE = {};
|
2021-08-20 21:40:07 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @param {string} uid
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
getMessageKey = (folderFullName, uid) => `${folderFullName}#${uid}`,
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} folder
|
|
|
|
* @param {string} uid
|
|
|
|
*/
|
|
|
|
addRequestedMessage = (folder, uid) => REQUESTED_MESSAGE_CACHE[getMessageKey(folder, uid)] = true,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} folder
|
|
|
|
* @param {string} uid
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
hasRequestedMessage = (folder, uid) => true === REQUESTED_MESSAGE_CACHE[getMessageKey(folder, uid)],
|
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @param {string} uid
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
addNewMessageCache = (folderFullName, uid) => NEW_MESSAGE_CACHE[getMessageKey(folderFullName, uid)] = true,
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @param {string} uid
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
hasNewMessageAndRemoveFromCache = (folderFullName, uid) => {
|
|
|
|
if (NEW_MESSAGE_CACHE[getMessageKey(folderFullName, uid)]) {
|
|
|
|
NEW_MESSAGE_CACHE[getMessageKey(folderFullName, uid)] = null;
|
2021-08-20 21:40:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clearNewMessageCache = () => NEW_MESSAGE_CACHE = {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getFolderInboxName = () => inboxFolderName,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
setFolderInboxName = name => inboxFolderName = name,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} folderHash
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
getFolderFullName = folderHash =>
|
2021-08-20 21:40:07 +08:00
|
|
|
folderHash && FOLDERS_NAME_CACHE[folderHash] ? FOLDERS_NAME_CACHE[folderHash] : '',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} folderHash
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @param {?FolderModel} folder
|
|
|
|
*/
|
2021-12-07 21:03:30 +08:00
|
|
|
setFolder = folder => {
|
|
|
|
folder.hash = '';
|
|
|
|
FOLDERS_CACHE[folder.fullName] = folder;
|
|
|
|
FOLDERS_NAME_CACHE[folder.fullNameHash] = folder.fullName;
|
2021-08-20 21:40:07 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
getFolderHash = folderFullName =>
|
2021-12-07 21:03:30 +08:00
|
|
|
FOLDERS_CACHE[folderFullName] ? FOLDERS_CACHE[folderFullName].hash : '',
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @param {string} folderHash
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
setFolderHash = (folderFullName, folderHash) =>
|
2021-12-07 21:03:30 +08:00
|
|
|
FOLDERS_CACHE[folderFullName] && (FOLDERS_CACHE[folderFullName].hash = folderHash),
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
getFolderUidNext = folderFullName =>
|
2021-12-07 21:03:30 +08:00
|
|
|
FOLDERS_CACHE[folderFullName] ? FOLDERS_CACHE[folderFullName].uidNext : 0,
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @param {string} uidNext
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
setFolderUidNext = (folderFullName, uidNext) =>
|
2021-12-07 21:03:30 +08:00
|
|
|
FOLDERS_CACHE[folderFullName] && (FOLDERS_CACHE[folderFullName].uidNext = uidNext),
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
* @returns {?FolderModel}
|
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
getFolderFromCacheList = folderFullName =>
|
2021-12-07 21:03:30 +08:00
|
|
|
FOLDERS_CACHE[folderFullName] ? FOLDERS_CACHE[folderFullName] : null,
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} folderFullName
|
2021-08-20 21:40:07 +08:00
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
removeFolderFromCacheList = folderFullName => delete FOLDERS_CACHE[folderFullName];
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
export class MessageFlagsCache
|
|
|
|
{
|
2021-12-07 07:05:55 +08:00
|
|
|
/**
|
|
|
|
* @param {string} folderFullName
|
|
|
|
* @param {string} uid
|
|
|
|
* @param {string} flag
|
|
|
|
* @returns {bool}
|
|
|
|
*/
|
|
|
|
static hasFlag(folderFullName, uid, flag) {
|
|
|
|
return MESSAGE_FLAGS_CACHE[folderFullName]
|
|
|
|
&& MESSAGE_FLAGS_CACHE[folderFullName][uid]
|
|
|
|
&& MESSAGE_FLAGS_CACHE[folderFullName][uid].includes(flag);
|
|
|
|
}
|
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
/**
|
|
|
|
* @param {string} folderFullName
|
|
|
|
* @param {string} uid
|
|
|
|
* @returns {?Array}
|
|
|
|
*/
|
|
|
|
static getFor(folderFullName, uid) {
|
2021-12-07 21:03:30 +08:00
|
|
|
return MESSAGE_FLAGS_CACHE[folderFullName] && MESSAGE_FLAGS_CACHE[folderFullName][uid];
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
/**
|
|
|
|
* @param {string} folderFullName
|
|
|
|
* @param {string} uid
|
|
|
|
* @param {Array} flagsCache
|
|
|
|
*/
|
2021-12-07 02:25:28 +08:00
|
|
|
static setFor(folderFullName, uid, flags) {
|
|
|
|
if (isArray(flags)) {
|
|
|
|
if (!MESSAGE_FLAGS_CACHE[folderFullName]) {
|
|
|
|
MESSAGE_FLAGS_CACHE[folderFullName] = {};
|
|
|
|
}
|
|
|
|
MESSAGE_FLAGS_CACHE[folderFullName][uid] = flags;
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
2020-10-25 20:50:26 +08:00
|
|
|
}
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
/**
|
|
|
|
* @param {string} folderFullName
|
|
|
|
*/
|
|
|
|
static clearFolder(folderFullName) {
|
|
|
|
MESSAGE_FLAGS_CACHE[folderFullName] = {};
|
|
|
|
}
|
2016-06-17 07:23:49 +08:00
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
/**
|
|
|
|
* @param {(MessageModel|null)} message
|
|
|
|
*/
|
|
|
|
static initMessage(message) {
|
|
|
|
if (message) {
|
|
|
|
const uid = message.uid,
|
2021-12-23 03:16:14 +08:00
|
|
|
flags = this.getFor(message.folder, uid),
|
|
|
|
thread = message.threads;
|
2020-10-25 20:50:26 +08:00
|
|
|
|
2021-12-06 23:08:58 +08:00
|
|
|
if (isArray(flags)) {
|
|
|
|
message.flags(flags);
|
2020-10-25 20:50:26 +08:00
|
|
|
}
|
2016-06-17 07:23:49 +08:00
|
|
|
|
2021-12-23 03:16:14 +08:00
|
|
|
if (thread.length) {
|
|
|
|
const unseenSubUid = thread.find(iSubUid =>
|
|
|
|
(uid !== iSubUid) && !this.hasFlag(message.folder, iSubUid, '\\seen')
|
2021-12-07 07:05:55 +08:00
|
|
|
);
|
|
|
|
|
2021-12-23 03:16:14 +08:00
|
|
|
const flaggedSubUid = thread.find(iSubUid =>
|
|
|
|
(uid !== iSubUid) && this.hasFlag(message.folder, iSubUid, '\\flagged')
|
2021-12-07 07:05:55 +08:00
|
|
|
);
|
2020-10-25 20:50:26 +08:00
|
|
|
|
2021-12-07 02:25:28 +08:00
|
|
|
message.hasUnseenSubMessage(!!unseenSubUid);
|
|
|
|
message.hasFlaggedSubMessage(!!flaggedSubUid);
|
2020-10-25 20:50:26 +08:00
|
|
|
}
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
/**
|
|
|
|
* @param {(MessageModel|null)} message
|
|
|
|
*/
|
|
|
|
static store(message) {
|
|
|
|
if (message) {
|
2021-12-06 23:08:58 +08:00
|
|
|
this.setFor(message.folder, message.uid, message.flags());
|
2020-10-25 20:50:26 +08:00
|
|
|
}
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
/**
|
|
|
|
* @param {string} folder
|
|
|
|
* @param {string} uid
|
|
|
|
* @param {number} setAction
|
|
|
|
*/
|
|
|
|
static storeBySetAction(folder, uid, setAction) {
|
2021-12-07 07:05:55 +08:00
|
|
|
let flags = this.getFor(folder, uid) || [];
|
2021-12-07 02:25:28 +08:00
|
|
|
const
|
|
|
|
unread = flags.includes('\\seen') ? 0 : 1,
|
2021-12-07 07:05:55 +08:00
|
|
|
add = item => flags.includes(item) || flags.push(item),
|
|
|
|
remove = item => flags = flags.filter(flag => flag != item);
|
2020-10-25 20:50:26 +08:00
|
|
|
|
2021-12-07 02:25:28 +08:00
|
|
|
switch (setAction) {
|
|
|
|
case MessageSetAction.SetSeen:
|
2021-12-07 07:05:55 +08:00
|
|
|
add('\\seen');
|
2021-12-07 02:25:28 +08:00
|
|
|
break;
|
|
|
|
case MessageSetAction.UnsetSeen:
|
|
|
|
remove('\\seen');
|
|
|
|
break;
|
|
|
|
case MessageSetAction.SetFlag:
|
2021-12-07 07:05:55 +08:00
|
|
|
add('\\flagged');
|
2021-12-07 02:25:28 +08:00
|
|
|
break;
|
|
|
|
case MessageSetAction.UnsetFlag:
|
|
|
|
remove('\\flagged');
|
|
|
|
break;
|
|
|
|
// no default
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2021-12-07 07:05:55 +08:00
|
|
|
this.setFor(folder, uid, flags);
|
2021-12-07 02:25:28 +08:00
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
return unread;
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
}
|