snappymail/dev/Stores/User/Message.js

752 lines
21 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2021-01-25 05:58:06 +08:00
import { StorageResultType, Notification } from 'Common/Enums';
import { Layout, Focused, MessageSetAction } from 'Common/EnumsUser';
import { doc } from 'Common/Globals';
import { pInt, pString } from 'Common/Utils';
import { plainToHtml } from 'Common/UtilsUser';
import {
getFolderInboxName,
addNewMessageCache,
setFolderUidNext,
getFolderFromCacheList,
setFolderHash,
MessageFlagsCache,
addRequestedMessage,
clearNewMessageCache
} from 'Common/Cache';
import { mailBox } from 'Common/Links';
2019-07-05 03:19:24 +08:00
import { i18n, getNotification } from 'Common/Translator';
import { EmailCollectionModel } from 'Model/EmailCollection';
2019-07-05 03:19:24 +08:00
import { MessageModel } from 'Model/Message';
import { MessageCollectionModel } from 'Model/MessageCollection';
import AppStore from 'Stores/User/App';
import AccountStore from 'Stores/User/Account';
import FolderStore from 'Stores/User/Folder';
import PgpStore from 'Stores/User/Pgp';
import SettingsStore from 'Stores/User/Settings';
import NotificationStore from 'Stores/User/Notification';
import Remote from 'Remote/User/Fetch';
2020-08-07 22:28:30 +08:00
const
hcont = Element.fromHTML('<div area="hidden" style="position:absolute;left:-5000px"></div>'),
2020-08-27 21:45:47 +08:00
getRealHeight = el => {
hcont.innerHTML = el.outerHTML;
const result = hcont.clientHeight;
hcont.innerHTML = '';
2020-08-07 22:28:30 +08:00
return result;
},
/*eslint-disable max-len*/
url = /(^|[\s\n]|\/?>)(https:\/\/[-A-Z0-9+\u0026\u2019#/%?=()~_|!:,.;]*[-A-Z0-9+\u0026#/%=~()_|])/gi,
email = /(^|[\s\n]|\/?>)((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x21\x23-\x5b\x5d-\x7f]|\\[\x21\x23-\x5b\x5d-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x21-\x5a\x53-\x7f]|\\[\x21\x23-\x5b\x5d-\x7f])+)\]))/gi,
findEmailAndLinks = html => html
.replace(url, '$1<a href="$2" target="_blank">$2</a>')
.replace(email, '$1<a href="mailto:$2">$2</a>'),
isChecked = item => item.checked();
let iMessageBodyCacheCount = 0;
doc.body.append(hcont);
2020-08-07 22:28:30 +08:00
2019-07-05 03:19:24 +08:00
class MessageUserStore {
constructor() {
this.staticMessage = new MessageModel();
2015-02-22 06:00:51 +08:00
this.messageList = ko.observableArray().extend({ debounce: 0 });
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
ko.addObservablesTo(this, {
messageListCount: 0,
messageListSearch: '',
messageListThreadUid: '',
messageListPage: 1,
messageListPageBeforeThread: 1,
messageListError: '',
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
messageListEndFolder: '',
messageListEndSearch: '',
messageListEndThreadUid: '',
messageListEndPage: 1,
2015-02-23 00:35:17 +08:00
2020-10-27 18:09:24 +08:00
messageListLoading: false,
messageListIsNotCompleted: false,
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
selectorMessageSelected: null,
selectorMessageFocused: null,
2015-03-06 08:42:40 +08:00
2020-10-27 18:09:24 +08:00
// message viewer
message: null,
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
messageViewTrigger: false,
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
messageError: '',
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
messageCurrentLoading: false,
messageFullScreenMode: false,
2015-02-22 06:00:51 +08:00
2020-10-27 18:09:24 +08:00
messagesBodiesDom: null,
messageActiveDom: null
});
2015-02-22 06:00:51 +08:00
this.messageListCompleteLoadingThrottle = ko.observable(false).extend({ debounce: 200 });
this.messageListCompleteLoadingThrottleForAnimation = ko.observable(false);
2020-10-27 18:09:24 +08:00
this.messageListDisableAutoSelect = ko.observable(false).extend({ falseTimeout: 500 });
this.messageLoadingThrottle = ko.observable(false).extend({ debounce: 50 });
2015-02-22 06:00:51 +08:00
this.messageLoading = ko.computed(() => this.messageCurrentLoading());
2015-02-22 06:00:51 +08:00
this.messageListEndHash = ko.computed(
2019-07-05 03:19:24 +08:00
() =>
this.messageListEndFolder() +
'|' +
this.messageListEndSearch() +
'|' +
this.messageListEndThreadUid() +
'|' +
this.messageListEndPage()
);
2015-02-22 06:00:51 +08:00
this.messageListPageCount = ko.computed(() => {
const page = Math.ceil(this.messageListCount() / SettingsStore.messagesPerPage());
return 0 >= page ? 1 : page;
});
2015-02-22 06:00:51 +08:00
this.mainMessageListSearch = ko.computed({
read: this.messageListSearch,
write: (value) => {
rl.route.setHash(
mailBox(FolderStore.currentFolderFullNameHash(), 1, value.toString().trim(), this.messageListThreadUid())
);
}
});
2015-03-06 08:42:40 +08:00
this.messageListCompleteLoading = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const one = this.messageListLoading(),
two = this.messageListIsNotCompleted();
return one || two;
});
2015-02-22 06:00:51 +08:00
this.isMessageSelected = ko.computed(() => null !== this.message());
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
this.messageListChecked = ko
.computed(() => this.messageList.filter(isChecked))
2019-07-05 03:19:24 +08:00
.extend({ rateLimit: 0 });
2015-02-22 06:00:51 +08:00
this.hasCheckedMessages = ko
.computed(() => !!this.messageList.find(isChecked))
.extend({ rateLimit: 0 });
this.hasCheckedOrSelected = ko
.computed(() => !!(this.selectorMessageSelected()
|| this.selectorMessageFocused()
|| this.messageList.find(item => item.checked())))
.extend({ rateLimit: 50 });
this.messageListCheckedOrSelected = ko.computed(() => {
2019-07-05 03:19:24 +08:00
const checked = this.messageListChecked(),
2016-12-15 05:56:17 +08:00
selectedMessage = this.selectorMessageSelected(),
focusedMessage = this.selectorMessageFocused();
if (checked.length) {
return selectedMessage
? checked.concat([selectedMessage]).unique()
: checked;
}
2017-04-03 05:40:13 +08:00
return selectedMessage ? [selectedMessage] : (focusedMessage ? [focusedMessage] : []);
2016-06-30 08:02:45 +08:00
});
2015-03-06 08:42:40 +08:00
this.messageListCheckedOrSelectedUidsWithSubMails = ko.computed(() => {
let result = [];
this.messageListCheckedOrSelected().forEach(message => {
2019-07-05 03:19:24 +08:00
if (message) {
result.push(message.uid);
2019-07-05 03:19:24 +08:00
if (1 < message.threadsLen()) {
result = result.concat(message.threads()).unique();
}
}
});
return result;
});
this.subscribers();
this.onMessageResponse = this.onMessageResponse.bind(this);
this.purgeMessageBodyCacheThrottle = this.purgeMessageBodyCache.throttle(30000);
}
2015-03-06 08:42:40 +08:00
subscribers() {
let timer = 0, fn = this.messageListCompleteLoadingThrottleForAnimation;
this.messageListCompleteLoading.subscribe((value) => {
value = !!value;
this.messageListCompleteLoadingThrottle(value);
if (value) {
fn(value);
} else if (fn()) {
clearTimeout(timer);
timer = setTimeout(() => {
fn(value);
timer = 0;
}, 700);
} else {
fn(value);
}
});
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
this.messageList.subscribe(
(list=> {
list.forEach(item =>
item && item.newForAnimation() && item.newForAnimation(false)
)
}).debounce(500)
2019-07-05 03:19:24 +08:00
);
2015-02-22 06:00:51 +08:00
this.message.subscribe(message => {
2019-07-05 03:19:24 +08:00
if (message) {
if (Layout.NoPreview === SettingsStore.layout()) {
AppStore.focusedState(Focused.MessageView);
2015-02-22 06:00:51 +08:00
}
2019-07-05 03:19:24 +08:00
} else {
AppStore.focusedState(Focused.MessageList);
this.messageFullScreenMode(false);
this.hideMessageBodies();
2015-02-22 06:00:51 +08:00
}
2016-06-30 08:02:45 +08:00
});
this.messageLoading.subscribe(value => this.messageLoadingThrottle(value));
2015-02-22 06:00:51 +08:00
this.messageListEndFolder.subscribe(folder => {
const message = this.message();
2020-10-23 21:15:54 +08:00
if (message && folder && folder !== message.folder) {
this.message(null);
}
});
}
2015-02-22 06:00:51 +08:00
purgeMessageBodyCache() {
2020-09-30 20:07:03 +08:00
const end = iMessageBodyCacheCount - 15;
2019-07-05 03:19:24 +08:00
if (0 < end) {
2020-08-27 21:45:47 +08:00
let count = 0;
const messagesDom = this.messagesBodiesDom();
2019-07-05 03:19:24 +08:00
if (messagesDom) {
2020-08-27 21:45:47 +08:00
messagesDom.querySelectorAll('.rl-cache-class').forEach(node => {
if (end > node.rlCacheCount) {
node.classList.add('rl-cache-purge');
++count;
}
});
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if (0 < count) {
2020-08-27 21:45:47 +08:00
setTimeout(() => messagesDom.querySelectorAll('.rl-cache-purge').forEach(node => node.remove()), 350);
2015-02-22 06:00:51 +08:00
}
}
}
2016-06-30 08:02:45 +08:00
}
2015-02-22 06:00:51 +08:00
initUidNextAndNewMessages(folder, uidNext, newMessages) {
if (getFolderInboxName() === folder && uidNext) {
2020-09-04 23:07:35 +08:00
if (Array.isNotEmpty(newMessages)) {
newMessages.forEach(item => addNewMessageCache(folder, item.Uid));
2016-06-30 08:02:45 +08:00
NotificationStore.playSoundNotification();
2015-02-22 06:00:51 +08:00
const len = newMessages.length;
2019-07-05 03:19:24 +08:00
if (3 < len) {
2015-02-22 06:00:51 +08:00
NotificationStore.displayDesktopNotification(
AccountStore.email(),
i18n('MESSAGE_LIST/NEW_MESSAGE_NOTIFICATION', {
'COUNT': len
}),
{ 'Url': mailBox(newMessages[0].Folder, 1) }
2015-02-22 06:00:51 +08:00
);
2019-07-05 03:19:24 +08:00
} else {
newMessages.forEach(item => {
NotificationStore.displayDesktopNotification(
EmailCollectionModel.reviveFromJson(item.From).toString(),
item.Subject,
2019-07-05 03:19:24 +08:00
{ 'Folder': item.Folder, 'Uid': item.Uid }
);
});
}
2015-02-22 06:00:51 +08:00
}
setFolderUidNext(folder, uidNext);
}
2016-06-30 08:02:45 +08:00
}
hideMessageBodies() {
const messagesDom = this.messagesBodiesDom();
2020-08-27 21:45:47 +08:00
messagesDom && messagesDom.querySelectorAll('.b-text-part').forEach(el => el.hidden = true);
}
2015-02-22 06:00:51 +08:00
/**
* @param {string} fromFolderFullNameRaw
* @param {Array} uidForRemove
* @param {string=} toFolderFullNameRaw = ''
2016-12-15 05:56:17 +08:00
* @param {boolean=} copy = false
*/
removeMessagesFromList(fromFolderFullNameRaw, uidForRemove, toFolderFullNameRaw = '', copy = false) {
uidForRemove = uidForRemove.map(mValue => pInt(mValue));
2019-07-05 03:19:24 +08:00
let unseenCount = 0,
messageList = this.messageList,
currentMessage = this.message();
2019-07-05 03:19:24 +08:00
const trashFolder = FolderStore.trashFolder(),
spamFolder = FolderStore.spamFolder(),
fromFolder = getFolderFromCacheList(fromFolderFullNameRaw),
toFolder = toFolderFullNameRaw ? getFolderFromCacheList(toFolderFullNameRaw) : null,
currentFolderFullNameRaw = FolderStore.currentFolderFullNameRaw(),
2019-07-05 03:19:24 +08:00
messages =
currentFolderFullNameRaw === fromFolderFullNameRaw
? messageList.filter(item => item && uidForRemove.includes(pInt(item.uid)))
2019-07-05 03:19:24 +08:00
: [];
messages.forEach(item => {
2020-10-23 21:15:54 +08:00
if (item && item.isUnseen()) {
++unseenCount;
}
});
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
if (fromFolder && !copy) {
fromFolder.messageCountAll(
0 <= fromFolder.messageCountAll() - uidForRemove.length ? fromFolder.messageCountAll() - uidForRemove.length : 0
);
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
if (0 < unseenCount) {
fromFolder.messageCountUnread(
0 <= fromFolder.messageCountUnread() - unseenCount ? fromFolder.messageCountUnread() - unseenCount : 0
);
}
2015-02-22 06:00:51 +08:00
}
2019-07-05 03:19:24 +08:00
if (toFolder) {
if (trashFolder === toFolder.fullNameRaw || spamFolder === toFolder.fullNameRaw) {
unseenCount = 0;
}
2015-06-05 02:02:31 +08:00
toFolder.messageCountAll(toFolder.messageCountAll() + uidForRemove.length);
2019-07-05 03:19:24 +08:00
if (0 < unseenCount) {
toFolder.messageCountUnread(toFolder.messageCountUnread() + unseenCount);
}
2015-02-22 06:00:51 +08:00
toFolder.actionBlink(true);
2015-02-22 06:00:51 +08:00
}
if (messages.length) {
2019-07-05 03:19:24 +08:00
if (copy) {
messages.forEach(item => {
item.checked(false);
});
2019-07-05 03:19:24 +08:00
} else {
this.messageListIsNotCompleted(true);
2015-02-22 06:00:51 +08:00
messages.forEach(item => {
2019-07-05 03:19:24 +08:00
if (currentMessage && currentMessage.hash === item.hash) {
currentMessage = null;
this.message(null);
}
2015-02-22 06:00:51 +08:00
item.deleted(true);
2016-06-30 08:02:45 +08:00
});
2015-02-22 06:00:51 +08:00
setTimeout(() => {
messages.forEach(item => {
messageList.remove(item);
});
2020-08-14 04:58:41 +08:00
}, 350);
}
}
2015-02-22 06:00:51 +08:00
if (fromFolderFullNameRaw) {
setFolderHash(fromFolderFullNameRaw, '');
}
2016-06-30 08:02:45 +08:00
if (toFolderFullNameRaw) {
setFolderHash(toFolderFullNameRaw, '');
}
2015-04-22 05:01:29 +08:00
if (this.messageListThreadUid()) {
2019-07-05 03:19:24 +08:00
if (
messageList.length &&
!!messageList.find(item => !!(item && item.deleted() && item.uid === this.messageListThreadUid()))
2019-07-05 03:19:24 +08:00
) {
const message = messageList.find(item => item && !item.deleted());
2019-07-05 03:19:24 +08:00
if (message && this.messageListThreadUid() !== pString(message.uid)) {
this.messageListThreadUid(pString(message.uid));
2015-04-22 05:01:29 +08:00
rl.route.setHash(
2019-07-05 03:19:24 +08:00
mailBox(
FolderStore.currentFolderFullNameHash(),
this.messageListPage(),
this.messageListSearch(),
this.messageListThreadUid()
2019-07-05 03:19:24 +08:00
),
true,
true
);
} else if (!message) {
if (1 < this.messageListPage()) {
this.messageListPage(this.messageListPage() - 1);
rl.route.setHash(
2019-07-05 03:19:24 +08:00
mailBox(
FolderStore.currentFolderFullNameHash(),
this.messageListPage(),
this.messageListSearch(),
this.messageListThreadUid()
),
true,
true
);
} else {
this.messageListThreadUid('');
2015-04-22 05:01:29 +08:00
rl.route.setHash(
2019-07-05 03:19:24 +08:00
mailBox(
FolderStore.currentFolderFullNameHash(),
this.messageListPageBeforeThread(),
this.messageListSearch()
),
true,
true
);
}
2015-04-22 05:01:29 +08:00
}
}
}
2016-06-30 08:02:45 +08:00
}
/**
* @param {Object} messageTextBody
*/
initBlockquoteSwitcher(messageTextBody) {
2020-08-27 21:45:47 +08:00
messageTextBody && messageTextBody.querySelectorAll('blockquote:not(.rl-bq-switcher)').forEach(node => {
if (node.textContent.trim() && !node.parentNode.closest('blockquote')) {
let h = node.clientHeight || getRealHeight(node);
if (0 === h || 100 < h) {
const el = Element.fromHTML('<span class="rlBlockquoteSwitcher">•••</span>');
2020-08-27 21:45:47 +08:00
node.classList.add('rl-bq-switcher','hidden-bq');
node.before(el);
el.addEventListener('click', () => node.classList.toggle('hidden-bq'));
}
}
2020-08-27 21:45:47 +08:00
});
2016-06-30 08:02:45 +08:00
}
/**
2016-12-15 05:56:17 +08:00
* @param {Object} messageTextBody
* @param {Object} message
*/
2016-12-15 05:56:17 +08:00
initOpenPgpControls(messageTextBody, message) {
2020-08-27 21:45:47 +08:00
messageTextBody && messageTextBody.querySelectorAll('.b-plain-openpgp:not(.inited)').forEach(node =>
PgpStore.initMessageBodyControls(node, message)
);
2016-06-30 08:02:45 +08:00
}
2015-04-07 03:32:19 +08:00
setMessage(data, cached) {
2019-07-05 03:19:24 +08:00
let isNew = false,
body = null,
2020-10-23 21:15:54 +08:00
json = data && data.Result,
id = '',
plain = '',
resultHtml = '',
pgpSigned = false,
messagesDom = this.messagesBodiesDom(),
selectedMessage = this.selectorMessageSelected(),
message = this.message();
2019-07-05 03:19:24 +08:00
if (
2020-10-23 21:15:54 +08:00
json &&
MessageModel.validJson(json) &&
2019-07-05 03:19:24 +08:00
message &&
2020-10-23 21:15:54 +08:00
message.folder === json.Folder
2019-07-05 03:19:24 +08:00
) {
const threads = message.threads();
2020-10-23 21:15:54 +08:00
if (message.uid !== json.Uid && 1 < threads.length && threads.includes(json.Uid)) {
message = MessageModel.reviveFromJson(json);
2019-07-05 03:19:24 +08:00
if (message) {
message.threads(threads);
MessageFlagsCache.initMessage(message);
2015-02-22 06:00:51 +08:00
this.message(this.staticMessage.populateByMessageListItem(message));
message = this.message();
2016-06-30 08:02:45 +08:00
isNew = true;
}
2016-06-30 08:02:45 +08:00
}
2015-02-22 06:00:51 +08:00
2020-10-23 21:15:54 +08:00
if (message && message.uid === json.Uid) {
this.messageError('');
2015-02-22 06:00:51 +08:00
2020-10-23 21:15:54 +08:00
if (cached) {
delete json.IsSeen;
delete json.IsFlagged;
delete json.IsAnswered;
delete json.IsForwarded;
delete json.IsReadReceipt;
delete json.IsDeleted;
}
2015-03-06 08:42:40 +08:00
2020-10-23 21:15:54 +08:00
message.revivePropertiesFromJson(json);
addRequestedMessage(message.folder, message.uid);
2019-07-05 03:19:24 +08:00
if (messagesDom) {
id = 'rl-mgs-' + message.hash.replace(/[^a-zA-Z0-9]/g, '');
2016-06-30 08:02:45 +08:00
const textBody = doc.getElementById(id);
2020-08-27 21:45:47 +08:00
if (textBody) {
textBody.rlCacheCount = ++iMessageBodyCacheCount;
message.body = textBody;
message.fetchDataFromDom();
2020-08-27 21:45:47 +08:00
} else {
let isHtml = false;
2020-10-23 21:15:54 +08:00
if (json.Html) {
isHtml = true;
2020-10-23 21:15:54 +08:00
resultHtml = json.Html.toString();
} else if (json.Plain) {
isHtml = false;
2020-10-23 21:15:54 +08:00
resultHtml = plainToHtml(json.Plain.toString());
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
if ((message.isPgpSigned() || message.isPgpEncrypted()) && PgpStore.capaOpenPGP()) {
2020-10-23 21:15:54 +08:00
plain = pString(json.Plain);
2019-07-05 03:19:24 +08:00
const isPgpEncrypted = /---BEGIN PGP MESSAGE---/.test(plain);
if (!isPgpEncrypted) {
pgpSigned =
/-----BEGIN PGP SIGNED MESSAGE-----/.test(plain) && /-----BEGIN PGP SIGNATURE-----/.test(plain);
}
const pre = doc.createElement('pre');
2019-07-05 03:19:24 +08:00
if (pgpSigned && message.isPgpSigned()) {
2020-08-27 21:45:47 +08:00
pre.className = 'b-plain-openpgp signed';
pre.textContent = plain;
2019-07-05 03:19:24 +08:00
} else if (isPgpEncrypted && message.isPgpEncrypted()) {
2020-08-27 21:45:47 +08:00
pre.className = 'b-plain-openpgp encrypted';
pre.textContent = plain;
2019-07-05 03:19:24 +08:00
} else {
2020-08-27 21:45:47 +08:00
pre.innerHTML = resultHtml;
}
2020-08-27 21:45:47 +08:00
resultHtml = pre.outerHTML;
message.isPgpSigned(pgpSigned);
message.isPgpEncrypted(isPgpEncrypted);
2019-07-05 03:19:24 +08:00
} else {
resultHtml = '<pre>' + resultHtml + '</pre>';
2015-07-30 01:21:24 +08:00
}
2019-07-05 03:19:24 +08:00
} else {
isHtml = false;
resultHtml = '<pre>' + resultHtml + '</pre>';
2015-03-06 08:42:40 +08:00
}
2015-02-22 06:00:51 +08:00
body = Element.fromHTML('<div id="' + id + '" hidden="" class="rl-cache-class b-text-part '
2020-08-27 21:45:47 +08:00
+ (isHtml ? 'html' : 'plain') + '">'
+ findEmailAndLinks(resultHtml)
+ '</div>');
body.rlCacheCount = ++iMessageBodyCacheCount;
2015-03-06 08:42:40 +08:00
// Drop Microsoft Office style properties
const rgbRE = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/g,
hex = n => ('0' + parseInt(n).toString(16)).slice(-2);
body.querySelectorAll('[style*=mso]').forEach(el =>
el.setAttribute('style', el.style.cssText.replace(rgbRE, (m,r,g,b) => '#' + hex(r) + hex(g) + hex(b)))
);
message.body = body;
message.isHtml(!!isHtml);
2020-10-23 21:15:54 +08:00
message.hasImages(!!json.HasExternals);
2015-03-06 08:42:40 +08:00
2020-08-27 21:45:47 +08:00
messagesDom.append(body);
2015-02-22 06:00:51 +08:00
message.storeDataInDom();
2015-02-22 06:00:51 +08:00
2020-10-23 21:15:54 +08:00
if (json.HasInternals) {
2020-08-27 21:45:47 +08:00
message.showInternalImages();
}
2019-07-05 03:19:24 +08:00
if (message.hasImages() && SettingsStore.showImages()) {
2020-08-27 21:45:47 +08:00
message.showExternalImages();
}
2015-02-22 06:00:51 +08:00
this.purgeMessageBodyCacheThrottle();
2016-06-30 08:02:45 +08:00
}
this.messageActiveDom(message.body);
2016-06-30 08:02:45 +08:00
this.hideMessageBodies();
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
if (body) {
this.initOpenPgpControls(body, message);
2016-06-30 08:02:45 +08:00
this.initBlockquoteSwitcher(body);
}
2015-03-06 08:42:40 +08:00
2020-08-27 21:45:47 +08:00
message.body.hidden = false;
}
2016-06-30 08:02:45 +08:00
MessageFlagsCache.initMessage(message);
2020-10-23 21:15:54 +08:00
if (message.isUnseen() || message.hasUnseenSubMessage()) {
rl.app.messageListAction(message.folder, MessageSetAction.SetSeen, [message]);
2016-06-30 08:02:45 +08:00
}
2019-07-05 03:19:24 +08:00
if (isNew) {
message = this.message();
2015-03-06 08:42:40 +08:00
2019-07-05 03:19:24 +08:00
if (
selectedMessage &&
message &&
2020-10-23 21:15:54 +08:00
(message.folder !== selectedMessage.folder || message.uid !== selectedMessage.uid)
2019-07-05 03:19:24 +08:00
) {
this.selectorMessageSelected(null);
if (1 === this.messageList.length) {
this.selectorMessageFocused(null);
}
2019-07-05 03:19:24 +08:00
} else if (!selectedMessage && message) {
selectedMessage = this.messageList.find(
subMessage =>
2019-07-05 03:19:24 +08:00
subMessage &&
2020-10-23 21:15:54 +08:00
subMessage.folder === message.folder &&
subMessage.uid === message.uid
);
2019-07-05 03:19:24 +08:00
if (selectedMessage) {
this.selectorMessageSelected(selectedMessage);
this.selectorMessageFocused(selectedMessage);
}
2015-02-22 06:00:51 +08:00
}
}
2015-03-06 08:42:40 +08:00
}
}
2016-06-30 08:02:45 +08:00
}
2015-02-22 06:00:51 +08:00
selectMessage(oMessage) {
2019-07-05 03:19:24 +08:00
if (oMessage) {
this.message(this.staticMessage.populateByMessageListItem(oMessage));
this.populateMessageBody(this.message());
2019-07-05 03:19:24 +08:00
} else {
this.message(null);
}
2016-06-30 08:02:45 +08:00
}
2015-03-06 08:42:40 +08:00
selectMessageByFolderAndUid(sFolder, sUid) {
2019-07-05 03:19:24 +08:00
if (sFolder && sUid) {
this.message(this.staticMessage.populateByMessageListItem(null));
2020-10-23 21:15:54 +08:00
this.message().folder = sFolder;
this.message().uid = sUid;
2015-07-07 02:46:44 +08:00
this.populateMessageBody(this.message());
2019-07-05 03:19:24 +08:00
} else {
this.message(null);
}
2016-06-30 08:02:45 +08:00
}
2015-07-07 02:46:44 +08:00
populateMessageBody(oMessage) {
2020-10-23 21:15:54 +08:00
if (oMessage && Remote.message(this.onMessageResponse, oMessage.folder, oMessage.uid)) {
this.messageCurrentLoading(true);
2015-03-06 08:42:40 +08:00
}
2016-06-30 08:02:45 +08:00
}
2015-02-22 06:00:51 +08:00
/**
* @param {string} sResult
* @param {FetchJsonDefaultResponse} oData
* @param {boolean} bCached
*/
onMessageResponse(sResult, oData, bCached) {
this.hideMessageBodies();
this.messageCurrentLoading(false);
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === sResult && oData && oData.Result) {
this.setMessage(oData, bCached);
2019-07-05 03:19:24 +08:00
} else if (StorageResultType.Unload === sResult) {
this.message(null);
this.messageError('');
2019-07-05 03:19:24 +08:00
} else if (StorageResultType.Abort !== sResult) {
this.message(null);
2019-07-05 03:19:24 +08:00
this.messageError(
oData && oData.ErrorCode ? getNotification(oData.ErrorCode) : getNotification(Notification.UnknownError)
);
}
2016-06-30 08:02:45 +08:00
}
/**
2016-12-15 05:56:17 +08:00
* @param {Array} list
* @returns {string}
*/
calculateMessageListHash(list) {
return list.map(message => '' + message.hash + '_' + message.threadsLen() + '_' + message.flagHash()).join(
2019-07-05 03:19:24 +08:00
'|'
);
}
2016-06-30 08:02:45 +08:00
setMessageList(data, cached) {
const collection = data && MessageCollectionModel.reviveFromJson(data.Result, cached);
if (collection) {
let unreadCountChange = false;
2015-02-22 06:00:51 +08:00
const iCount = collection.MessageResultCount,
iOffset = collection.Offset,
folder = getFolderFromCacheList(collection.Folder);
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
if (folder && !cached) {
folder.interval = Date.now();
setFolderHash(collection.Folder, collection.FolderHash);
if (null != collection.MessageCount) {
folder.messageCountAll(collection.MessageCount);
2016-06-30 08:02:45 +08:00
}
if (null != collection.MessageUnseenCount) {
if (pInt(folder.messageCountUnread()) !== pInt(collection.MessageUnseenCount)) {
unreadCountChange = true;
MessageFlagsCache.clearFolder(folder.fullNameRaw);
}
2015-02-22 06:00:51 +08:00
folder.messageCountUnread(collection.MessageUnseenCount);
}
2016-06-30 08:02:45 +08:00
this.initUidNextAndNewMessages(folder.fullNameRaw, collection.UidNext, collection.NewMessages);
}
2016-06-30 08:02:45 +08:00
this.messageListCount(iCount);
this.messageListSearch(pString(collection.Search));
this.messageListPage(Math.ceil(iOffset / SettingsStore.messagesPerPage() + 1));
this.messageListThreadUid(pString(data.Result.ThreadUid));
2015-02-22 06:00:51 +08:00
this.messageListEndFolder(collection.Folder);
this.messageListEndSearch(this.messageListSearch());
this.messageListEndThreadUid(this.messageListThreadUid());
this.messageListEndPage(this.messageListPage());
2015-02-22 06:00:51 +08:00
this.messageListDisableAutoSelect(true);
2015-03-06 08:42:40 +08:00
this.messageList(collection);
this.messageListIsNotCompleted(false);
2015-02-22 06:00:51 +08:00
clearNewMessageCache();
2015-02-22 06:00:51 +08:00
2019-07-05 03:19:24 +08:00
if (folder && (cached || unreadCountChange || SettingsStore.useThreads())) {
rl.app.folderInformation(folder.fullNameRaw, collection);
}
2019-07-05 03:19:24 +08:00
} else {
this.messageListCount(0);
this.messageList([]);
2019-07-05 03:19:24 +08:00
this.messageListError(getNotification(data && data.ErrorCode ? data.ErrorCode : Notification.CantGetMessageList));
2015-02-22 06:00:51 +08:00
}
2016-06-30 08:02:45 +08:00
}
}
2015-02-22 06:00:51 +08:00
export default new MessageUserStore();