snappymail/dev/View/User/MailBox/MessageView.js

618 lines
18 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
import { UNUSED_OPTION_VALUE } from 'Common/Consts';
import {
2019-07-05 03:19:24 +08:00
Capa,
Scope
2021-01-25 05:58:06 +08:00
} from 'Common/Enums';
import {
2019-07-05 03:19:24 +08:00
ComposeType,
ClientSideKeyName,
FolderType,
MessageSetAction
2021-01-25 05:58:06 +08:00
} from 'Common/EnumsUser';
2021-09-01 17:01:13 +08:00
import { $htmlCL, leftPanelDisabled, keyScopeReal, moveAction, Settings, getFullscreenElement, exitFullscreen } from 'Common/Globals';
2021-07-22 03:34:17 +08:00
import { arrayLength, inFocus } from 'Common/Utils';
2021-11-16 20:15:02 +08:00
import { mailToHelper, showMessageComposer, initFullscreen } from 'Common/UtilsUser';
import { SMAudio } from 'Common/Audio';
2019-07-05 03:19:24 +08:00
import { i18n } from 'Common/Translator';
import { attachmentDownload } from 'Common/Links';
import { MessageFlagsCache } from 'Common/Cache';
import { AppUserStore } from 'Stores/User/App';
import { SettingsUserStore } from 'Stores/User/Settings';
import { AccountUserStore } from 'Stores/User/Account';
import { FolderUserStore } from 'Stores/User/Folder';
import { MessageUserStore } from 'Stores/User/Message';
import { ThemeStore } from 'Stores/Theme';
import * as Local from 'Storage/Client';
import Remote from 'Remote/User/Fetch';
import { decorateKoCommands, createCommand } from 'Knoin/Knoin';
import { AbstractViewRight } from 'Knoin/AbstractViews';
export class MailMessageView extends AbstractViewRight {
constructor() {
super('MailMessageView');
2014-08-20 23:03:12 +08:00
2021-03-13 00:08:58 +08:00
const
createCommandReplyHelper = type =>
createCommand(() => {
this.lastReplyAction(type);
this.replyOrforward(type);
}, this.canBeRepliedOrForwarded),
createCommandActionHelper = (folderType, useFolder) =>
createCommand(() => {
2021-08-13 02:17:37 +08:00
const message = MessageUserStore.message();
if (message) {
2021-08-13 02:17:37 +08:00
MessageUserStore.message(null);
2021-03-13 00:08:58 +08:00
rl.app.deleteMessagesFromFolder(folderType, message.folder, [message.uid], useFolder);
}
}, this.messageVisibility);
2014-08-20 23:03:12 +08:00
this.oMessageScrollerDom = null;
2014-08-20 23:03:12 +08:00
this.addObservables({
showAttachmnetControls: false,
downloadAsZipLoading: false,
lastReplyAction_: '',
showFullInfo: '1' === Local.get(ClientSideKeyName.MessageHeaderFullInfo),
moreDropdownTrigger: false
});
2014-08-20 23:03:12 +08:00
2017-03-02 02:38:18 +08:00
this.moveAction = moveAction;
this.allowMessageActions = Settings.capa(Capa.MessageActions);
const attachmentsActions = Settings.app('attachmentsActions');
2021-07-22 03:34:17 +08:00
this.attachmentsActions = ko.observableArray(arrayLength(attachmentsActions) ? attachmentsActions : []);
2016-05-01 09:07:10 +08:00
this.message = MessageUserStore.message;
this.hasCheckedMessages = MessageUserStore.hasCheckedMessages;
2021-03-12 23:54:37 +08:00
this.messageLoadingThrottle = MessageUserStore.messageLoading;
this.messagesBodiesDom = MessageUserStore.messagesBodiesDom;
this.messageActiveDom = MessageUserStore.messageActiveDom;
this.messageError = MessageUserStore.messageError;
2015-04-25 06:15:11 +08:00
this.fullScreenMode = MessageUserStore.messageFullScreenMode;
2014-08-22 23:08:56 +08:00
2019-07-05 03:19:24 +08:00
this.messageListOfThreadsLoading = ko.observable(false).extend({ rateLimit: 1 });
this.highlightUnselectedAttachments = ko.observable(false).extend({ falseTimeout: 2000 });
2014-08-20 23:03:12 +08:00
2020-10-02 18:40:33 +08:00
this.showAttachmnetControlsState = v => Local.set(ClientSideKeyName.MessageAttachmentControls, !!v);
2019-07-05 03:19:24 +08:00
this.downloadAsZipError = ko.observable(false).extend({ falseTimeout: 7000 });
2015-04-25 06:15:11 +08:00
2019-07-05 03:19:24 +08:00
this.messageDomFocused = ko.observable(false).extend({ rateLimit: 0 });
// commands
this.replyCommand = createCommandReplyHelper(ComposeType.Reply);
this.replyAllCommand = createCommandReplyHelper(ComposeType.ReplyAll);
this.forwardCommand = createCommandReplyHelper(ComposeType.Forward);
this.forwardAsAttachmentCommand = createCommandReplyHelper(ComposeType.ForwardAsAttachment);
this.editAsNewCommand = createCommandReplyHelper(ComposeType.EditAsNew);
this.deleteCommand = createCommandActionHelper(FolderType.Trash, true);
this.deleteWithoutMoveCommand = createCommandActionHelper(FolderType.Trash, false);
this.archiveCommand = createCommandActionHelper(FolderType.Archive, true);
this.spamCommand = createCommandActionHelper(FolderType.Spam, true);
this.notSpamCommand = createCommandActionHelper(FolderType.NotSpam, true);
// viewer
this.viewFolder = '';
this.viewUid = '';
this.viewHash = '';
this.addObservables({
viewSubject: '',
viewFromShort: '',
viewFromDkimData: ['none', ''],
viewToShort: '',
viewFrom: '',
viewTo: '',
viewCc: '',
viewBcc: '',
viewReplyTo: '',
viewTimeStamp: 0,
viewSize: '',
viewSpamScore: 0,
viewSpamStatus: '',
viewLineAsCss: '',
viewViewLink: '',
viewUnsubscribeLink: '',
viewDownloadLink: '',
viewIsImportant: false,
2021-09-02 19:38:59 +08:00
viewIsFlagged: false,
hasVirus: null
});
2014-08-20 23:03:12 +08:00
this.addComputables({
allowAttachmnetControls: () => this.attachmentsActions.length && Settings.capa(Capa.AttachmentsActions),
downloadAsZipAllowed: () => this.attachmentsActions.includes('zip') && this.allowAttachmnetControls(),
lastReplyAction: {
read: this.lastReplyAction_,
write: value => this.lastReplyAction_(
[ComposeType.Reply, ComposeType.ReplyAll, ComposeType.Forward].includes(value)
? ComposeType.Reply
: value
)
},
2021-08-13 02:17:37 +08:00
messageVisibility: () => !MessageUserStore.messageLoading() && !!MessageUserStore.message(),
canBeRepliedOrForwarded: () => !this.isDraftFolder() && this.messageVisibility(),
viewFromDkimVisibility: () => 'none' !== this.viewFromDkimData()[0],
viewFromDkimStatusIconClass:() => {
switch (this.viewFromDkimData()[0]) {
case 'none':
return '';
case 'pass':
return 'icon-ok iconcolor-green';
default:
return 'icon-warning-alt iconcolor-red';
}
},
viewFromDkimStatusTitle:() => {
const status = this.viewFromDkimData();
2021-07-22 03:34:17 +08:00
if (arrayLength(status) && status[0]) {
return status[1] || 'DKIM: ' + status[0];
}
return '';
},
messageListAndMessageViewLoading:
2021-03-12 23:54:37 +08:00
() => MessageUserStore.listCompleteLoading() || MessageUserStore.messageLoading()
});
this.addSubscribables({
2021-08-13 02:17:37 +08:00
showAttachmnetControls: v => MessageUserStore.message()
&& MessageUserStore.message().attachments.forEach(item => item && item.checked(!!v)),
lastReplyAction_: value => Local.set(ClientSideKeyName.LastReplyAction, value),
message: message => {
this.messageActiveDom(null);
if (message) {
this.showAttachmnetControls(false);
if (Local.get(ClientSideKeyName.MessageAttachmentControls)) {
setTimeout(() => {
this.showAttachmnetControls(true);
}, 50);
}
if (this.viewHash !== message.hash) {
this.scrollMessageToTop();
}
2021-09-02 19:38:59 +08:00
let spam = message.spamResult();
this.viewFolder = message.folder;
this.viewUid = message.uid;
this.viewHash = message.hash;
this.viewSubject(message.subject());
this.viewFromShort(message.fromToLine(true, true));
this.viewFromDkimData(message.fromDkimData());
this.viewToShort(message.toToLine(true, true));
this.viewFrom(message.fromToLine());
this.viewTo(message.toToLine());
this.viewCc(message.ccToLine());
this.viewBcc(message.bccToLine());
this.viewReplyTo(message.replyToToLine());
this.viewTimeStamp(message.dateTimeStampInUTC());
this.viewSize(message.friendlySize());
this.viewSpamScore(message.spamScore());
2021-09-02 19:38:59 +08:00
this.viewSpamStatus(spam ? i18n(message.isSpam() ? 'GLOBAL/SPAM' : 'GLOBAL/NOT_SPAM') + ': ' + spam : '');
this.viewLineAsCss(message.lineAsCss());
this.viewViewLink(message.viewLink());
this.viewUnsubscribeLink(message.getFirstUnsubsribeLink());
this.viewDownloadLink(message.downloadLink());
this.viewIsImportant(message.isImportant());
this.viewIsFlagged(message.isFlagged());
2021-09-02 19:38:59 +08:00
this.hasVirus(message.hasVirus());
} else {
MessageUserStore.selectorMessageSelected(null);
this.viewFolder = '';
this.viewUid = '';
this.viewHash = '';
this.scrollMessageToTop();
}
},
2016-06-30 08:02:45 +08:00
2021-03-03 18:16:02 +08:00
fullScreenMode: value => {
if (this.oContent) {
2021-09-01 17:01:13 +08:00
value ? this.oContent.requestFullscreen() : exitFullscreen();
2021-03-03 18:16:02 +08:00
} else {
$htmlCL.toggle('rl-message-fullscreen', value);
}
}
});
MessageUserStore.messageViewTrigger.subscribe(() => {
2021-08-13 02:17:37 +08:00
const message = MessageUserStore.message();
2021-03-12 23:54:37 +08:00
this.viewIsFlagged(message ? message.isFlagged() : false);
});
this.lastReplyAction(Local.get(ClientSideKeyName.LastReplyAction) || ComposeType.Reply);
2016-06-30 08:02:45 +08:00
addEventListener('mailbox.message-view.toggle-full-screen', () => this.toggleFullScreen());
decorateKoCommands(this, {
closeMessageCommand: 1,
messageEditCommand: self => self.messageVisibility(),
goUpCommand: self => !self.messageListAndMessageViewLoading(),
goDownCommand: self => !self.messageListAndMessageViewLoading()
});
2016-06-30 08:02:45 +08:00
}
closeMessageCommand() {
MessageUserStore.message(null);
}
messageEditCommand() {
this.editMessage();
}
goUpCommand() {
dispatchEvent(new CustomEvent('mailbox.message-list.selector.go-up',
2021-08-13 02:17:37 +08:00
{detail:SettingsUserStore.usePreviewPane() || !!MessageUserStore.message()} // bForceSelect
));
}
goDownCommand() {
dispatchEvent(new CustomEvent('mailbox.message-list.selector.go-down',
2021-08-13 02:17:37 +08:00
{detail:SettingsUserStore.usePreviewPane() || !!MessageUserStore.message()} // bForceSelect
));
}
toggleFullScreen() {
try {
getSelection().removeAllRanges();
} catch (e) {} // eslint-disable-line no-empty
2015-01-18 23:26:00 +08:00
this.fullScreenMode(!this.fullScreenMode());
}
/**
* @param {string} sType
* @returns {void}
*/
replyOrforward(sType) {
showMessageComposer([sType, MessageUserStore.message()]);
2016-06-30 08:02:45 +08:00
}
2016-05-01 09:07:10 +08:00
onBuild(dom) {
this.oMessageScrollerDom = dom.querySelector('.messageItem');
2021-08-13 02:17:37 +08:00
this.fullScreenMode.subscribe(value =>
value && MessageUserStore.message() && AppUserStore.focusedState(Scope.MessageView));
this.showFullInfo.subscribe(value => Local.set(ClientSideKeyName.MessageHeaderFullInfo, value ? '1' : '0'));
2021-11-16 20:15:02 +08:00
const el = dom.querySelector('.b-content');
this.oContent = initFullscreen(el, () => this.fullScreenMode(getFullscreenElement() === el));
2021-03-03 18:16:02 +08:00
const eqs = (ev, s) => ev.target.closestWithin(s, dom);
dom.addEventListener('click', event => {
ThemeStore.isMobile() && leftPanelDisabled(true);
let el = eqs(event, 'a');
if (el) {
2019-07-05 03:19:24 +08:00
return !(
0 === event.button &&
mailToHelper(el.href)
2019-07-05 03:19:24 +08:00
);
}
if (eqs(event, '.attachmentsPlace .attachmentIconParent')) {
event.stopPropagation();
}
el = eqs(event, '.attachmentsPlace .showPreplay');
if (el) {
event.stopPropagation();
2021-03-13 00:08:58 +08:00
const attachment = ko.dataFor(el);
if (attachment && SMAudio.supported) {
2019-07-05 03:19:24 +08:00
switch (true) {
case SMAudio.supportedMp3 && attachment.isMp3():
SMAudio.playMp3(attachment.linkDownload(), attachment.fileName);
break;
case SMAudio.supportedOgg && attachment.isOgg():
SMAudio.playOgg(attachment.linkDownload(), attachment.fileName);
break;
case SMAudio.supportedWav && attachment.isWav():
SMAudio.playWav(attachment.linkDownload(), attachment.fileName);
break;
// no default
}
}
}
el = eqs(event, '.attachmentsPlace .attachmentItem .attachmentNameParent');
if (el) {
const attachment = ko.dataFor(el);
2020-09-15 15:29:25 +08:00
attachment && attachment.download && rl.app.download(attachment.linkDownload());
}
if (eqs(event, '.messageItemHeader .subjectParent .flagParent')) {
2021-08-13 02:17:37 +08:00
const message = MessageUserStore.message();
2020-09-15 15:29:25 +08:00
message && rl.app.messageListAction(
2020-10-23 21:15:54 +08:00
message.folder,
message.isFlagged() ? MessageSetAction.UnsetFlag : MessageSetAction.SetFlag,
[message]
);
}
});
AppUserStore.focusedState.subscribe((value) => {
if (Scope.MessageView !== value) {
this.scrollMessageToTop();
this.scrollMessageToLeft();
}
});
keyScopeReal.subscribe(value => this.messageDomFocused(Scope.MessageView === value && !inFocus()));
2021-08-13 02:17:37 +08:00
// initShortcuts
2021-04-23 16:47:24 +08:00
// exit fullscreen, back
shortcuts.add('escape,backspace', '', Scope.MessageView, () => {
if (!this.viewModelDom.hidden && MessageUserStore.message()) {
2021-04-23 16:47:24 +08:00
const preview = SettingsUserStore.usePreviewPane();
if (this.fullScreenMode()) {
this.fullScreenMode(false);
if (preview) {
AppUserStore.focusedState(Scope.MessageList);
}
} else if (!preview) {
2021-08-13 02:17:37 +08:00
MessageUserStore.message(null);
2021-04-23 16:47:24 +08:00
} else {
AppUserStore.focusedState(Scope.MessageList);
}
2016-07-01 06:50:11 +08:00
2021-04-23 16:47:24 +08:00
return false;
}
});
// fullscreen
shortcuts.add('enter,open', '', Scope.MessageView, () => {
this.toggleFullScreen();
2016-06-30 08:02:45 +08:00
return false;
});
2016-07-01 06:50:11 +08:00
// reply
shortcuts.add('r,mailreply', '', [Scope.MessageList, Scope.MessageView], () => {
if (MessageUserStore.message()) {
this.replyCommand();
return false;
}
return true;
});
2016-06-30 08:02:45 +08:00
2021-08-13 02:17:37 +08:00
// replyAll
shortcuts.add('a', '', [Scope.MessageList, Scope.MessageView], () => {
if (MessageUserStore.message()) {
this.replyAllCommand();
return false;
}
});
shortcuts.add('mailreply', 'shift', [Scope.MessageList, Scope.MessageView], () => {
if (MessageUserStore.message()) {
this.replyAllCommand();
return false;
}
});
// forward
shortcuts.add('f,mailforward', '', [Scope.MessageList, Scope.MessageView], () => {
if (MessageUserStore.message()) {
this.forwardCommand();
return false;
}
});
// message information
shortcuts.add('i', 'meta', [Scope.MessageList, Scope.MessageView], () => {
if (MessageUserStore.message()) {
this.showFullInfo(!this.showFullInfo());
2014-11-20 07:25:39 +08:00
}
return false;
});
// toggle message blockquotes
shortcuts.add('b', '', [Scope.MessageList, Scope.MessageView], () => {
const message = MessageUserStore.message();
2020-08-27 21:45:47 +08:00
if (message && message.body) {
message.body.querySelectorAll('.rlBlockquoteSwitcher').forEach(node => node.click());
return false;
2014-11-20 07:25:39 +08:00
}
});
shortcuts.add('arrowup,arrowleft', 'meta', [Scope.MessageList, Scope.MessageView], () => {
this.goUpCommand();
2016-06-30 08:02:45 +08:00
return false;
});
2016-07-01 06:50:11 +08:00
shortcuts.add('arrowdown,arrowright', 'meta', [Scope.MessageList, Scope.MessageView], () => {
this.goDownCommand();
return false;
});
2015-04-14 02:45:09 +08:00
// print
shortcuts.add('p,printscreen', 'meta', [Scope.MessageView, Scope.MessageList], () => {
2021-08-13 02:17:37 +08:00
MessageUserStore.message() && MessageUserStore.message().printMessage();
return false;
});
// delete
shortcuts.add('delete', '', Scope.MessageView, () => {
this.deleteCommand();
return false;
});
shortcuts.add('delete', 'shift', Scope.MessageView, () => {
this.deleteWithoutMoveCommand();
return false;
});
2015-04-14 02:45:09 +08:00
// change focused state
shortcuts.add('arrowleft', '', Scope.MessageView, () => {
if (!this.fullScreenMode() && MessageUserStore.message() && SettingsUserStore.usePreviewPane()
&& !this.oMessageScrollerDom.scrollLeft) {
AppUserStore.focusedState(Scope.MessageList);
return false;
}
});
shortcuts.add('tab', 'shift', Scope.MessageView, () => {
2021-08-13 02:17:37 +08:00
if (!this.fullScreenMode() && MessageUserStore.message() && SettingsUserStore.usePreviewPane()) {
AppUserStore.focusedState(Scope.MessageList);
}
return false;
});
}
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
isDraftFolder() {
return MessageUserStore.message() && FolderUserStore.draftFolder() === MessageUserStore.message().folder;
}
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
isSentFolder() {
return MessageUserStore.message() && FolderUserStore.sentFolder() === MessageUserStore.message().folder;
}
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
isSpamFolder() {
return MessageUserStore.message() && FolderUserStore.spamFolder() === MessageUserStore.message().folder;
}
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
isSpamDisabled() {
return MessageUserStore.message() && FolderUserStore.spamFolder() === UNUSED_OPTION_VALUE;
}
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
isArchiveFolder() {
return MessageUserStore.message() && FolderUserStore.archiveFolder() === MessageUserStore.message().folder;
}
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
isArchiveDisabled() {
return MessageUserStore.message() && FolderUserStore.archiveFolder() === UNUSED_OPTION_VALUE;
2016-06-30 08:02:45 +08:00
}
/**
* @returns {boolean}
*/
isDraftOrSentFolder() {
return this.isDraftFolder() || this.isSentFolder();
2016-06-30 08:02:45 +08:00
}
composeClick() {
showMessageComposer();
2016-06-30 08:02:45 +08:00
}
editMessage() {
if (MessageUserStore.message()) {
showMessageComposer([ComposeType.Draft, MessageUserStore.message()]);
}
2016-06-30 08:02:45 +08:00
}
scrollMessageToTop() {
this.oMessageScrollerDom.scrollTop = (50 < this.oMessageScrollerDom.scrollTop) ? 50 : 0;
2016-06-30 08:02:45 +08:00
}
scrollMessageToLeft() {
this.oMessageScrollerDom.scrollLeft = 0;
2016-06-30 08:02:45 +08:00
}
2015-04-25 06:15:11 +08:00
downloadAsZip() {
2021-08-13 02:17:37 +08:00
const hashes = (MessageUserStore.message() ? MessageUserStore.message().attachments : [])
.map(item => (item && !item.isLinked && item.checked() ? item.download : ''))
.filter(v => v);
if (hashes.length) {
Remote.attachmentsActions('Zip', hashes, this.downloadAsZipLoading)
2021-08-25 19:22:40 +08:00
.then(result => {
if (result && result.Result && result.Result.FileHash) {
rl.app.download(attachmentDownload(result.Result.FileHash));
2019-07-05 03:19:24 +08:00
} else {
this.downloadAsZipError(true);
}
})
2021-08-25 19:22:40 +08:00
.catch(() => this.downloadAsZipError(true));
2019-07-05 03:19:24 +08:00
} else {
this.highlightUnselectedAttachments(true);
}
2016-06-30 08:02:45 +08:00
}
2015-04-14 02:45:09 +08:00
/**
* @param {MessageModel} oMessage
* @returns {void}
*/
2021-08-13 02:17:37 +08:00
showImages() {
MessageUserStore.message() && MessageUserStore.message().showExternalImages();
2016-06-30 08:02:45 +08:00
}
/**
* @returns {string}
*/
printableCheckedMessageCount() {
2021-03-30 17:48:22 +08:00
const cnt = MessageUserStore.listCheckedOrSelectedUidsWithSubMails().length;
2021-03-13 00:08:58 +08:00
return 0 < cnt ? (100 > cnt ? cnt : '99+') : '';
2016-06-30 08:02:45 +08:00
}
/**
* @param {MessageModel} oMessage
* @returns {void}
*/
2021-08-13 02:17:37 +08:00
readReceipt() {
let oMessage = MessageUserStore.message()
if (oMessage && oMessage.readReceipt()) {
2019-07-05 03:19:24 +08:00
Remote.sendReadReceiptMessage(
2021-11-13 16:45:06 +08:00
null,
2020-10-23 21:15:54 +08:00
oMessage.folder,
2019-07-05 03:19:24 +08:00
oMessage.uid,
oMessage.readReceipt(),
i18n('READ_RECEIPT/SUBJECT', { SUBJECT: oMessage.subject() }),
i18n('READ_RECEIPT/BODY', { 'READ-RECEIPT': AccountUserStore.email() })
2019-07-05 03:19:24 +08:00
);
2016-06-30 08:02:45 +08:00
oMessage.isReadReceipt(true);
MessageFlagsCache.store(oMessage);
2014-08-25 15:10:51 +08:00
2020-09-15 15:29:25 +08:00
rl.app.reloadFlagsCurrentMessageListAndMessageFromCache();
}
2016-06-30 08:02:45 +08:00
}
}