snappymail/dev/Common/Cache.js

310 lines
7.2 KiB
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import _ from '_';
2020-03-11 21:17:52 +08:00
import { MessageSetAction } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { trim, pInt, 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 = {},
FOLDERS_HASH_CACHE = {},
FOLDERS_UID_NEXT_CACHE = {},
MESSAGE_FLAGS_CACHE = {},
NEW_MESSAGE_CACHE = {},
inboxFolderName = '';
2016-06-17 07:23:49 +08:00
2020-03-11 21:17:52 +08:00
const REQUESTED_MESSAGE_CACHE = {};
2016-06-17 07:23:49 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function clear() {
2016-06-17 07:23:49 +08:00
FOLDERS_CACHE = {};
FOLDERS_NAME_CACHE = {};
FOLDERS_HASH_CACHE = {};
FOLDERS_UID_NEXT_CACHE = {};
MESSAGE_FLAGS_CACHE = {};
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} email
* @param {Function} callback
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getUserPic(email, callback) {
2016-06-17 07:23:49 +08:00
email = trim(email);
2020-03-11 21:17:52 +08:00
callback('', email);
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
* @param {string} uid
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getMessageKey(folderFullNameRaw, uid) {
2016-06-17 07:23:49 +08:00
return `${folderFullNameRaw}#${uid}`;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folder
* @param {string} uid
*/
2019-07-05 03:19:24 +08:00
export function addRequestedMessage(folder, uid) {
2016-06-17 07:23:49 +08:00
REQUESTED_MESSAGE_CACHE[getMessageKey(folder, uid)] = true;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folder
* @param {string} uid
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function hasRequestedMessage(folder, uid) {
2016-06-17 07:23:49 +08:00
return true === REQUESTED_MESSAGE_CACHE[getMessageKey(folder, uid)];
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
* @param {string} uid
*/
2019-07-05 03:19:24 +08:00
export function addNewMessageCache(folderFullNameRaw, uid) {
2016-06-17 07:23:49 +08:00
NEW_MESSAGE_CACHE[getMessageKey(folderFullNameRaw, uid)] = true;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
* @param {string} uid
*/
2019-07-05 03:19:24 +08:00
export function hasNewMessageAndRemoveFromCache(folderFullNameRaw, uid) {
if (NEW_MESSAGE_CACHE[getMessageKey(folderFullNameRaw, uid)]) {
2016-06-17 07:23:49 +08:00
NEW_MESSAGE_CACHE[getMessageKey(folderFullNameRaw, uid)] = null;
return true;
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
return false;
}
2015-11-19 01:32:29 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function clearNewMessageCache() {
2016-06-17 07:23:49 +08:00
NEW_MESSAGE_CACHE = {};
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getFolderInboxName() {
2016-06-17 07:23:49 +08:00
return '' === inboxFolderName ? 'INBOX' : inboxFolderName;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderHash
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getFolderFullNameRaw(folderHash) {
2016-06-17 07:23:49 +08:00
return '' !== folderHash && FOLDERS_NAME_CACHE[folderHash] ? FOLDERS_NAME_CACHE[folderHash] : '';
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderHash
* @param {string} folderFullNameRaw
*/
2019-07-05 03:19:24 +08:00
export function setFolderFullNameRaw(folderHash, folderFullNameRaw) {
2016-06-17 07:23:49 +08:00
FOLDERS_NAME_CACHE[folderHash] = folderFullNameRaw;
2019-07-05 03:19:24 +08:00
if ('INBOX' === folderFullNameRaw || '' === inboxFolderName) {
2016-06-17 07:23:49 +08:00
inboxFolderName = folderFullNameRaw;
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getFolderHash(folderFullNameRaw) {
2016-06-17 07:23:49 +08:00
return '' !== folderFullNameRaw && FOLDERS_HASH_CACHE[folderFullNameRaw] ? FOLDERS_HASH_CACHE[folderFullNameRaw] : '';
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
* @param {string} folderHash
*/
2019-07-05 03:19:24 +08:00
export function setFolderHash(folderFullNameRaw, folderHash) {
if ('' !== folderFullNameRaw) {
2016-06-17 07:23:49 +08:00
FOLDERS_HASH_CACHE[folderFullNameRaw] = folderHash;
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getFolderUidNext(folderFullNameRaw) {
return '' !== folderFullNameRaw && FOLDERS_UID_NEXT_CACHE[folderFullNameRaw]
? FOLDERS_UID_NEXT_CACHE[folderFullNameRaw]
: '';
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
* @param {string} uidNext
*/
2019-07-05 03:19:24 +08:00
export function setFolderUidNext(folderFullNameRaw, uidNext) {
2016-06-17 07:23:49 +08:00
FOLDERS_UID_NEXT_CACHE[folderFullNameRaw] = uidNext;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
2016-06-30 08:02:45 +08:00
* @returns {?FolderModel}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getFolderFromCacheList(folderFullNameRaw) {
2016-06-17 07:23:49 +08:00
return '' !== folderFullNameRaw && FOLDERS_CACHE[folderFullNameRaw] ? FOLDERS_CACHE[folderFullNameRaw] : null;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
* @param {?FolderModel} folder
*/
2019-07-05 03:19:24 +08:00
export function setFolderToCacheList(folderFullNameRaw, folder) {
2016-06-17 07:23:49 +08:00
FOLDERS_CACHE[folderFullNameRaw] = folder;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullNameRaw
*/
2019-07-05 03:19:24 +08:00
export function removeFolderFromCacheList(folderFullNameRaw) {
2016-06-17 07:23:49 +08:00
setFolderToCacheList(folderFullNameRaw, null);
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullName
* @param {string} uid
2016-06-30 08:02:45 +08:00
* @returns {?Array}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getMessageFlagsFromCache(folderFullName, uid) {
return MESSAGE_FLAGS_CACHE[folderFullName] && MESSAGE_FLAGS_CACHE[folderFullName][uid]
? MESSAGE_FLAGS_CACHE[folderFullName][uid]
: null;
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullName
* @param {string} uid
* @param {Array} flagsCache
*/
2019-07-05 03:19:24 +08:00
export function setMessageFlagsToCache(folderFullName, uid, flagsCache) {
if (!MESSAGE_FLAGS_CACHE[folderFullName]) {
2016-06-17 07:23:49 +08:00
MESSAGE_FLAGS_CACHE[folderFullName] = {};
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
MESSAGE_FLAGS_CACHE[folderFullName][uid] = flagsCache;
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folderFullName
*/
2019-07-05 03:19:24 +08:00
export function clearMessageFlagsFromCacheByFolder(folderFullName) {
2016-06-17 07:23:49 +08:00
MESSAGE_FLAGS_CACHE[folderFullName] = {};
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {(MessageModel|null)} message
*/
2019-07-05 03:19:24 +08:00
export function initMessageFlagsFromCache(message) {
if (message) {
const uid = message.uid,
2016-06-30 08:02:45 +08:00
flags = getMessageFlagsFromCache(message.folderFullNameRaw, uid);
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
if (flags && 0 < flags.length) {
2016-06-17 07:23:49 +08:00
message.flagged(!!flags[1]);
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
if (!message.isSimpleMessage) {
2016-06-17 07:23:49 +08:00
message.unseen(!!flags[0]);
message.answered(!!flags[2]);
message.forwarded(!!flags[3]);
message.isReadReceipt(!!flags[4]);
message.deletedMark(!!flags[5]);
2015-11-19 01:32:29 +08:00
}
}
2019-07-05 03:19:24 +08:00
if (0 < message.threads().length) {
2016-06-17 07:23:49 +08:00
const unseenSubUid = _.find(message.threads(), (sSubUid) => {
if (uid !== sSubUid) {
const subFlags = getMessageFlagsFromCache(message.folderFullNameRaw, sSubUid);
return subFlags && 0 < subFlags.length && !!subFlags[0];
}
return false;
});
const flaggedSubUid = _.find(message.threads(), (sSubUid) => {
if (uid !== sSubUid) {
const subFlags = getMessageFlagsFromCache(message.folderFullNameRaw, sSubUid);
return subFlags && 0 < 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
}
}
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {(MessageModel|null)} message
*/
2019-07-05 03:19:24 +08:00
export function storeMessageFlagsToCache(message) {
if (message) {
setMessageFlagsToCache(message.folderFullNameRaw, message.uid, [
message.unseen(),
message.flagged(),
message.answered(),
message.forwarded(),
message.isReadReceipt(),
message.deletedMark()
]);
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folder
* @param {string} uid
* @param {Array} flags
*/
2019-07-05 03:19:24 +08:00
export function storeMessageFlagsToCacheByFolderAndUid(folder, uid, flags) {
if (isArray(flags) && 0 < flags.length) {
2016-06-17 07:23:49 +08:00
setMessageFlagsToCache(folder, uid, flags);
}
}
2015-11-19 01:32:29 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {string} folder
* @param {string} uid
* @param {number} setAction
*/
2019-07-05 03:19:24 +08:00
export function storeMessageFlagsToCacheBySetAction(folder, uid, setAction) {
2016-06-17 07:23:49 +08:00
let unread = 0;
const flags = getMessageFlagsFromCache(folder, uid);
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
if (isArray(flags) && 0 < flags.length) {
if (flags[0]) {
2016-06-17 07:23:49 +08:00
unread = 1;
}
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
switch (setAction) {
2016-06-17 07:23:49 +08:00
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;
2016-06-30 08:02:45 +08:00
// no default
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
setMessageFlagsToCache(folder, uid, flags);
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
return unread;
}