snappymail/dev/Common/Cache.js

175 lines
3.8 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 = {},
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 = {};
},
/**
2021-11-30 17:19:43 +08:00
* @param {string} folderFullName
2021-08-20 21:40:07 +08:00
* @param {string} uid
* @returns {string}
*/
2022-04-01 00:52:46 +08:00
getMessageKey = (folderFullName, uid) => folderFullName + '#' + uid,
2021-08-20 21:40:07 +08:00
/**
* @returns {string}
*/
getFolderInboxName = () => inboxFolderName,
/**
* @returns {string}
*/
setFolderInboxName = name => inboxFolderName = name,
/**
* @param {string} fullNameHash
2021-08-20 21:40:07 +08:00
* @returns {string}
*/
getFolderFullName = fullNameHash => (fullNameHash && FOLDERS_NAME_CACHE[fullNameHash]) || '',
2021-08-20 21:40:07 +08:00
/**
* @param {?FolderModel} folder
*/
setFolder = folder => {
folder.etag = '';
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
* @param {string} folderETag
2021-08-20 21:40:07 +08:00
*/
setFolderETag = (folderFullName, folderETag) =>
FOLDERS_CACHE[folderFullName] && (FOLDERS_CACHE[folderFullName].etag = folderETag),
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 =>
2023-01-09 19:28:07 +08:00
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) {
2022-09-02 17:52:07 +08:00
return MESSAGE_FLAGS_CACHE[folderFullName]?.[uid]?.includes(flag);
2021-12-07 07:05:55 +08:00
}
/**
* @param {string} folderFullName
* @param {string} uid
* @returns {?Array}
*/
static getFor(folderFullName, uid) {
2022-09-02 17:52:07 +08:00
return 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),
2022-06-08 19:08:27 +08:00
thread = message.threads();
2022-10-10 19:52:56 +08:00
isArray(flags) && message.flags(flags);
2016-06-17 07:23:49 +08:00
if (thread.length) {
message.hasUnseenSubMessage(!!thread.find(iSubUid =>
(uid !== iSubUid) && !this.hasFlag(message.folder, iSubUid, '\\seen')
));
message.hasFlaggedSubMessage(!!thread.find(iSubUid =>
(uid !== iSubUid) && this.hasFlag(message.folder, iSubUid, '\\flagged')
));
}
2015-11-19 01:32:29 +08:00
}
}
/**
* @param {(MessageModel|null)} message
*/
static store(message) {
2022-06-08 19:08:27 +08:00
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
}