snappymail/dev/Storage/User/Cache.js
2014-10-18 17:43:44 +04:00

366 lines
No EOL
8.7 KiB
JavaScript

/* RainLoop Webmail (c) RainLoop Team | Licensed under CC BY-NC-SA 3.0 */
(function () {
'use strict';
var
_ = require('_'),
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Links = require('Common/Links'),
Settings = require('Storage/Settings')
;
/**
* @constructor
*/
function CacheUserStorage()
{
this.oFoldersCache = {};
this.oFoldersNamesCache = {};
this.oFolderHashCache = {};
this.oFolderUidNextCache = {};
this.oMessageListHashCache = {};
this.oMessageFlagsCache = {};
this.oNewMessage = {};
this.oRequestedMessage = {};
this.bCapaGravatar = Settings.capa(Enums.Capa.Gravatar);
}
/**
* @type {boolean}
*/
CacheUserStorage.prototype.bCapaGravatar = false;
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFoldersCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFoldersNamesCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFolderHashCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oFolderUidNextCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oMessageListHashCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oMessageFlagsCache = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oBodies = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oNewMessage = {};
/**
* @type {Object}
*/
CacheUserStorage.prototype.oRequestedMessage = {};
CacheUserStorage.prototype.clear = function ()
{
this.oFoldersCache = {};
this.oFoldersNamesCache = {};
this.oFolderHashCache = {};
this.oFolderUidNextCache = {};
this.oMessageListHashCache = {};
this.oMessageFlagsCache = {};
this.oBodies = {};
};
/**
* @param {string} sEmail
* @param {Function} fCallback
* @return {string}
*/
CacheUserStorage.prototype.getUserPic = function (sEmail, fCallback)
{
sEmail = Utils.trim(sEmail);
fCallback(this.bCapaGravatar && '' !== sEmail ? Links.avatarLink(sEmail) : '', sEmail);
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUid
* @return {string}
*/
CacheUserStorage.prototype.getMessageKey = function (sFolderFullNameRaw, sUid)
{
return sFolderFullNameRaw + '#' + sUid;
};
/**
* @param {string} sFolder
* @param {string} sUid
*/
CacheUserStorage.prototype.addRequestedMessage = function (sFolder, sUid)
{
this.oRequestedMessage[this.getMessageKey(sFolder, sUid)] = true;
};
/**
* @param {string} sFolder
* @param {string} sUid
* @return {boolean}
*/
CacheUserStorage.prototype.hasRequestedMessage = function (sFolder, sUid)
{
return true === this.oRequestedMessage[this.getMessageKey(sFolder, sUid)];
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUid
*/
CacheUserStorage.prototype.addNewMessageCache = function (sFolderFullNameRaw, sUid)
{
this.oNewMessage[this.getMessageKey(sFolderFullNameRaw, sUid)] = true;
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUid
*/
CacheUserStorage.prototype.hasNewMessageAndRemoveFromCache = function (sFolderFullNameRaw, sUid)
{
if (this.oNewMessage[this.getMessageKey(sFolderFullNameRaw, sUid)])
{
this.oNewMessage[this.getMessageKey(sFolderFullNameRaw, sUid)] = null;
return true;
}
return false;
};
CacheUserStorage.prototype.clearNewMessageCache = function ()
{
this.oNewMessage = {};
};
/**
* @type {string}
*/
CacheUserStorage.prototype.sInboxFolderName = '';
/**
* @return {string}
*/
CacheUserStorage.prototype.getFolderInboxName = function ()
{
return '' === this.sInboxFolderName ? 'INBOX' : this.sInboxFolderName;
};
/**
* @param {string} sFolderHash
* @return {string}
*/
CacheUserStorage.prototype.getFolderFullNameRaw = function (sFolderHash)
{
return '' !== sFolderHash && this.oFoldersNamesCache[sFolderHash] ? this.oFoldersNamesCache[sFolderHash] : '';
};
/**
* @param {string} sFolderHash
* @param {string} sFolderFullNameRaw
*/
CacheUserStorage.prototype.setFolderFullNameRaw = function (sFolderHash, sFolderFullNameRaw)
{
this.oFoldersNamesCache[sFolderHash] = sFolderFullNameRaw;
if ('INBOX' === sFolderFullNameRaw || '' === this.sInboxFolderName)
{
this.sInboxFolderName = sFolderFullNameRaw;
}
};
/**
* @param {string} sFolderFullNameRaw
* @return {string}
*/
CacheUserStorage.prototype.getFolderHash = function (sFolderFullNameRaw)
{
return '' !== sFolderFullNameRaw && this.oFolderHashCache[sFolderFullNameRaw] ? this.oFolderHashCache[sFolderFullNameRaw] : '';
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sFolderHash
*/
CacheUserStorage.prototype.setFolderHash = function (sFolderFullNameRaw, sFolderHash)
{
this.oFolderHashCache[sFolderFullNameRaw] = sFolderHash;
};
/**
* @param {string} sFolderFullNameRaw
* @return {string}
*/
CacheUserStorage.prototype.getFolderUidNext = function (sFolderFullNameRaw)
{
return '' !== sFolderFullNameRaw && this.oFolderUidNextCache[sFolderFullNameRaw] ? this.oFolderUidNextCache[sFolderFullNameRaw] : '';
};
/**
* @param {string} sFolderFullNameRaw
* @param {string} sUidNext
*/
CacheUserStorage.prototype.setFolderUidNext = function (sFolderFullNameRaw, sUidNext)
{
this.oFolderUidNextCache[sFolderFullNameRaw] = sUidNext;
};
/**
* @param {string} sFolderFullNameRaw
* @return {?FolderModel}
*/
CacheUserStorage.prototype.getFolderFromCacheList = function (sFolderFullNameRaw)
{
return '' !== sFolderFullNameRaw && this.oFoldersCache[sFolderFullNameRaw] ? this.oFoldersCache[sFolderFullNameRaw] : null;
};
/**
* @param {string} sFolderFullNameRaw
* @param {?FolderModel} oFolder
*/
CacheUserStorage.prototype.setFolderToCacheList = function (sFolderFullNameRaw, oFolder)
{
this.oFoldersCache[sFolderFullNameRaw] = oFolder;
};
/**
* @param {string} sFolderFullNameRaw
*/
CacheUserStorage.prototype.removeFolderFromCacheList = function (sFolderFullNameRaw)
{
this.setFolderToCacheList(sFolderFullNameRaw, null);
};
/**
* @param {string} sFolderFullName
* @param {string} sUid
* @return {?Array}
*/
CacheUserStorage.prototype.getMessageFlagsFromCache = function (sFolderFullName, sUid)
{
return this.oMessageFlagsCache[sFolderFullName] && this.oMessageFlagsCache[sFolderFullName][sUid] ?
this.oMessageFlagsCache[sFolderFullName][sUid] : null;
};
/**
* @param {string} sFolderFullName
* @param {string} sUid
* @param {Array} aFlagsCache
*/
CacheUserStorage.prototype.setMessageFlagsToCache = function (sFolderFullName, sUid, aFlagsCache)
{
if (!this.oMessageFlagsCache[sFolderFullName])
{
this.oMessageFlagsCache[sFolderFullName] = {};
}
this.oMessageFlagsCache[sFolderFullName][sUid] = aFlagsCache;
};
/**
* @param {string} sFolderFullName
*/
CacheUserStorage.prototype.clearMessageFlagsFromCacheByFolder = function (sFolderFullName)
{
this.oMessageFlagsCache[sFolderFullName] = {};
};
/**
* @param {(MessageModel|null)} oMessage
*/
CacheUserStorage.prototype.initMessageFlagsFromCache = function (oMessage)
{
if (oMessage)
{
var
self = this,
aFlags = this.getMessageFlagsFromCache(oMessage.folderFullNameRaw, oMessage.uid),
mUnseenSubUid = null,
mFlaggedSubUid = null
;
if (aFlags && 0 < aFlags.length)
{
oMessage.unseen(!!aFlags[0]);
oMessage.flagged(!!aFlags[1]);
oMessage.answered(!!aFlags[2]);
oMessage.forwarded(!!aFlags[3]);
oMessage.isReadReceipt(!!aFlags[4]);
}
if (0 < oMessage.threads().length)
{
mUnseenSubUid = _.find(oMessage.threads(), function (iSubUid) {
var aFlags = self.getMessageFlagsFromCache(oMessage.folderFullNameRaw, iSubUid);
return aFlags && 0 < aFlags.length && !!aFlags[0];
});
mFlaggedSubUid = _.find(oMessage.threads(), function (iSubUid) {
var aFlags = self.getMessageFlagsFromCache(oMessage.folderFullNameRaw, iSubUid);
return aFlags && 0 < aFlags.length && !!aFlags[1];
});
oMessage.hasUnseenSubMessage(mUnseenSubUid && 0 < Utils.pInt(mUnseenSubUid));
oMessage.hasFlaggedSubMessage(mFlaggedSubUid && 0 < Utils.pInt(mFlaggedSubUid));
}
}
};
/**
* @param {(MessageModel|null)} oMessage
*/
CacheUserStorage.prototype.storeMessageFlagsToCache = function (oMessage)
{
if (oMessage)
{
this.setMessageFlagsToCache(
oMessage.folderFullNameRaw,
oMessage.uid,
[oMessage.unseen(), oMessage.flagged(), oMessage.answered(), oMessage.forwarded(), oMessage.isReadReceipt()]
);
}
};
/**
* @param {string} sFolder
* @param {string} sUid
* @param {Array} aFlags
*/
CacheUserStorage.prototype.storeMessageFlagsToCacheByFolderAndUid = function (sFolder, sUid, aFlags)
{
if (Utils.isArray(aFlags) && 0 < aFlags.length)
{
this.setMessageFlagsToCache(sFolder, sUid, aFlags);
}
};
module.exports = new CacheUserStorage();
}());