Cleanup JavaScript MessageModel by moving dynamic code to MailMessageView and ComposePopupView

This commit is contained in:
the-djmaze 2022-12-22 09:56:06 +01:00
parent ae1de36ee0
commit bad33e6dc4
4 changed files with 54 additions and 112 deletions

View file

@ -25,7 +25,7 @@ import { LanguageStore } from 'Stores/Language';
import Remote from 'Remote/User/Fetch';
const
hcont = Element.fromHTML('<div area="hidden" style="position:absolute;left:-5000px"></div>'),
hcont = Element.fromHTML('<div area="hidden" style="position:absolute;left:-200vw;width:max(50vw,400px)"></div>'),
getRealHeight = el => {
hcont.innerHTML = el.outerHTML;
const result = hcont.clientHeight;
@ -133,22 +133,12 @@ export class MessageModel extends AbstractModel {
attachmentIconClass: () =>
this.encrypted() ? 'icon-lock' : FileInfo.getAttachmentsIconClass(this.attachments()),
threadsLen: () => this.threads().length,
listAttachments: () => this.attachments()
.filter(item => SettingsUserStore.listInlineAttachments() || !item.isLinked()),
hasAttachments: () => this.listAttachments().length,
isUnseen: () => !this.flags().includes('\\seen'),
isFlagged: () => this.flags().includes('\\flagged'),
isReadReceipt: () => this.flags().includes('$mdnsent'),
// isJunk: () => this.flags().includes('$junk') && !this.flags().includes('$nonjunk'),
// isPhishing: () => this.flags().includes('$phishing'),
tagsToHTML: () => this.flags().map(value =>
isAllowedKeyword(value)
? '<span class="focused msgflag-'+value+'">' + i18n('MESSAGE_TAGS/'+value,0,value) + '</span>'
: ''
).join(' '),
tagOptions: () => {
const tagOptions = [];
FolderUserStore.currentFolder().permanentFlags.forEach(value => {
@ -209,61 +199,6 @@ export class MessageModel extends AbstractModel {
}
}
/**
* @returns {boolean}
*/
hasUnsubsribeLinks() {
return this.unsubsribeLinks().length;
}
/**
* @returns {string}
*/
getFirstUnsubsribeLink() {
return this.unsubsribeLinks()[0] || '';
}
/**
* @param {boolean} friendlyView
* @param {boolean=} wrapWithLink
* @returns {string}
*/
fromToLine(friendlyView, wrapWithLink) {
return this.from.toString(friendlyView, wrapWithLink);
}
/**
* @param {boolean} friendlyView
* @param {boolean=} wrapWithLink
* @returns {string}
*/
toToLine(friendlyView, wrapWithLink) {
return this.to.toString(friendlyView, wrapWithLink);
}
/**
* @param {boolean} friendlyView
* @param {boolean=} wrapWithLink
* @returns {string}
*/
ccToLine(friendlyView, wrapWithLink) {
return this.cc.toString(friendlyView, wrapWithLink);
}
/**
* @returns {string}
*/
bccToLine() {
return this.bcc.toString();
}
/**
* @returns {string}
*/
replyToToLine() {
return this.replyTo.toString();
}
/**
* @return string
*/
@ -288,7 +223,7 @@ export class MessageModel extends AbstractModel {
/**
* @returns {string}
*/
viewLink() {
viewRaw() {
return serverRequestRaw('ViewAsPlain', this.requestHash);
}
@ -390,7 +325,7 @@ export class MessageModel extends AbstractModel {
viewPopupMessage(print) {
const timeStampInUTC = this.dateTimeStampInUTC() || 0,
ccLine = this.ccToLine(),
ccLine = this.cc.toString(),
m = 0 < timeStampInUTC ? new Date(timeStampInUTC * 1000) : null,
win = open(''),
sdoc = win.document;
@ -402,7 +337,7 @@ export class MessageModel extends AbstractModel {
sdoc.write(PreviewHTML
.replace('<title>', '<title>'+subject)
// eslint-disable-next-line max-len
.replace('<body>', `<body style="background-color:${prop('background-color')};color:${prop('color')}"><header><h1>${subject}</h1><time>${encodeHtml(m ? m.format('LLL',0,LanguageStore.hourCycle()) : '')}</time><div>${encodeHtml(this.fromToLine())}</div><div>${encodeHtml(i18n('GLOBAL/TO'))}: ${encodeHtml(this.toToLine())}</div>${cc}</header><${mode}>${this.bodyAsHTML()}</${mode}>`)
.replace('<body>', `<body style="background-color:${prop('background-color')};color:${prop('color')}"><header><h1>${subject}</h1><time>${encodeHtml(m ? m.format('LLL',0,LanguageStore.hourCycle()) : '')}</time><div>${encodeHtml(this.from)}</div><div>${encodeHtml(i18n('GLOBAL/TO'))}: ${encodeHtml(this.to)}</div>${cc}</header><${mode}>${this.bodyAsHTML()}</${mode}>`)
);
sdoc.close();
@ -435,7 +370,6 @@ export class MessageModel extends AbstractModel {
*/
static fromMessageListItem(message) {
let self = new MessageModel();
if (message) {
// Clone message values
forEachObjectEntry(message, (key, value) => {
@ -445,10 +379,8 @@ export class MessageModel extends AbstractModel {
self[key] = value;
}
});
self.computeSenderEmail();
}
self.computeSenderEmail();
return self;
}

View file

@ -900,7 +900,7 @@ export class ComposePopupView extends AbstractViewPopup {
switch (msgComposeType) {
case ComposeType.Reply:
case ComposeType.ReplyAll:
sFrom = message.fromToLine(false, true);
sFrom = message.from.toString(false, true);
sText = '<br><br><p>' + i18n('COMPOSE/REPLY_MESSAGE_TITLE', { DATETIME: sDate, EMAIL: sFrom })
+ ':</p><blockquote>'
+ sText.trim()
@ -908,9 +908,9 @@ export class ComposePopupView extends AbstractViewPopup {
break;
case ComposeType.Forward:
sFrom = message.fromToLine(false, true);
sTo = message.toToLine(false, true);
sCc = message.ccToLine(false, true);
sFrom = message.from.toString(false, true);
sTo = message.to.toString(false, true);
sCc = message.cc.toString(false, true);
sText = '<br><br><p>' + i18n('COMPOSE/FORWARD_MESSAGE_TOP_TITLE') + '</p><div>'
+ i18n('GLOBAL/FROM') + ': ' + sFrom
+ '<br>'

View file

@ -130,7 +130,7 @@ export class MailMessageView extends AbstractViewRight {
this.isDraftFolder = MessagelistUserStore.isDraftFolder;
this.isSpamFolder = MessagelistUserStore.isSpamFolder;
this.message = MessageUserStore.message;
this.message = currentMessage;
this.messageLoadingThrottle = MessageUserStore.loading;
this.messagesBodiesDom = MessageUserStore.bodiesDom;
this.messageError = MessageUserStore.error;
@ -157,6 +157,21 @@ export class MailMessageView extends AbstractViewRight {
messageVisibility: () => !MessageUserStore.loading() && !!currentMessage(),
tagsToHTML: () => currentMessage()?.flags().map(value =>
isAllowedKeyword(value)
? '<span class="focused msgflag-'+value+'">' + i18n('MESSAGE_TAGS/'+value,0,value) + '</span>'
: ''
).join(' '),
askReadReceipt: () =>
(MessagelistUserStore.isDraftFolder() || MessagelistUserStore.isSentFolder())
&& currentMessage()?.readReceipt()
&& currentMessage()?.flags().includes('$mdnsent'),
listAttachments: () => currentMessage()?.attachments()
.filter(item => SettingsUserStore.listInlineAttachments() || !item.isLinked()),
hasAttachments: () => this.listAttachments().length,
canBeRepliedOrForwarded: () => !MessagelistUserStore.isDraftFolder() && this.messageVisibility(),
viewFromDkimVisibility: () => 'none' !== this.viewFromDkimData()[0],
@ -181,6 +196,8 @@ export class MailMessageView extends AbstractViewRight {
return '';
},
firstUnsubsribeLink: () => currentMessage()?.unsubsribeLinks()[0] || '',
pgpSupported: () => currentMessage() && PgpUserStore.isSupported(),
messageListOrViewLoading:
@ -195,11 +212,11 @@ export class MailMessageView extends AbstractViewRight {
}
this.viewHash = message.hash;
// TODO: make first param a user setting #683
this.viewFromShort(message.fromToLine(false, true));
this.viewFromShort(message.from.toString(false, true));
let dkim = 1 === arrayLength(message.from) && message.dkim
&& message.dkim.find(dkim => message.from[0].email.includes(dkim[1]));
this.viewFromDkimData(dkim ? [dkim[0], dkim[2]] : ['none', '']);
this.viewToShort(message.toToLine(true, true));
this.viewToShort(message.to.toString(true, true));
} else {
MessagelistUserStore.selectedMessage(null);
@ -453,13 +470,6 @@ export class MailMessageView extends AbstractViewRight {
});
}
/**
* @returns {boolean}
*/
isDraftOrSentFolder() {
return MessagelistUserStore.isDraftFolder() || MessagelistUserStore.isSentFolder();
}
scrollMessageToTop() {
oMessageScrollerDom().scrollTop = (50 < oMessageScrollerDom().scrollTop) ? 50 : 0;
}