2017-09-28 01:58:15 +08:00
|
|
|
import addressparser from 'emailjs-addressparser';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { trim, encodeHtml, isNonEmptyArray } from 'Common/Utils';
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class EmailModel {
|
2016-09-10 06:38:16 +08:00
|
|
|
email = '';
|
|
|
|
name = '';
|
|
|
|
dkimStatus = '';
|
|
|
|
dkimValue = '';
|
|
|
|
|
2016-07-07 05:03:30 +08:00
|
|
|
/**
|
|
|
|
* @param {string=} email = ''
|
|
|
|
* @param {string=} name = ''
|
|
|
|
* @param {string=} dkimStatus = 'none'
|
|
|
|
* @param {string=} dkimValue = ''
|
|
|
|
*/
|
2019-07-05 03:19:24 +08:00
|
|
|
constructor(email = '', name = '', dkimStatus = 'none', dkimValue = '') {
|
2016-07-07 05:03:30 +08:00
|
|
|
this.email = email;
|
|
|
|
this.name = name;
|
|
|
|
this.dkimStatus = dkimStatus;
|
|
|
|
this.dkimValue = dkimValue;
|
|
|
|
|
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param {AjaxJsonEmail} json
|
|
|
|
* @returns {?EmailModel}
|
|
|
|
*/
|
|
|
|
static newInstanceFromJson(json) {
|
|
|
|
const email = new EmailModel();
|
|
|
|
return email.initByJson(json) ? email : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
this.email = '';
|
|
|
|
this.name = '';
|
|
|
|
|
|
|
|
this.dkimStatus = 'none';
|
|
|
|
this.dkimValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
validate() {
|
2020-07-28 23:20:14 +08:00
|
|
|
return this.name || this.email;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} withoutName = false
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
hash(withoutName = false) {
|
|
|
|
return '#' + (withoutName ? '' : this.name) + '#' + this.email + '#';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clearDuplicateName() {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.name === this.email) {
|
2016-07-07 05:03:30 +08:00
|
|
|
this.name = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} query
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
search(query) {
|
2020-07-21 03:39:00 +08:00
|
|
|
return (this.name + ' ' + this.email).toLowerCase().includes(query.toLowerCase());
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {AjaxJsonEmail} oJsonEmail
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
initByJson(json) {
|
|
|
|
let result = false;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (json && 'Object/Email' === json['@Object']) {
|
2016-07-07 05:03:30 +08:00
|
|
|
this.name = trim(json.Name);
|
|
|
|
this.email = trim(json.Email);
|
|
|
|
this.dkimStatus = trim(json.DkimStatus || '');
|
|
|
|
this.dkimValue = trim(json.DkimValue || '');
|
|
|
|
|
2020-07-28 23:20:14 +08:00
|
|
|
result = !!this.email;
|
2016-07-07 05:03:30 +08:00
|
|
|
this.clearDuplicateName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {boolean} friendlyView
|
|
|
|
* @param {boolean=} wrapWithLink = false
|
|
|
|
* @param {boolean=} useEncodeHtml = false
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
toLine(friendlyView, wrapWithLink = false, useEncodeHtml = false) {
|
|
|
|
let result = '';
|
2020-07-28 23:20:14 +08:00
|
|
|
if (this.email) {
|
|
|
|
if (friendlyView && this.name) {
|
2019-07-05 03:19:24 +08:00
|
|
|
result = wrapWithLink
|
|
|
|
? '<a href="mailto:' +
|
|
|
|
encodeHtml(this.email) +
|
|
|
|
'?to=' +
|
|
|
|
encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
|
|
|
'" target="_blank" tabindex="-1">' +
|
|
|
|
encodeHtml(this.name) +
|
|
|
|
'</a>'
|
|
|
|
: useEncodeHtml
|
|
|
|
? encodeHtml(this.name)
|
|
|
|
: this.name;
|
2017-10-07 02:52:00 +08:00
|
|
|
// result = wrapWithLink ? '<a href="mailto:' + encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
|
|
|
// '" target="_blank" tabindex="-1">' + encodeHtml(this.name) + '</a>' : (useEncodeHtml ? encodeHtml(this.name) : this.name);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-07-07 05:03:30 +08:00
|
|
|
result = this.email;
|
2020-07-28 23:20:14 +08:00
|
|
|
if (this.name) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (wrapWithLink) {
|
|
|
|
result =
|
|
|
|
encodeHtml('"' + this.name + '" <') +
|
|
|
|
'<a href="mailto:' +
|
|
|
|
encodeHtml(this.email) +
|
|
|
|
'?to=' +
|
|
|
|
encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
2016-07-07 05:03:30 +08:00
|
|
|
'" target="_blank" tabindex="-1">' +
|
|
|
|
encodeHtml(result) +
|
|
|
|
'</a>' +
|
|
|
|
encodeHtml('>');
|
2017-10-07 02:52:00 +08:00
|
|
|
// result = encodeHtml('"' + this.name + '" <') + '<a href="mailto:' +
|
|
|
|
// encodeHtml('"' + this.name + '" <' + this.email + '>') +
|
|
|
|
// '" target="_blank" tabindex="-1">' +
|
|
|
|
// encodeHtml(result) +
|
|
|
|
// '</a>' +
|
|
|
|
// encodeHtml('>');
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-07-07 05:03:30 +08:00
|
|
|
result = '"' + this.name + '" <' + result + '>';
|
2019-07-05 03:19:24 +08:00
|
|
|
if (useEncodeHtml) {
|
2016-07-07 05:03:30 +08:00
|
|
|
result = encodeHtml(result);
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if (wrapWithLink) {
|
|
|
|
result =
|
|
|
|
'<a href="mailto:' +
|
|
|
|
encodeHtml(this.email) +
|
|
|
|
'" target="_blank" tabindex="-1">' +
|
|
|
|
encodeHtml(this.email) +
|
|
|
|
'</a>';
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
static splitEmailLine(line) {
|
2018-02-28 03:10:26 +08:00
|
|
|
const parsedResult = addressparser(line);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (isNonEmptyArray(parsedResult)) {
|
2017-09-28 01:58:15 +08:00
|
|
|
const result = [];
|
|
|
|
let exists = false;
|
|
|
|
parsedResult.forEach((item) => {
|
2019-07-05 03:19:24 +08:00
|
|
|
const address = item.address
|
|
|
|
? new EmailModel(item.address.replace(/^[<]+(.*)[>]+$/g, '$1'), item.name || '')
|
|
|
|
: null;
|
2017-09-28 01:58:15 +08:00
|
|
|
|
|
|
|
if (address && address.email) {
|
|
|
|
exists = true;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
result.push(address ? address.toLine(false) : item.name);
|
|
|
|
});
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return exists ? result : null;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return null;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
static parseEmailLine(line) {
|
2018-02-28 03:10:26 +08:00
|
|
|
const parsedResult = addressparser(line);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (isNonEmptyArray(parsedResult)) {
|
2020-07-23 22:06:16 +08:00
|
|
|
return parsedResult.map(item =>
|
|
|
|
item.address ? new EmailModel(item.address.replace(/^[<]+(.*)[>]+$/g, '$1'), item.name || '') : null
|
|
|
|
).filter(value => !!value);
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return [];
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
/**
|
|
|
|
* @param {string} emailAddress
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
parse(emailAddress) {
|
|
|
|
emailAddress = trim(emailAddress);
|
2020-07-28 23:20:14 +08:00
|
|
|
if (!emailAddress) {
|
2017-09-28 01:58:15 +08:00
|
|
|
return false;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 03:10:26 +08:00
|
|
|
const result = addressparser(emailAddress);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (isNonEmptyArray(result) && result[0]) {
|
2017-09-28 01:58:15 +08:00
|
|
|
this.name = result[0].name || '';
|
|
|
|
this.email = result[0].address || '';
|
|
|
|
this.clearDuplicateName();
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2017-09-28 01:58:15 +08:00
|
|
|
return false;
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { EmailModel, EmailModel as default };
|