Message from, to, cc, bcc, replyTo and deliveredTo to the new EmailCollectionModel

This commit is contained in:
djmaze 2020-09-15 09:43:53 +02:00
parent b904eca98e
commit 062f8d078e
4 changed files with 100 additions and 128 deletions

View file

@ -1,67 +0,0 @@
import { EmailModel } from 'Model/Email';
/**
* @param {Array.<EmailModel>} emails
* @param {boolean=} friendlyView = false
* @param {boolean=} wrapWithLink = false
* @returns {string}
*/
export function emailArrayToString(emails, friendlyView = false, wrapWithLink = false) {
const result = [];
if (Array.isNotEmpty(emails)) {
emails.forEach(email => result.push(email.toLine(friendlyView, wrapWithLink)));
}
return result.join(', ');
}
/**
* @param {Array.<EmailModel>} emails
* @returns {string}
*/
export function emailArrayToStringClear(emails) {
const result = [];
if (Array.isNotEmpty(emails)) {
emails.forEach(email => {
if (email && email.email && email.name) {
result.push(email.email);
}
});
}
return result.join(', ');
}
/**
* @param {?Array} json
* @returns {Array.<EmailModel>}
*/
export function emailArrayFromJson(json) {
const result = [];
if (Array.isNotEmpty(json)) {
json.forEach(email => {
email = EmailModel.newInstanceFromJson(email);
if (email) {
result.push(email);
}
});
}
return result;
}
/**
* @param {Array.<EmailModel>} inputEmails
* @param {Object} unic
* @param {Array} localEmails
*/
export function replyHelper(inputEmails, unic, localEmails) {
if (inputEmails) {
inputEmails.forEach(email => {
if (undefined === unic[email.email]) {
unic[email.email] = true;
localEmails.push(email);
}
});
}
}

View file

@ -0,0 +1,51 @@
import { EmailModel } from 'Model/Email';
'use strict';
class EmailCollectionModel extends Array
{
constructor() {
super();
}
/**
* @param {?Array} json
* @returns {Array.<EmailModel>}
*/
static reviveFromJson(items) {
let result = new EmailCollectionModel;
if (Array.isArray(items)) {
items.forEach(email => {
email = EmailModel.newInstanceFromJson(email);
email && result.push(email);
});
}
return result;
}
/**
* @param {boolean=} friendlyView = false
* @param {boolean=} wrapWithLink = false
* @returns {string}
*/
toString(friendlyView = false, wrapWithLink = false) {
const result = [];
this.forEach(email => result.push(email.toLine(friendlyView, wrapWithLink)));
return result.join(', ');
}
/**
* @returns {string}
*/
toStringClear() {
const result = [];
this.forEach(email => {
if (email && email.email && email.name) {
result.push(email.email);
}
});
return result.join(', ');
}
}
export { EmailCollectionModel, EmailCollectionModel as default };

View file

@ -15,21 +15,27 @@ import { messageViewLink, messageDownloadLink } from 'Common/Links';
import FolderStore from 'Stores/User/Folder'; import FolderStore from 'Stores/User/Folder';
import PgpStore from 'Stores/User/Pgp'; import PgpStore from 'Stores/User/Pgp';
import { emailArrayFromJson, emailArrayToStringClear, emailArrayToString, replyHelper } from 'Helper/Message';
import { AttachmentModel, staticCombinedIconClass } from 'Model/Attachment'; import { AttachmentModel, staticCombinedIconClass } from 'Model/Attachment';
import { EmailCollectionModel } from 'Model/EmailCollection';
import { AbstractModel } from 'Knoin/AbstractModel'; import { AbstractModel } from 'Knoin/AbstractModel';
const isArray = Array.isArray; const isArray = Array.isArray,
replyHelper = (emails, unic, localEmails) => {
emails.forEach(email => {
if (undefined === unic[email.email]) {
unic[email.email] = true;
localEmails.push(email);
}
});
};
class MessageModel extends AbstractModel { class MessageModel extends AbstractModel {
constructor() { constructor() {
super('MessageModel'); super('MessageModel');
this.folderFullNameRaw = ''; this._reset();
this.uid = '';
this.hash = '';
this.requestHash = '';
this.subject = ko.observable(''); this.subject = ko.observable('');
this.subjectPrefix = ko.observable(''); this.subjectPrefix = ko.observable('');
this.subjectSuffix = ko.observable(''); this.subjectSuffix = ko.observable('');
@ -37,8 +43,6 @@ class MessageModel extends AbstractModel {
this.dateTimeStampInUTC = ko.observable(0); this.dateTimeStampInUTC = ko.observable(0);
this.priority = ko.observable(MessagePriority.Normal); this.priority = ko.observable(MessagePriority.Normal);
this.proxy = false;
this.fromEmailString = ko.observable(''); this.fromEmailString = ko.observable('');
this.fromClearEmailString = ko.observable(''); this.fromClearEmailString = ko.observable('');
this.toEmailsString = ko.observable(''); this.toEmailsString = ko.observable('');
@ -47,16 +51,6 @@ class MessageModel extends AbstractModel {
this.senderEmailsString = ko.observable(''); this.senderEmailsString = ko.observable('');
this.senderClearEmailsString = ko.observable(''); this.senderClearEmailsString = ko.observable('');
this.emails = [];
this.from = [];
this.to = [];
this.cc = [];
this.bcc = [];
this.replyTo = [];
this.deliveredTo = [];
this.unsubsribeLinks = [];
this.newForAnimation = ko.observable(false); this.newForAnimation = ko.observable(false);
this.deleted = ko.observable(false); this.deleted = ko.observable(false);
@ -77,8 +71,6 @@ class MessageModel extends AbstractModel {
staticCombinedIconClass(this.hasAttachments() ? this.attachmentsSpecData() : []) staticCombinedIconClass(this.hasAttachments() ? this.attachmentsSpecData() : [])
); );
this.body = null;
this.isHtml = ko.observable(false); this.isHtml = ko.observable(false);
this.hasImages = ko.observable(false); this.hasImages = ko.observable(false);
this.attachments = ko.observableArray([]); this.attachments = ko.observableArray([]);
@ -91,11 +83,6 @@ class MessageModel extends AbstractModel {
this.priority = ko.observable(MessagePriority.Normal); this.priority = ko.observable(MessagePriority.Normal);
this.readReceipt = ko.observable(''); this.readReceipt = ko.observable('');
this.aDraftInfo = [];
this.sMessageId = '';
this.sInReplyTo = '';
this.sReferences = '';
this.hasUnseenSubMessage = ko.observable(false); this.hasUnseenSubMessage = ko.observable(false);
this.hasFlaggedSubMessage = ko.observable(false); this.hasFlaggedSubMessage = ko.observable(false);
@ -117,11 +104,29 @@ class MessageModel extends AbstractModel {
return oMessageModel.initByJson(json) ? oMessageModel : null; return oMessageModel.initByJson(json) ? oMessageModel : null;
} }
clear() { _reset() {
this.folderFullNameRaw = ''; this.folderFullNameRaw = '';
this.uid = ''; this.uid = '';
this.hash = ''; this.hash = '';
this.requestHash = ''; this.requestHash = '';
this.proxy = false;
this.emails = [];
this.from = new EmailCollectionModel;
this.to = new EmailCollectionModel;
this.cc = new EmailCollectionModel;
this.bcc = new EmailCollectionModel;
this.replyTo = new EmailCollectionModel;
this.deliveredTo = new EmailCollectionModel;
this.unsubsribeLinks = [];
this.body = null;
this.aDraftInfo = [];
this.sMessageId = '';
this.sInReplyTo = '';
this.sReferences = '';
}
clear() {
this._reset();
this.subject(''); this.subject('');
this.subjectPrefix(''); this.subjectPrefix('');
this.subjectSuffix(''); this.subjectSuffix('');
@ -129,8 +134,6 @@ class MessageModel extends AbstractModel {
this.dateTimeStampInUTC(0); this.dateTimeStampInUTC(0);
this.priority(MessagePriority.Normal); this.priority(MessagePriority.Normal);
this.proxy = false;
this.fromEmailString(''); this.fromEmailString('');
this.fromClearEmailString(''); this.fromClearEmailString('');
this.toEmailsString(''); this.toEmailsString('');
@ -138,16 +141,6 @@ class MessageModel extends AbstractModel {
this.senderEmailsString(''); this.senderEmailsString('');
this.senderClearEmailsString(''); this.senderClearEmailsString('');
this.emails = [];
this.from = [];
this.to = [];
this.cc = [];
this.bcc = [];
this.replyTo = [];
this.deliveredTo = [];
this.unsubsribeLinks = [];
this.newForAnimation(false); this.newForAnimation(false);
this.deleted(false); this.deleted(false);
@ -163,7 +156,6 @@ class MessageModel extends AbstractModel {
this.hasAttachments(false); this.hasAttachments(false);
this.attachmentsSpecData([]); this.attachmentsSpecData([]);
this.body = null;
this.isHtml(false); this.isHtml(false);
this.hasImages(false); this.hasImages(false);
this.attachments([]); this.attachments([]);
@ -175,10 +167,6 @@ class MessageModel extends AbstractModel {
this.priority(MessagePriority.Normal); this.priority(MessagePriority.Normal);
this.readReceipt(''); this.readReceipt('');
this.aDraftInfo = [];
this.sMessageId = '';
this.sInReplyTo = '';
this.sReferences = '';
this.threads([]); this.threads([]);
@ -250,12 +238,12 @@ class MessageModel extends AbstractModel {
this.size(pInt(json.Size)); this.size(pInt(json.Size));
this.from = emailArrayFromJson(json.From); this.from = EmailCollectionModel.reviveFromJson(json.From);
this.to = emailArrayFromJson(json.To); this.to = EmailCollectionModel.reviveFromJson(json.To);
this.cc = emailArrayFromJson(json.Cc); this.cc = EmailCollectionModel.reviveFromJson(json.Cc);
this.bcc = emailArrayFromJson(json.Bcc); this.bcc = EmailCollectionModel.reviveFromJson(json.Bcc);
this.replyTo = emailArrayFromJson(json.ReplyTo); this.replyTo = EmailCollectionModel.reviveFromJson(json.ReplyTo);
this.deliveredTo = emailArrayFromJson(json.DeliveredTo); this.deliveredTo = EmailCollectionModel.reviveFromJson(json.DeliveredTo);
this.unsubsribeLinks = Array.isNotEmpty(json.UnsubsribeLinks) ? json.UnsubsribeLinks : []; this.unsubsribeLinks = Array.isNotEmpty(json.UnsubsribeLinks) ? json.UnsubsribeLinks : [];
this.subject(json.Subject); this.subject(json.Subject);
@ -271,10 +259,10 @@ class MessageModel extends AbstractModel {
this.hasAttachments(!!json.HasAttachments); this.hasAttachments(!!json.HasAttachments);
this.attachmentsSpecData(isArray(json.AttachmentsSpecData) ? json.AttachmentsSpecData : []); this.attachmentsSpecData(isArray(json.AttachmentsSpecData) ? json.AttachmentsSpecData : []);
this.fromEmailString(emailArrayToString(this.from, true)); this.fromEmailString(this.from.toString(true));
this.fromClearEmailString(emailArrayToStringClear(this.from)); this.fromClearEmailString(this.from.toStringClear());
this.toEmailsString(emailArrayToString(this.to, true)); this.toEmailsString(this.to.toString(true));
this.toClearEmailsString(emailArrayToStringClear(this.to)); this.toClearEmailsString(this.to.toStringClear());
this.threads(isArray(json.Threads) ? json.Threads : []); this.threads(isArray(json.Threads) ? json.Threads : []);
@ -399,7 +387,7 @@ class MessageModel extends AbstractModel {
* @returns {string} * @returns {string}
*/ */
fromToLine(friendlyView, wrapWithLink = false) { fromToLine(friendlyView, wrapWithLink = false) {
return emailArrayToString(this.from, friendlyView, wrapWithLink); return this.from.toString(friendlyView, wrapWithLink);
} }
/** /**
@ -420,7 +408,7 @@ class MessageModel extends AbstractModel {
* @returns {string} * @returns {string}
*/ */
toToLine(friendlyView, wrapWithLink = false) { toToLine(friendlyView, wrapWithLink = false) {
return emailArrayToString(this.to, friendlyView, wrapWithLink); return this.to.toString(friendlyView, wrapWithLink);
} }
/** /**
@ -429,7 +417,7 @@ class MessageModel extends AbstractModel {
* @returns {string} * @returns {string}
*/ */
ccToLine(friendlyView, wrapWithLink = false) { ccToLine(friendlyView, wrapWithLink = false) {
return emailArrayToString(this.cc, friendlyView, wrapWithLink); return this.cc.toString(friendlyView, wrapWithLink);
} }
/** /**
@ -438,7 +426,7 @@ class MessageModel extends AbstractModel {
* @returns {string} * @returns {string}
*/ */
bccToLine(friendlyView, wrapWithLink = false) { bccToLine(friendlyView, wrapWithLink = false) {
return emailArrayToString(this.bcc, friendlyView, wrapWithLink); return this.bcc.toString(friendlyView, wrapWithLink);
} }
/** /**
@ -447,7 +435,7 @@ class MessageModel extends AbstractModel {
* @returns {string} * @returns {string}
*/ */
replyToToLine(friendlyView, wrapWithLink = false) { replyToToLine(friendlyView, wrapWithLink = false) {
return emailArrayToString(this.replyTo, friendlyView, wrapWithLink); return this.replyTo.toString(friendlyView, wrapWithLink);
} }
/** /**

View file

@ -27,7 +27,7 @@ import { MESSAGE_BODY_CACHE_LIMIT } from 'Common/Consts';
import { mailBox, notificationMailIcon } from 'Common/Links'; import { mailBox, notificationMailIcon } from 'Common/Links';
import { i18n, getNotification } from 'Common/Translator'; import { i18n, getNotification } from 'Common/Translator';
import * as MessageHelper from 'Helper/Message'; import { EmailCollectionModel } from 'Model/EmailCollection';
import { MessageModel } from 'Model/Message'; import { MessageModel } from 'Model/Message';
import { setHash } from 'Knoin/Knoin'; import { setHash } from 'Knoin/Knoin';
@ -259,7 +259,7 @@ class MessageUserStore {
newMessages.forEach(item => { newMessages.forEach(item => {
NotificationStore.displayDesktopNotification( NotificationStore.displayDesktopNotification(
notificationMailIcon(), notificationMailIcon(),
MessageHelper.emailArrayToString(MessageHelper.emailArrayFromJson(item.From), false), EmailCollectionModel.reviveFromJson(item.From).toString(),
item.Subject, item.Subject,
{ 'Folder': item.Folder, 'Uid': item.Uid } { 'Folder': item.Folder, 'Uid': item.Uid }
); );