snappymail/dev/Common/Cache.js

249 lines
5.7 KiB
JavaScript
Raw Normal View History

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 = {},
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 = {};
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
*/
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 =>
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) =>
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 =>
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) =>
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 =>
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
{
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);
}
/**
* @param {string} folderFullName
* @param {string} uid
* @returns {?Array}
*/
static getFor(folderFullName, uid) {
return MESSAGE_FLAGS_CACHE[folderFullName] && MESSAGE_FLAGS_CACHE[folderFullName][uid];
2015-11-19 01:32:29 +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
}
}
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),
thread = message.threads;
if (isArray(flags)) {
message.flags(flags);
}
2016-06-17 07:23:49 +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
);
const flaggedSubUid = thread.find(iSubUid =>
(uid !== iSubUid) && this.hasFlag(message.folder, iSubUid, '\\flagged')
2021-12-07 07:05:55 +08:00
);
2021-12-07 02:25:28 +08:00
message.hasUnseenSubMessage(!!unseenSubUid);
message.hasFlaggedSubMessage(!!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.flags());
}
2015-11-19 01:32:29 +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);
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
return unread;
2015-11-19 01:32:29 +08:00
}
2016-06-17 07:23:49 +08:00
}