snappymail/dev/Common/Cache.js

278 lines
6.3 KiB
JavaScript
Raw Normal View History

2021-01-25 05:58:06 +08:00
import { MessageSetAction } from 'Common/EnumsUser';
2021-07-22 03:34:17 +08:00
import { arrayLength, pInt } 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 = {},
FOLDERS_HASH_CACHE = {},
FOLDERS_UID_NEXT_CACHE = {},
MESSAGE_FLAGS_CACHE = {},
NEW_MESSAGE_CACHE = {},
REQUESTED_MESSAGE_CACHE = {},
inboxFolderName = 'INBOX';
2016-06-17 07:23:49 +08:00
2021-08-20 21:40:07 +08:00
export const
/**
* @returns {void}
*/
clearCache = () => {
2021-08-20 21:40:07 +08:00
FOLDERS_CACHE = {};
FOLDERS_NAME_CACHE = {};
FOLDERS_HASH_CACHE = {};
FOLDERS_UID_NEXT_CACHE = {};
MESSAGE_FLAGS_CACHE = {};
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-11-30 17:19:43 +08:00
setFolder = (folderHash, folderFullName, folder) => {
FOLDERS_CACHE[folderFullName] = folder;
FOLDERS_NAME_CACHE[folderHash] = folderFullName;
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 =>
folderFullName && FOLDERS_HASH_CACHE[folderFullName] ? FOLDERS_HASH_CACHE[folderFullName] : '',
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) =>
folderFullName && (FOLDERS_HASH_CACHE[folderFullName] = 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 =>
folderFullName && FOLDERS_UID_NEXT_CACHE[folderFullName]
? FOLDERS_UID_NEXT_CACHE[folderFullName]
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) =>
FOLDERS_UID_NEXT_CACHE[folderFullName] = 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 =>
folderFullName && 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
export class MessageFlagsCache
{
/**
* @param {string} folderFullName
* @param {string} uid
* @returns {?Array}
*/
static getFor(folderFullName, uid) {
return MESSAGE_FLAGS_CACHE[folderFullName] && MESSAGE_FLAGS_CACHE[folderFullName][uid]
? MESSAGE_FLAGS_CACHE[folderFullName][uid]
: null;
2015-11-19 01:32:29 +08:00
}
/**
* @param {string} folderFullName
* @param {string} uid
* @param {Array} flagsCache
*/
static setFor(folderFullName, uid, flagsCache) {
if (!MESSAGE_FLAGS_CACHE[folderFullName]) {
MESSAGE_FLAGS_CACHE[folderFullName] = {};
2015-11-19 01:32:29 +08:00
}
MESSAGE_FLAGS_CACHE[folderFullName][uid] = flagsCache;
}
2015-11-19 01:32:29 +08:00
/**
* @param {string} folderFullName
*/
static clearFolder(folderFullName) {
MESSAGE_FLAGS_CACHE[folderFullName] = {};
}
2016-06-17 07:23:49 +08:00
/**
* @param {(MessageModel|null)} message
*/
static initMessage(message) {
if (message) {
const uid = message.uid,
flags = this.getFor(message.folder, uid);
if (flags && flags.length) {
message.isFlagged(!!flags[1]);
if (!message.isSimpleMessage) {
message.isUnseen(!!flags[0]);
message.isAnswered(!!flags[2]);
message.isForwarded(!!flags[3]);
message.isReadReceipt(!!flags[4]);
message.isDeleted(!!flags[5]);
2016-06-17 07:23:49 +08:00
}
}
2016-06-17 07:23:49 +08:00
if (message.threads.length) {
const unseenSubUid = message.threads.find(sSubUid => {
if (uid !== sSubUid) {
const subFlags = this.getFor(message.folder, sSubUid);
return subFlags && subFlags.length && !!subFlags[0];
}
return false;
});
const flaggedSubUid = message.threads.find(sSubUid => {
if (uid !== sSubUid) {
const subFlags = this.getFor(message.folder, sSubUid);
return subFlags && subFlags.length && !!subFlags[1];
}
return false;
});
message.hasUnseenSubMessage(unseenSubUid && 0 < pInt(unseenSubUid));
message.hasFlaggedSubMessage(flaggedSubUid && 0 < pInt(flaggedSubUid));
}
2015-11-19 01:32:29 +08:00
}
}
/**
* @param {(MessageModel|null)} message
*/
static store(message) {
if (message) {
this.setFor(message.folder, message.uid, [
message.isUnseen(),
message.isFlagged(),
message.isAnswered(),
message.isForwarded(),
message.isReadReceipt(),
message.isDeleted()
]);
}
2015-11-19 01:32:29 +08:00
}
/**
* @param {string} folder
* @param {string} uid
* @param {Array} flags
*/
static storeByFolderAndUid(folder, uid, flags) {
2021-07-22 03:34:17 +08:00
if (arrayLength(flags)) {
this.setFor(folder, uid, flags);
}
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
/**
* @param {string} folder
* @param {string} uid
* @param {number} setAction
*/
static storeBySetAction(folder, uid, setAction) {
let unread = 0;
const flags = this.getFor(folder, uid);
2021-07-22 03:34:17 +08:00
if (arrayLength(flags)) {
if (flags[0]) {
unread = 1;
}
2015-11-19 01:32:29 +08:00
switch (setAction) {
case MessageSetAction.SetSeen:
flags[0] = false;
break;
case MessageSetAction.UnsetSeen:
flags[0] = true;
break;
case MessageSetAction.SetFlag:
flags[1] = true;
break;
case MessageSetAction.UnsetFlag:
flags[1] = false;
break;
// no default
}
2015-11-19 01:32:29 +08:00
this.setFor(folder, uid, flags);
2015-11-19 01:32:29 +08:00
}
return unread;
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
}