snappymail/dev/Model/Message.js

1014 lines
24 KiB
JavaScript
Raw Normal View History

2016-06-30 08:02:45 +08:00
var
_ = require('_'),
$ = require('$'),
ko = require('ko'),
Enums = require('Common/Enums'),
Utils = require('Common/Utils'),
Globals = require('Common/Globals'),
Links = require('Common/Links'),
AttachmentModel = require('Model/Attachment'),
MessageHelper = require('Helper/Message').default,
AbstractModel = require('Knoin/AbstractModel');
/**
* @constructor
*/
function MessageModel()
{
AbstractModel.call(this, 'MessageModel');
this.folderFullNameRaw = '';
this.uid = '';
this.hash = '';
this.requestHash = '';
this.subject = ko.observable('');
this.subjectPrefix = ko.observable('');
this.subjectSuffix = ko.observable('');
this.size = ko.observable(0);
this.dateTimeStampInUTC = ko.observable(0);
this.priority = ko.observable(Enums.MessagePriority.Normal);
this.proxy = false;
this.fromEmailString = ko.observable('');
this.fromClearEmailString = ko.observable('');
this.toEmailsString = ko.observable('');
this.toClearEmailsString = ko.observable('');
this.senderEmailsString = ko.observable('');
this.senderClearEmailsString = ko.observable('');
this.emails = [];
this.from = [];
this.to = [];
this.cc = [];
this.bcc = [];
this.replyTo = [];
this.deliveredTo = [];
this.newForAnimation = ko.observable(false);
this.deleted = ko.observable(false);
this.deletedMark = ko.observable(false);
this.unseen = ko.observable(false);
this.flagged = ko.observable(false);
this.answered = ko.observable(false);
this.forwarded = ko.observable(false);
this.isReadReceipt = ko.observable(false);
this.focused = ko.observable(false);
this.selected = ko.observable(false);
this.checked = ko.observable(false);
this.hasAttachments = ko.observable(false);
this.attachmentsSpecData = ko.observableArray([]);
this.attachmentIconClass = ko.computed(function() {
return AttachmentModel.staticCombinedIconClass(
this.hasAttachments() ? this.attachmentsSpecData() : []);
}, this);
this.body = null;
this.isHtml = ko.observable(false);
this.hasImages = ko.observable(false);
this.attachments = ko.observableArray([]);
this.isPgpSigned = ko.observable(false);
this.isPgpEncrypted = ko.observable(false);
this.pgpSignedVerifyStatus = ko.observable(Enums.SignedVerifyStatus.None);
this.pgpSignedVerifyUser = ko.observable('');
this.priority = ko.observable(Enums.MessagePriority.Normal);
this.readReceipt = ko.observable('');
this.aDraftInfo = [];
this.sMessageId = '';
this.sInReplyTo = '';
this.sReferences = '';
this.hasUnseenSubMessage = ko.observable(false);
this.hasFlaggedSubMessage = ko.observable(false);
this.threads = ko.observableArray([]);
this.threadsLen = ko.computed(function() {
return this.threads().length;
}, this);
this.isImportant = ko.computed(function() {
return Enums.MessagePriority.High === this.priority();
}, this);
this.regDisposables([this.attachmentIconClass, this.threadsLen, this.isImportant]);
}
_.extend(MessageModel.prototype, AbstractModel.prototype);
/**
* @static
* @param {AjaxJsonMessage} oJsonMessage
* @returns {?MessageModel}
*/
MessageModel.newInstanceFromJson = function(oJsonMessage)
{
var oMessageModel = new MessageModel();
return oMessageModel.initByJson(oJsonMessage) ? oMessageModel : null;
};
MessageModel.prototype.clear = function()
{
this.folderFullNameRaw = '';
this.uid = '';
this.hash = '';
this.requestHash = '';
this.subject('');
this.subjectPrefix('');
this.subjectSuffix('');
this.size(0);
this.dateTimeStampInUTC(0);
this.priority(Enums.MessagePriority.Normal);
this.proxy = false;
this.fromEmailString('');
this.fromClearEmailString('');
this.toEmailsString('');
this.toClearEmailsString('');
this.senderEmailsString('');
this.senderClearEmailsString('');
this.emails = [];
this.from = [];
this.to = [];
this.cc = [];
this.bcc = [];
this.replyTo = [];
this.deliveredTo = [];
this.newForAnimation(false);
this.deleted(false);
this.deletedMark(false);
this.unseen(false);
this.flagged(false);
this.answered(false);
this.forwarded(false);
this.isReadReceipt(false);
this.selected(false);
this.checked(false);
this.hasAttachments(false);
this.attachmentsSpecData([]);
this.body = null;
this.isHtml(false);
this.hasImages(false);
this.attachments([]);
this.isPgpSigned(false);
this.isPgpEncrypted(false);
this.pgpSignedVerifyStatus(Enums.SignedVerifyStatus.None);
this.pgpSignedVerifyUser('');
this.priority(Enums.MessagePriority.Normal);
this.readReceipt('');
this.aDraftInfo = [];
this.sMessageId = '';
this.sInReplyTo = '';
this.sReferences = '';
this.threads([]);
this.hasUnseenSubMessage(false);
this.hasFlaggedSubMessage(false);
};
/**
* @returns {Array}
*/
MessageModel.prototype.getRecipientsEmails = function()
{
return this.getEmails(['to', 'cc']);
};
/**
* @param {Array} aProperties
* @returns {Array}
*/
MessageModel.prototype.getEmails = function(aProperties)
{
var self = this;
return _.compact(_.uniq(_.map(_.reduce(aProperties, function(aCarry, sProperty) {
return aCarry.concat(self[sProperty]);
}, []), function(oItem) {
return oItem ? oItem.email : '';
})));
};
/**
* @returns {string}
*/
MessageModel.prototype.friendlySize = function()
{
return Utils.friendlySize(this.size());
};
MessageModel.prototype.computeSenderEmail = function()
{
2014-08-07 17:34:20 +08:00
var
2016-06-30 08:02:45 +08:00
sSent = require('Stores/User/Folder').sentFolder(),
sDraft = require('Stores/User/Folder').draftFolder();
this.senderEmailsString(this.folderFullNameRaw === sSent || this.folderFullNameRaw === sDraft ?
this.toEmailsString() : this.fromEmailString());
this.senderClearEmailsString(this.folderFullNameRaw === sSent || this.folderFullNameRaw === sDraft ?
this.toClearEmailsString() : this.fromClearEmailString());
};
/**
* @param {AjaxJsonMessage} oJsonMessage
* @returns {boolean}
*/
MessageModel.prototype.initByJson = function(oJsonMessage)
{
var
bResult = false,
iPriority = Enums.MessagePriority.Normal;
2016-06-30 08:02:45 +08:00
if (oJsonMessage && 'Object/Message' === oJsonMessage['@Object'])
{
2016-06-30 08:02:45 +08:00
iPriority = Utils.pInt(oJsonMessage.Priority);
this.priority(-1 < Utils.inArray(iPriority, [Enums.MessagePriority.High, Enums.MessagePriority.Low]) ?
iPriority : Enums.MessagePriority.Normal);
2016-06-30 08:02:45 +08:00
this.folderFullNameRaw = oJsonMessage.Folder;
this.uid = oJsonMessage.Uid;
this.hash = oJsonMessage.Hash;
this.requestHash = oJsonMessage.RequestHash;
2016-06-30 08:02:45 +08:00
this.proxy = !!oJsonMessage.ExternalProxy;
2016-06-30 08:02:45 +08:00
this.size(Utils.pInt(oJsonMessage.Size));
2016-06-30 08:02:45 +08:00
this.from = MessageHelper.emailArrayFromJson(oJsonMessage.From);
this.to = MessageHelper.emailArrayFromJson(oJsonMessage.To);
this.cc = MessageHelper.emailArrayFromJson(oJsonMessage.Cc);
this.bcc = MessageHelper.emailArrayFromJson(oJsonMessage.Bcc);
this.replyTo = MessageHelper.emailArrayFromJson(oJsonMessage.ReplyTo);
this.deliveredTo = MessageHelper.emailArrayFromJson(oJsonMessage.DeliveredTo);
2016-06-30 08:02:45 +08:00
this.subject(oJsonMessage.Subject);
if (Utils.isArray(oJsonMessage.SubjectParts))
{
2016-06-30 08:02:45 +08:00
this.subjectPrefix(oJsonMessage.SubjectParts[0]);
this.subjectSuffix(oJsonMessage.SubjectParts[1]);
}
else
{
this.subjectPrefix('');
this.subjectSuffix(this.subject());
}
2016-06-30 08:02:45 +08:00
this.dateTimeStampInUTC(Utils.pInt(oJsonMessage.DateTimeStampInUTC));
this.hasAttachments(!!oJsonMessage.HasAttachments);
this.attachmentsSpecData(Utils.isArray(oJsonMessage.AttachmentsSpecData) ?
oJsonMessage.AttachmentsSpecData : []);
2016-06-30 08:02:45 +08:00
this.fromEmailString(MessageHelper.emailArrayToString(this.from, true));
this.fromClearEmailString(MessageHelper.emailArrayToStringClear(this.from));
this.toEmailsString(MessageHelper.emailArrayToString(this.to, true));
this.toClearEmailsString(MessageHelper.emailArrayToStringClear(this.to));
2016-06-30 08:02:45 +08:00
this.threads(Utils.isArray(oJsonMessage.Threads) ? oJsonMessage.Threads : []);
2016-06-30 08:02:45 +08:00
this.initFlagsByJson(oJsonMessage);
this.computeSenderEmail();
2016-06-30 08:02:45 +08:00
bResult = true;
}
2016-06-30 08:02:45 +08:00
return bResult;
};
2016-06-30 08:02:45 +08:00
/**
* @param {AjaxJsonMessage} oJsonMessage
* @returns {boolean}
*/
MessageModel.prototype.initUpdateByMessageJson = function(oJsonMessage)
{
var
bResult = false,
iPriority = Enums.MessagePriority.Normal;
2016-06-30 08:02:45 +08:00
if (oJsonMessage && 'Object/Message' === oJsonMessage['@Object'])
{
2016-06-30 08:02:45 +08:00
iPriority = Utils.pInt(oJsonMessage.Priority);
this.priority(-1 < Utils.inArray(iPriority, [Enums.MessagePriority.High, Enums.MessagePriority.Low]) ?
iPriority : Enums.MessagePriority.Normal);
2016-06-30 08:02:45 +08:00
this.aDraftInfo = oJsonMessage.DraftInfo;
2016-06-30 08:02:45 +08:00
this.sMessageId = oJsonMessage.MessageId;
this.sInReplyTo = oJsonMessage.InReplyTo;
this.sReferences = oJsonMessage.References;
2016-06-30 08:02:45 +08:00
this.proxy = !!oJsonMessage.ExternalProxy;
2016-06-30 08:02:45 +08:00
if (require('Stores/User/Pgp').capaOpenPGP())
{
this.isPgpSigned(!!oJsonMessage.PgpSigned);
this.isPgpEncrypted(!!oJsonMessage.PgpEncrypted);
}
2014-01-29 00:09:41 +08:00
2016-06-30 08:02:45 +08:00
this.hasAttachments(!!oJsonMessage.HasAttachments);
this.attachmentsSpecData(Utils.isArray(oJsonMessage.AttachmentsSpecData) ?
oJsonMessage.AttachmentsSpecData : []);
2016-06-30 08:02:45 +08:00
this.foundedCIDs = Utils.isArray(oJsonMessage.FoundedCIDs) ? oJsonMessage.FoundedCIDs : [];
this.attachments(this.initAttachmentsFromJson(oJsonMessage.Attachments));
2016-06-30 08:02:45 +08:00
this.readReceipt(oJsonMessage.ReadReceipt || '');
2014-01-04 08:20:07 +08:00
2016-06-30 08:02:45 +08:00
this.computeSenderEmail();
2016-06-30 08:02:45 +08:00
bResult = true;
}
2016-06-30 08:02:45 +08:00
return bResult;
};
2016-06-30 08:02:45 +08:00
/**
* @param {(AjaxJsonAttachment|null)} oJsonAttachments
* @returns {Array}
*/
MessageModel.prototype.initAttachmentsFromJson = function(oJsonAttachments)
{
var
iIndex = 0,
iLen = 0,
oAttachmentModel = null,
aResult = [];
2016-06-30 08:02:45 +08:00
if (oJsonAttachments && 'Collection/AttachmentCollection' === oJsonAttachments['@Object'] &&
Utils.isNonEmptyArray(oJsonAttachments['@Collection']))
{
2016-06-30 08:02:45 +08:00
for (iIndex = 0, iLen = oJsonAttachments['@Collection'].length; iIndex < iLen; iIndex++)
{
2016-06-30 08:02:45 +08:00
oAttachmentModel = AttachmentModel.newInstanceFromJson(oJsonAttachments['@Collection'][iIndex]);
if (oAttachmentModel)
{
2016-06-30 08:02:45 +08:00
if ('' !== oAttachmentModel.cidWithOutTags && 0 < this.foundedCIDs.length &&
0 <= Utils.inArray(oAttachmentModel.cidWithOutTags, this.foundedCIDs))
{
2016-06-30 08:02:45 +08:00
oAttachmentModel.isLinked = true;
}
2016-06-30 08:02:45 +08:00
aResult.push(oAttachmentModel);
}
}
2016-06-30 08:02:45 +08:00
}
2016-06-30 08:02:45 +08:00
return aResult;
};
/**
* @param {AjaxJsonMessage} oJsonMessage
* @returns {boolean}
*/
MessageModel.prototype.initFlagsByJson = function(oJsonMessage)
{
var bResult = false;
2016-06-30 08:02:45 +08:00
if (oJsonMessage && 'Object/Message' === oJsonMessage['@Object'])
{
2016-06-30 08:02:45 +08:00
this.unseen(!oJsonMessage.IsSeen);
this.flagged(!!oJsonMessage.IsFlagged);
this.answered(!!oJsonMessage.IsAnswered);
this.forwarded(!!oJsonMessage.IsForwarded);
this.isReadReceipt(!!oJsonMessage.IsReadReceipt);
this.deletedMark(!!oJsonMessage.IsDeleted);
2016-06-30 08:02:45 +08:00
bResult = true;
}
2016-06-30 08:02:45 +08:00
return bResult;
};
/**
* @param {boolean} bFriendlyView
* @param {boolean=} bWrapWithLink = false
* @returns {string}
*/
MessageModel.prototype.fromToLine = function(bFriendlyView, bWrapWithLink)
{
return MessageHelper.emailArrayToString(this.from, bFriendlyView, bWrapWithLink);
};
/**
* @returns {string}
*/
MessageModel.prototype.fromDkimData = function()
{
var aResult = ['none', ''];
if (Utils.isNonEmptyArray(this.from) && 1 === this.from.length &&
this.from[0] && this.from[0].dkimStatus)
{
aResult = [this.from[0].dkimStatus, this.from[0].dkimValue || ''];
}
2016-06-30 08:02:45 +08:00
return aResult;
};
/**
* @param {boolean} bFriendlyView
* @param {boolean=} bWrapWithLink = false
* @returns {string}
*/
MessageModel.prototype.toToLine = function(bFriendlyView, bWrapWithLink)
{
return MessageHelper.emailArrayToString(this.to, bFriendlyView, bWrapWithLink);
};
/**
* @param {boolean} bFriendlyView
* @param {boolean=} bWrapWithLink = false
* @returns {string}
*/
MessageModel.prototype.ccToLine = function(bFriendlyView, bWrapWithLink)
{
return MessageHelper.emailArrayToString(this.cc, bFriendlyView, bWrapWithLink);
};
/**
* @param {boolean} bFriendlyView
* @param {boolean=} bWrapWithLink = false
* @returns {string}
*/
MessageModel.prototype.bccToLine = function(bFriendlyView, bWrapWithLink)
{
return MessageHelper.emailArrayToString(this.bcc, bFriendlyView, bWrapWithLink);
};
/**
* @param {boolean} bFriendlyView
* @param {boolean=} bWrapWithLink = false
* @returns {string}
*/
MessageModel.prototype.replyToToLine = function(bFriendlyView, bWrapWithLink)
{
return MessageHelper.emailArrayToString(this.replyTo, bFriendlyView, bWrapWithLink);
};
/**
* @return string
*/
MessageModel.prototype.lineAsCss = function()
{
var aResult = [];
if (this.deleted())
{
aResult.push('deleted');
}
if (this.deletedMark())
{
2016-06-30 08:02:45 +08:00
aResult.push('deleted-mark');
}
if (this.selected())
2015-01-13 07:24:08 +08:00
{
2016-06-30 08:02:45 +08:00
aResult.push('selected');
}
if (this.checked())
{
2016-06-30 08:02:45 +08:00
aResult.push('checked');
}
if (this.flagged())
{
2016-06-30 08:02:45 +08:00
aResult.push('flagged');
}
if (this.unseen())
{
2016-06-30 08:02:45 +08:00
aResult.push('unseen');
}
if (this.answered())
{
2016-06-30 08:02:45 +08:00
aResult.push('answered');
}
if (this.forwarded())
{
2016-06-30 08:02:45 +08:00
aResult.push('forwarded');
}
if (this.focused())
{
aResult.push('focused');
}
if (this.isImportant())
{
aResult.push('important');
}
if (this.hasAttachments())
{
aResult.push('withAttachments');
}
if (this.newForAnimation())
{
aResult.push('new');
}
if ('' === this.subject())
{
aResult.push('emptySubject');
}
2015-03-16 05:58:50 +08:00
// if (1 < this.threadsLen())
// {
// aResult.push('hasChildrenMessage');
// }
2016-06-30 08:02:45 +08:00
if (this.hasUnseenSubMessage())
{
2016-06-30 08:02:45 +08:00
aResult.push('hasUnseenSubMessage');
}
if (this.hasFlaggedSubMessage())
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
aResult.push('hasFlaggedSubMessage');
}
2016-06-30 08:02:45 +08:00
return aResult.join(' ');
};
/**
* @returns {boolean}
*/
MessageModel.prototype.hasVisibleAttachments = function()
{
return !!_.find(this.attachments(), function(oAttachment) {
return !oAttachment.isLinked;
});
};
/**
* @param {string} sCid
* @returns {*}
*/
MessageModel.prototype.findAttachmentByCid = function(sCid)
{
var
oResult = null,
aAttachments = this.attachments();
2016-06-30 08:02:45 +08:00
if (Utils.isNonEmptyArray(aAttachments))
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
sCid = sCid.replace(/^<+/, '').replace(/>+$/, '');
oResult = _.find(aAttachments, function(oAttachment) {
return sCid === oAttachment.cidWithOutTags;
});
}
2016-06-30 08:02:45 +08:00
return oResult || null;
};
2016-06-30 08:02:45 +08:00
/**
* @param {string} sContentLocation
* @returns {*}
*/
MessageModel.prototype.findAttachmentByContentLocation = function(sContentLocation)
{
var
oResult = null,
aAttachments = this.attachments();
2016-06-30 08:02:45 +08:00
if (Utils.isNonEmptyArray(aAttachments))
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
oResult = _.find(aAttachments, function(oAttachment) {
return sContentLocation === oAttachment.contentLocation;
});
}
2016-06-30 08:02:45 +08:00
return oResult || null;
};
/**
* @returns {string}
*/
MessageModel.prototype.messageId = function()
{
return this.sMessageId;
};
/**
* @returns {string}
*/
MessageModel.prototype.inReplyTo = function()
{
return this.sInReplyTo;
};
/**
* @returns {string}
*/
MessageModel.prototype.references = function()
{
return this.sReferences;
};
/**
* @returns {string}
*/
MessageModel.prototype.fromAsSingleEmail = function()
{
return Utils.isArray(this.from) && this.from[0] ? this.from[0].email : '';
};
/**
* @returns {string}
*/
MessageModel.prototype.viewLink = function()
{
return Links.messageViewLink(this.requestHash);
};
/**
* @returns {string}
*/
MessageModel.prototype.downloadLink = function()
{
return Links.messageDownloadLink(this.requestHash);
};
/**
* @param {Object} oExcludeEmails
* @param {boolean=} bLast = false
* @returns {Array}
*/
MessageModel.prototype.replyEmails = function(oExcludeEmails, bLast)
{
var
aResult = [],
oUnic = Utils.isUnd(oExcludeEmails) ? {} : oExcludeEmails;
2016-06-30 08:02:45 +08:00
MessageHelper.replyHelper(this.replyTo, oUnic, aResult);
if (0 === aResult.length)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
MessageHelper.replyHelper(this.from, oUnic, aResult);
}
2016-06-30 08:02:45 +08:00
if (0 === aResult.length && !bLast)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
return this.replyEmails({}, true);
}
2016-06-30 08:02:45 +08:00
return aResult;
};
2016-06-30 08:02:45 +08:00
/**
* @param {Object} oExcludeEmails
* @param {boolean=} bLast = false
* @returns {Array.<Array>}
*/
MessageModel.prototype.replyAllEmails = function(oExcludeEmails, bLast)
{
var
aData = [],
aToResult = [],
aCcResult = [],
oUnic = Utils.isUnd(oExcludeEmails) ? {} : oExcludeEmails;
2016-06-30 08:02:45 +08:00
MessageHelper.replyHelper(this.replyTo, oUnic, aToResult);
if (0 === aToResult.length)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
MessageHelper.replyHelper(this.from, oUnic, aToResult);
}
2015-03-31 08:12:05 +08:00
2016-06-30 08:02:45 +08:00
MessageHelper.replyHelper(this.to, oUnic, aToResult);
MessageHelper.replyHelper(this.cc, oUnic, aCcResult);
2016-06-30 08:02:45 +08:00
if (0 === aToResult.length && !bLast)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
aData = this.replyAllEmails({}, true);
return [aData[0], aCcResult];
}
2016-06-30 08:02:45 +08:00
return [aToResult, aCcResult];
};
/**
* @returns {string}
*/
MessageModel.prototype.textBodyToString = function()
{
return this.body ? this.body.html() : '';
};
/**
* @returns {string}
*/
MessageModel.prototype.attachmentsToStringLine = function()
{
var aAttachLines = _.map(this.attachments(), function(oItem) {
return oItem.fileName + ' (' + oItem.friendlySize + ')';
});
return aAttachLines && 0 < aAttachLines.length ? aAttachLines.join(', ') : '';
};
/**
* @returns {Object}
*/
MessageModel.prototype.getDataForWindowPopup = function()
{
return {
'popupFrom': this.fromToLine(false),
'popupTo': this.toToLine(false),
'popupCc': this.ccToLine(false),
'popupBcc': this.bccToLine(false),
'popupReplyTo': this.replyToToLine(false),
'popupSubject': this.subject(),
'popupIsHtml': this.isHtml(),
'popupDate': require('Common/Momentor').format(this.dateTimeStampInUTC(), 'FULL'),
'popupAttachments': this.attachmentsToStringLine(),
'popupBody': this.textBodyToString()
};
};
/**
* @param {boolean=} bPrint = false
*/
MessageModel.prototype.viewPopupMessage = function(bPrint)
{
this.showLazyExternalImagesInBody();
Utils.previewMessage(this.subject(), this.body, this.isHtml(), bPrint);
};
MessageModel.prototype.printMessage = function()
{
this.viewPopupMessage(true);
};
/**
* @returns {string}
*/
MessageModel.prototype.generateUid = function()
{
return this.folderFullNameRaw + '/' + this.uid;
};
/**
* @param {MessageModel} oMessage
* @returns {MessageModel}
*/
MessageModel.prototype.populateByMessageListItem = function(oMessage)
{
if (oMessage)
{
this.folderFullNameRaw = oMessage.folderFullNameRaw;
this.uid = oMessage.uid;
this.hash = oMessage.hash;
this.requestHash = oMessage.requestHash;
this.subject(oMessage.subject());
}
2015-03-31 08:12:05 +08:00
2016-06-30 08:02:45 +08:00
this.subjectPrefix(this.subjectPrefix());
this.subjectSuffix(this.subjectSuffix());
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
if (oMessage)
2014-04-02 02:03:37 +08:00
{
2016-06-30 08:02:45 +08:00
this.size(oMessage.size());
this.dateTimeStampInUTC(oMessage.dateTimeStampInUTC());
this.priority(oMessage.priority());
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
this.proxy = oMessage.proxy;
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
this.fromEmailString(oMessage.fromEmailString());
this.fromClearEmailString(oMessage.fromClearEmailString());
this.toEmailsString(oMessage.toEmailsString());
this.toClearEmailsString(oMessage.toClearEmailsString());
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
this.emails = oMessage.emails;
2016-06-30 08:02:45 +08:00
this.from = oMessage.from;
this.to = oMessage.to;
this.cc = oMessage.cc;
this.bcc = oMessage.bcc;
this.replyTo = oMessage.replyTo;
this.deliveredTo = oMessage.deliveredTo;
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
this.unseen(oMessage.unseen());
this.flagged(oMessage.flagged());
this.answered(oMessage.answered());
this.forwarded(oMessage.forwarded());
this.isReadReceipt(oMessage.isReadReceipt());
this.deletedMark(oMessage.deletedMark());
2015-07-07 02:46:44 +08:00
2016-06-30 08:02:45 +08:00
this.priority(oMessage.priority());
2016-06-30 08:02:45 +08:00
this.selected(oMessage.selected());
this.checked(oMessage.checked());
this.hasAttachments(oMessage.hasAttachments());
this.attachmentsSpecData(oMessage.attachmentsSpecData());
}
2015-07-07 02:46:44 +08:00
2016-06-30 08:02:45 +08:00
this.body = null;
2016-06-30 08:02:45 +08:00
this.aDraftInfo = [];
this.sMessageId = '';
this.sInReplyTo = '';
this.sReferences = '';
2016-06-30 08:02:45 +08:00
if (oMessage)
{
this.threads(oMessage.threads());
}
2016-06-30 08:02:45 +08:00
this.computeSenderEmail();
2016-06-30 08:02:45 +08:00
return this;
};
2016-06-30 08:02:45 +08:00
MessageModel.prototype.showLazyExternalImagesInBody = function()
{
if (this.body)
{
$('.lazy.lazy-inited[data-original]', this.body).each(function() {
$(this).attr('src', $(this).attr('data-original')).removeAttr('data-original');
});
}
};
2016-06-30 08:02:45 +08:00
MessageModel.prototype.showExternalImages = function(bLazy)
{
if (this.body && this.body.data('rl-has-images'))
{
bLazy = Utils.isUnd(bLazy) ? false : bLazy;
2016-06-30 08:02:45 +08:00
this.hasImages(false);
this.body.data('rl-has-images', false);
2016-06-30 08:02:45 +08:00
var sAttr = this.proxy ? 'data-x-additional-src' : 'data-x-src';
$('[' + sAttr + ']', this.body).each(function() {
if (bLazy && $(this).is('img'))
{
$(this)
.addClass('lazy')
.attr('data-original', $(this).attr(sAttr))
.removeAttr(sAttr);
}
else
{
$(this).attr('src', $(this).attr(sAttr)).removeAttr(sAttr);
}
});
2016-06-30 08:02:45 +08:00
sAttr = this.proxy ? 'data-x-additional-style-url' : 'data-x-style-url';
$('[' + sAttr + ']', this.body).each(function() {
var sStyle = Utils.trim($(this).attr('style'));
sStyle = '' === sStyle ? '' : (';' === sStyle.substr(-1) ? sStyle + ' ' : sStyle + '; ');
$(this).attr('style', sStyle + $(this).attr(sAttr)).removeAttr(sAttr);
});
2016-06-30 08:02:45 +08:00
if (bLazy)
2015-07-07 02:46:44 +08:00
{
2016-06-30 08:02:45 +08:00
$('img.lazy', this.body).addClass('lazy-inited').lazyload({
'threshold': 400,
'effect': 'fadeIn',
'skip_invisible': false,
'container': $('.RL-MailMessageView .messageView .messageItem .content')[0]
});
2016-06-30 08:02:45 +08:00
Globals.$win.resize();
}
2016-06-30 08:02:45 +08:00
Utils.windowResize(500);
}
};
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
MessageModel.prototype.showInternalImages = function(bLazy)
{
if (this.body && !this.body.data('rl-init-internal-images'))
2016-06-16 07:36:44 +08:00
{
2016-06-30 08:02:45 +08:00
this.body.data('rl-init-internal-images', true);
2016-06-16 07:36:44 +08:00
2016-06-30 08:02:45 +08:00
bLazy = Utils.isUnd(bLazy) ? false : bLazy;
var self = this;
2016-06-30 08:02:45 +08:00
$('[data-x-src-cid]', this.body).each(function() {
2016-06-30 08:02:45 +08:00
var oAttachment = self.findAttachmentByCid($(this).attr('data-x-src-cid'));
if (oAttachment && oAttachment.download)
{
if (bLazy && $(this).is('img'))
{
$(this)
.addClass('lazy')
2016-06-30 08:02:45 +08:00
.attr('data-original', oAttachment.linkPreview());
}
else
{
2016-06-30 08:02:45 +08:00
$(this).attr('src', oAttachment.linkPreview());
}
2016-06-30 08:02:45 +08:00
}
});
2016-06-30 08:02:45 +08:00
$('[data-x-src-location]', this.body).each(function() {
2016-06-30 08:02:45 +08:00
var oAttachment = self.findAttachmentByContentLocation($(this).attr('data-x-src-location'));
if (!oAttachment)
{
2016-06-30 08:02:45 +08:00
oAttachment = self.findAttachmentByCid($(this).attr('data-x-src-location'));
}
2016-06-30 08:02:45 +08:00
if (oAttachment && oAttachment.download)
{
if (bLazy && $(this).is('img'))
{
2016-06-30 08:02:45 +08:00
$(this)
.addClass('lazy')
.attr('data-original', oAttachment.linkPreview());
}
2016-06-30 08:02:45 +08:00
else
{
2016-06-30 08:02:45 +08:00
$(this).attr('src', oAttachment.linkPreview());
}
2016-06-30 08:02:45 +08:00
}
});
2016-06-30 08:02:45 +08:00
$('[data-x-style-cid]', this.body).each(function() {
2016-06-30 08:02:45 +08:00
var
sStyle = '',
sName = '',
oAttachment = self.findAttachmentByCid($(this).attr('data-x-style-cid'));
2016-06-30 08:02:45 +08:00
if (oAttachment && oAttachment.linkPreview)
{
sName = $(this).attr('data-x-style-cid-name');
if ('' !== sName)
{
2016-06-30 08:02:45 +08:00
sStyle = Utils.trim($(this).attr('style'));
sStyle = '' === sStyle ? '' : (';' === sStyle.substr(-1) ? sStyle + ' ' : sStyle + '; ');
$(this).attr('style', sStyle + sName + ': url(\'' + oAttachment.linkPreview() + '\')');
}
}
2016-06-30 08:02:45 +08:00
});
2016-06-30 08:02:45 +08:00
if (bLazy)
{
2016-06-30 08:02:45 +08:00
(function($oImg, oContainer) {
_.delay(function() {
$oImg.addClass('lazy-inited').lazyload({
'threshold': 400,
'effect': 'fadeIn',
'skip_invisible': false,
'container': oContainer
});
}, 300);
}($('img.lazy', self.body), $('.RL-MailMessageView .messageView .messageItem .content')[0]));
2014-08-25 15:10:51 +08:00
}
2014-04-02 02:03:37 +08:00
2016-06-30 08:02:45 +08:00
Utils.windowResize(500);
}
};
2016-06-30 08:02:45 +08:00
MessageModel.prototype.storeDataInDom = function()
{
if (this.body)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
this.body.data('rl-is-html', !!this.isHtml());
this.body.data('rl-has-images', !!this.hasImages());
}
};
2016-06-30 08:02:45 +08:00
MessageModel.prototype.fetchDataFromDom = function()
{
if (this.body)
2014-08-20 23:03:12 +08:00
{
2016-06-30 08:02:45 +08:00
this.isHtml(!!this.body.data('rl-is-html'));
this.hasImages(!!this.body.data('rl-has-images'));
}
};
2016-06-30 08:02:45 +08:00
MessageModel.prototype.replacePlaneTextBody = function(sPlain)
{
if (this.body)
{
this.body.html(sPlain).addClass('b-text-part plain');
}
};
/**
* @returns {string}
*/
MessageModel.prototype.flagHash = function()
{
return [this.deleted(), this.deletedMark(), this.unseen(), this.flagged(), this.answered(), this.forwarded(),
this.isReadReceipt()].join(',');
};
module.exports = MessageModel;