2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2022-01-01 00:02:32 +08:00
|
|
|
import { koComputable } from 'External/ko';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-03-16 23:06:16 +08:00
|
|
|
import { Scope, Notification } from 'Common/Enums';
|
|
|
|
import { MessageSetAction } from 'Common/EnumsUser';
|
2022-01-15 00:13:17 +08:00
|
|
|
import { $htmlCL, elementById } from 'Common/Globals';
|
2022-02-17 16:36:29 +08:00
|
|
|
import { arrayLength, pInt, pString } from 'Common/Utils';
|
|
|
|
import { addObservablesTo, addComputablesTo, addSubscribablesTo } from 'External/ko';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
|
|
|
import {
|
|
|
|
getFolderInboxName,
|
|
|
|
addNewMessageCache,
|
|
|
|
setFolderUidNext,
|
|
|
|
getFolderFromCacheList,
|
|
|
|
setFolderHash,
|
2020-10-25 20:50:26 +08:00
|
|
|
MessageFlagsCache,
|
2016-08-17 06:01:20 +08:00
|
|
|
addRequestedMessage,
|
|
|
|
clearNewMessageCache
|
|
|
|
} from 'Common/Cache';
|
|
|
|
|
2020-11-10 03:14:04 +08:00
|
|
|
import { mailBox } from 'Common/Links';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { i18n, getNotification } from 'Common/Translator';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2020-09-15 15:43:53 +08:00
|
|
|
import { EmailCollectionModel } from 'Model/EmailCollection';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { MessageModel } from 'Model/Message';
|
2020-09-16 21:44:36 +08:00
|
|
|
import { MessageCollectionModel } from 'Model/MessageCollection';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
import { AppUserStore } from 'Stores/User/App';
|
|
|
|
import { AccountUserStore } from 'Stores/User/Account';
|
|
|
|
import { FolderUserStore } from 'Stores/User/Folder';
|
|
|
|
import { SettingsUserStore } from 'Stores/User/Settings';
|
|
|
|
import { NotificationUserStore } from 'Stores/User/Notification';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-08-25 03:10:49 +08:00
|
|
|
//import Remote from 'Remote/User/Fetch'; Circular dependency
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2020-08-07 22:28:30 +08:00
|
|
|
const
|
2022-01-15 00:13:17 +08:00
|
|
|
isChecked = item => item.checked();
|
2020-08-25 15:34:29 +08:00
|
|
|
|
2021-07-26 16:15:50 +08:00
|
|
|
let MessageSeenTimer;
|
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
export const MessageUserStore = new class {
|
2019-07-05 03:19:24 +08:00
|
|
|
constructor() {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.staticMessage = new MessageModel();
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
this.list = ko.observableArray().extend({ debounce: 0 });
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-03-16 17:59:47 +08:00
|
|
|
addObservablesTo(this, {
|
2021-03-12 23:54:37 +08:00
|
|
|
listCount: 0,
|
|
|
|
listSearch: '',
|
2021-09-10 22:28:29 +08:00
|
|
|
listThreadUid: 0,
|
2021-03-12 23:54:37 +08:00
|
|
|
listPage: 1,
|
|
|
|
listPageBeforeThread: 1,
|
|
|
|
listError: '',
|
|
|
|
|
2021-12-16 21:35:29 +08:00
|
|
|
listEndHash: '',
|
2021-09-10 22:28:29 +08:00
|
|
|
listEndThreadUid: 0,
|
2021-03-12 23:54:37 +08:00
|
|
|
|
|
|
|
listLoading: false,
|
2021-12-16 02:02:36 +08:00
|
|
|
// Happens when message(s) removed from list
|
|
|
|
listIsIncomplete: 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,
|
|
|
|
messageViewTrigger: false,
|
|
|
|
messageError: '',
|
2021-03-12 23:54:37 +08:00
|
|
|
messageLoading: false,
|
2020-10-27 18:09:24 +08:00
|
|
|
messageFullScreenMode: false,
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-02-10 23:50:58 +08:00
|
|
|
// Cache mail bodies
|
2020-10-27 18:09:24 +08:00
|
|
|
messagesBodiesDom: null,
|
|
|
|
messageActiveDom: null
|
|
|
|
});
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listDisableAutoSelect = ko.observable(false).extend({ falseTimeout: 500 });
|
2020-10-27 18:09:24 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
// Computed Observables
|
2020-10-27 18:09:24 +08:00
|
|
|
|
2021-09-02 20:21:50 +08:00
|
|
|
addComputablesTo(this, {
|
2021-12-16 02:02:36 +08:00
|
|
|
listIsLoading: () => {
|
|
|
|
const value = this.listLoading() | this.listIsIncomplete();
|
|
|
|
$htmlCL.toggle('list-loading', value);
|
|
|
|
return value;
|
|
|
|
},
|
2022-01-05 18:46:08 +08:00
|
|
|
|
2021-09-02 20:21:50 +08:00
|
|
|
listPageCount: () => Math.max(1, Math.ceil(this.listCount() / SettingsUserStore.messagesPerPage())),
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-09-02 20:21:50 +08:00
|
|
|
mainMessageListSearch: {
|
|
|
|
read: this.listSearch,
|
|
|
|
write: value => rl.route.setHash(
|
2021-03-12 23:54:37 +08:00
|
|
|
mailBox(FolderUserStore.currentFolderFullNameHash(), 1, value.toString().trim(), this.listThreadUid())
|
|
|
|
)
|
2021-09-02 20:21:50 +08:00
|
|
|
},
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-09-02 20:21:50 +08:00
|
|
|
isMessageSelected: () => null !== this.message(),
|
|
|
|
|
|
|
|
listCheckedOrSelected: () => {
|
|
|
|
const
|
|
|
|
selectedMessage = this.selectorMessageSelected(),
|
|
|
|
focusedMessage = this.selectorMessageFocused(),
|
|
|
|
checked = this.list.filter(item => isChecked(item) || item === selectedMessage);
|
|
|
|
return checked.length ? checked : (focusedMessage ? [focusedMessage] : []);
|
|
|
|
},
|
|
|
|
|
|
|
|
listCheckedOrSelectedUidsWithSubMails: () => {
|
|
|
|
let result = [];
|
|
|
|
this.listCheckedOrSelected().forEach(message => {
|
|
|
|
result.push(message.uid);
|
|
|
|
if (1 < message.threadsLen()) {
|
|
|
|
result = result.concat(message.threads()).unique();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2022-01-01 00:02:32 +08:00
|
|
|
this.listChecked = koComputable(() => this.list.filter(isChecked))
|
2019-07-05 03:19:24 +08:00
|
|
|
.extend({ rateLimit: 0 });
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2022-01-01 00:02:32 +08:00
|
|
|
this.hasCheckedMessages = koComputable(() => !!this.list.find(isChecked))
|
2021-02-09 05:04:24 +08:00
|
|
|
.extend({ rateLimit: 0 });
|
|
|
|
|
2022-01-01 00:02:32 +08:00
|
|
|
this.hasCheckedOrSelected = koComputable(() => !!(this.selectorMessageSelected()
|
2021-02-09 05:04:24 +08:00
|
|
|
|| this.selectorMessageFocused()
|
2021-03-12 23:54:37 +08:00
|
|
|
|| this.list.find(item => item.checked())))
|
2021-02-09 05:04:24 +08:00
|
|
|
.extend({ rateLimit: 50 });
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
// Subscribers
|
2021-02-10 19:12:36 +08:00
|
|
|
|
2021-03-16 18:38:40 +08:00
|
|
|
addSubscribablesTo(this, {
|
|
|
|
message: message => {
|
2021-07-26 16:15:50 +08:00
|
|
|
clearTimeout(MessageSeenTimer);
|
2021-03-16 18:38:40 +08:00
|
|
|
if (message) {
|
|
|
|
if (!SettingsUserStore.usePreviewPane()) {
|
2021-03-16 23:06:16 +08:00
|
|
|
AppUserStore.focusedState(Scope.MessageView);
|
2021-03-16 18:38:40 +08:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-16 23:06:16 +08:00
|
|
|
AppUserStore.focusedState(Scope.MessageList);
|
2021-03-16 18:38:40 +08:00
|
|
|
|
|
|
|
this.messageFullScreenMode(false);
|
|
|
|
this.hideMessageBodies();
|
|
|
|
}
|
|
|
|
},
|
2015-04-02 02:18:15 +08:00
|
|
|
|
2021-11-06 19:27:37 +08:00
|
|
|
isMessageSelected: value => elementById('rl-right').classList.toggle('message-selected', value)
|
2016-08-17 06:01:20 +08:00
|
|
|
});
|
2021-03-12 23:54:37 +08:00
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
this.purgeMessageBodyCache = this.purgeMessageBodyCache.throttle(30000);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
purgeMessageBodyCache() {
|
2021-02-10 23:50:58 +08:00
|
|
|
const messagesDom = this.messagesBodiesDom(),
|
|
|
|
children = messagesDom && messagesDom.children;
|
|
|
|
if (children) {
|
|
|
|
while (15 < children.length) {
|
|
|
|
children[0].remove();
|
2015-02-22 06:00:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
initUidNextAndNewMessages(folder, uidNext, newMessages) {
|
2020-08-18 03:57:56 +08:00
|
|
|
if (getFolderInboxName() === folder && uidNext) {
|
2021-07-22 03:34:17 +08:00
|
|
|
if (arrayLength(newMessages)) {
|
2020-11-10 16:59:56 +08:00
|
|
|
newMessages.forEach(item => addNewMessageCache(folder, item.Uid));
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
NotificationUserStore.playSoundNotification();
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-09-13 04:50:21 +08:00
|
|
|
const len = newMessages.length;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (3 < len) {
|
2021-03-11 05:41:35 +08:00
|
|
|
NotificationUserStore.displayDesktopNotification(
|
|
|
|
AccountUserStore.email(),
|
2016-08-17 06:01:20 +08:00
|
|
|
i18n('MESSAGE_LIST/NEW_MESSAGE_NOTIFICATION', {
|
2021-03-25 04:26:40 +08:00
|
|
|
COUNT: len
|
2016-08-17 06:01:20 +08:00
|
|
|
}),
|
2021-10-29 21:25:00 +08:00
|
|
|
{ Url: mailBox(newMessages[0].Folder) }
|
2015-02-22 06:00:51 +08:00
|
|
|
);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2020-07-22 20:49:18 +08:00
|
|
|
newMessages.forEach(item => {
|
2021-03-11 05:41:35 +08:00
|
|
|
NotificationUserStore.displayDesktopNotification(
|
2020-09-15 15:43:53 +08:00
|
|
|
EmailCollectionModel.reviveFromJson(item.From).toString(),
|
2016-08-17 06:01:20 +08:00
|
|
|
item.Subject,
|
2021-03-25 04:26:40 +08:00
|
|
|
{ Folder: item.Folder, Uid: item.Uid }
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
setFolderUidNext(folder, uidNext);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
hideMessageBodies() {
|
|
|
|
const messagesDom = this.messagesBodiesDom();
|
2021-02-10 23:50:58 +08:00
|
|
|
messagesDom && Array.from(messagesDom.children).forEach(el => el.hidden = true);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string} fromFolderFullName
|
2016-08-17 06:01:20 +08:00
|
|
|
* @param {Array} uidForRemove
|
2021-11-30 17:19:43 +08:00
|
|
|
* @param {string=} toFolderFullName = ''
|
2016-12-15 05:56:17 +08:00
|
|
|
* @param {boolean=} copy = false
|
2016-08-17 06:01:20 +08:00
|
|
|
*/
|
2021-11-30 17:19:43 +08:00
|
|
|
removeMessagesFromList(fromFolderFullName, uidForRemove, toFolderFullName = '', copy = false) {
|
2020-07-23 02:09:31 +08:00
|
|
|
uidForRemove = uidForRemove.map(mValue => pInt(mValue));
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
let unseenCount = 0,
|
2021-03-12 23:54:37 +08:00
|
|
|
messageList = this.list,
|
2016-08-17 06:01:20 +08:00
|
|
|
currentMessage = this.message();
|
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
const trashFolder = FolderUserStore.trashFolder(),
|
|
|
|
spamFolder = FolderUserStore.spamFolder(),
|
2021-11-30 17:19:43 +08:00
|
|
|
fromFolder = getFolderFromCacheList(fromFolderFullName),
|
|
|
|
toFolder = toFolderFullName ? getFolderFromCacheList(toFolderFullName) : null,
|
2019-07-05 03:19:24 +08:00
|
|
|
messages =
|
2021-11-30 17:19:43 +08:00
|
|
|
FolderUserStore.currentFolderFullName() === fromFolderFullName
|
2020-07-21 03:29:44 +08:00
|
|
|
? messageList.filter(item => item && uidForRemove.includes(pInt(item.uid)))
|
2019-07-05 03:19:24 +08:00
|
|
|
: [];
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2020-07-22 20:49:18 +08:00
|
|
|
messages.forEach(item => {
|
2020-10-23 21:15:54 +08:00
|
|
|
if (item && item.isUnseen()) {
|
2020-09-20 17:29:31 +08:00
|
|
|
++unseenCount;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (toFolder) {
|
2021-11-23 04:01:30 +08:00
|
|
|
if (trashFolder === toFolder.fullName || spamFolder === toFolder.fullName) {
|
2016-08-17 06:01:20 +08:00
|
|
|
unseenCount = 0;
|
|
|
|
}
|
2015-06-05 02:02:31 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
toFolder.messageCountAll(toFolder.messageCountAll() + uidForRemove.length);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (0 < unseenCount) {
|
2016-08-17 06:01:20 +08:00
|
|
|
toFolder.messageCountUnread(toFolder.messageCountUnread() + unseenCount);
|
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
toFolder.actionBlink(true);
|
2015-02-22 06:00:51 +08:00
|
|
|
}
|
|
|
|
|
2020-07-28 18:35:41 +08:00
|
|
|
if (messages.length) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (copy) {
|
2021-03-12 23:54:37 +08:00
|
|
|
messages.forEach(item => item.checked(false));
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2021-12-16 02:02:36 +08:00
|
|
|
this.listIsIncomplete(true);
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2020-07-22 20:49:18 +08:00
|
|
|
messages.forEach(item => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (currentMessage && currentMessage.hash === item.hash) {
|
2016-08-17 06:01:20 +08:00
|
|
|
currentMessage = null;
|
|
|
|
this.message(null);
|
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
item.deleted(true);
|
2016-06-30 08:02:45 +08:00
|
|
|
});
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
setTimeout(() => messages.forEach(item => messageList.remove(item)), 350);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-11-30 17:19:43 +08:00
|
|
|
if (fromFolderFullName) {
|
|
|
|
setFolderHash(fromFolderFullName, '');
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-11-30 17:19:43 +08:00
|
|
|
if (toFolderFullName) {
|
|
|
|
setFolderHash(toFolderFullName, '');
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-04-22 05:01:29 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
if (this.listThreadUid()) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (
|
2020-07-28 18:35:41 +08:00
|
|
|
messageList.length &&
|
2021-09-10 22:28:29 +08:00
|
|
|
!!messageList.find(item => !!(item && item.deleted() && item.uid == this.listThreadUid()))
|
2019-07-05 03:19:24 +08:00
|
|
|
) {
|
2020-07-22 16:43:19 +08:00
|
|
|
const message = messageList.find(item => item && !item.deleted());
|
2021-09-10 22:28:29 +08:00
|
|
|
if (message && this.listThreadUid() != message.uid) {
|
|
|
|
this.listThreadUid(message.uid);
|
2015-04-22 05:01:29 +08:00
|
|
|
|
2020-09-17 02:35:29 +08:00
|
|
|
rl.route.setHash(
|
2019-07-05 03:19:24 +08:00
|
|
|
mailBox(
|
2021-03-11 05:41:35 +08:00
|
|
|
FolderUserStore.currentFolderFullNameHash(),
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listPage(),
|
|
|
|
this.listSearch(),
|
|
|
|
this.listThreadUid()
|
2019-07-05 03:19:24 +08:00
|
|
|
),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} else if (!message) {
|
2021-03-12 23:54:37 +08:00
|
|
|
if (1 < this.listPage()) {
|
|
|
|
this.listPage(this.listPage() - 1);
|
2019-07-05 03:19:24 +08:00
|
|
|
|
2020-09-17 02:35:29 +08:00
|
|
|
rl.route.setHash(
|
2019-07-05 03:19:24 +08:00
|
|
|
mailBox(
|
2021-03-11 05:41:35 +08:00
|
|
|
FolderUserStore.currentFolderFullNameHash(),
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listPage(),
|
|
|
|
this.listSearch(),
|
|
|
|
this.listThreadUid()
|
2019-07-05 03:19:24 +08:00
|
|
|
),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-10 22:28:29 +08:00
|
|
|
this.listThreadUid(0);
|
2015-04-22 05:01:29 +08:00
|
|
|
|
2020-09-17 02:35:29 +08:00
|
|
|
rl.route.setHash(
|
2019-07-05 03:19:24 +08:00
|
|
|
mailBox(
|
2021-03-11 05:41:35 +08:00
|
|
|
FolderUserStore.currentFolderFullNameHash(),
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listPageBeforeThread(),
|
|
|
|
this.listSearch()
|
2019-07-05 03:19:24 +08:00
|
|
|
),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-04-22 05:01:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
setMessage(data, cached, oMessage) {
|
2019-07-05 03:19:24 +08:00
|
|
|
let isNew = false,
|
2020-10-23 21:15:54 +08:00
|
|
|
json = data && data.Result,
|
2021-07-19 17:57:47 +08:00
|
|
|
message = oMessage || this.message();
|
2016-08-17 06:01:20 +08:00
|
|
|
|
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
|
|
|
) {
|
2022-01-15 00:13:17 +08:00
|
|
|
const threads = message.threads(),
|
|
|
|
messagesDom = this.messagesBodiesDom();
|
|
|
|
if (!oMessage && message.uid != json.Uid && threads.includes(json.Uid)) {
|
|
|
|
message = MessageModel.reviveFromJson(json);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (message) {
|
2016-08-17 06:01:20 +08:00
|
|
|
message.threads(threads);
|
2020-10-25 20:50:26 +08:00
|
|
|
MessageFlagsCache.initMessage(message);
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.message(this.staticMessage.populateByMessageListItem(message));
|
|
|
|
message = this.message();
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
isNew = true;
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-09-10 22:28:29 +08:00
|
|
|
if (message && message.uid == json.Uid) {
|
2021-07-19 17:57:47 +08:00
|
|
|
oMessage || this.messageError('');
|
2021-12-06 23:08:58 +08:00
|
|
|
/*
|
2020-10-23 21:15:54 +08:00
|
|
|
if (cached) {
|
2021-12-06 23:08:58 +08:00
|
|
|
delete json.Flags;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2021-12-06 23:08:58 +08:00
|
|
|
*/
|
2022-01-15 00:13:17 +08:00
|
|
|
isNew || message.revivePropertiesFromJson(json);
|
2020-10-23 21:15:54 +08:00
|
|
|
addRequestedMessage(message.folder, message.uid);
|
2019-07-05 03:19:24 +08:00
|
|
|
if (messagesDom) {
|
2022-01-13 19:00:12 +08:00
|
|
|
let id = 'rl-msg-' + message.hash.replace(/[^a-zA-Z0-9]/g, ''),
|
|
|
|
body = elementById(id);
|
|
|
|
if (body) {
|
|
|
|
message.body = body;
|
2022-01-15 00:13:17 +08:00
|
|
|
message.isHtml(body.classList.contains('html'));
|
|
|
|
message.hasImages(body.rlHasImages);
|
2020-08-27 21:45:47 +08:00
|
|
|
} else {
|
2021-02-10 23:50:58 +08:00
|
|
|
body = Element.fromHTML('<div id="' + id + '" hidden="" class="b-text-part '
|
2022-01-17 22:58:23 +08:00
|
|
|
+ (message.pgpSigned() ? ' openpgp-signed' : '')
|
2022-02-17 17:18:47 +08:00
|
|
|
+ (message.pgpEncrypted() ? ' openpgp-encrypted' : '')
|
2022-01-13 19:00:12 +08:00
|
|
|
+ '">'
|
2020-08-27 21:45:47 +08:00
|
|
|
+ '</div>');
|
2020-09-16 02:40:55 +08:00
|
|
|
message.body = body;
|
2022-01-15 00:13:17 +08:00
|
|
|
if (!SettingsUserStore.viewHTML() || !message.viewHtml()) {
|
|
|
|
message.viewPlain();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
this.purgeMessageBodyCache();
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2022-01-13 19:00:12 +08:00
|
|
|
messagesDom.append(body);
|
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
oMessage || this.messageActiveDom(message.body);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
oMessage || this.hideMessageBodies();
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
oMessage || (message.body.hidden = false);
|
|
|
|
oMessage && message.viewPopupMessage();
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-10-25 20:50:26 +08:00
|
|
|
MessageFlagsCache.initMessage(message);
|
2021-09-02 20:21:50 +08:00
|
|
|
if (message.isUnseen()) {
|
2021-07-26 16:15:50 +08:00
|
|
|
MessageSeenTimer = setTimeout(
|
|
|
|
() => rl.app.messageListAction(message.folder, MessageSetAction.SetSeen, [message]),
|
2021-09-02 20:21:50 +08:00
|
|
|
SettingsUserStore.messageReadDelay() * 1000 // seconds
|
2021-07-26 16:15:50 +08:00
|
|
|
);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2022-01-15 00:13:17 +08:00
|
|
|
if (message && isNew) {
|
|
|
|
let selectedMessage = this.selectorMessageSelected();
|
2019-07-05 03:19:24 +08:00
|
|
|
if (
|
|
|
|
selectedMessage &&
|
2021-09-10 22:28:29 +08:00
|
|
|
(message.folder !== selectedMessage.folder || message.uid != selectedMessage.uid)
|
2019-07-05 03:19:24 +08:00
|
|
|
) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.selectorMessageSelected(null);
|
2021-03-12 23:54:37 +08:00
|
|
|
if (1 === this.list.length) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.selectorMessageFocused(null);
|
|
|
|
}
|
2022-01-15 00:13:17 +08:00
|
|
|
} else if (!selectedMessage) {
|
2021-03-12 23:54:37 +08:00
|
|
|
selectedMessage = this.list.find(
|
2020-07-22 16:43:19 +08:00
|
|
|
subMessage =>
|
2019-07-05 03:19:24 +08:00
|
|
|
subMessage &&
|
2020-10-23 21:15:54 +08:00
|
|
|
subMessage.folder === message.folder &&
|
2021-09-10 22:28:29 +08:00
|
|
|
subMessage.uid == message.uid
|
2016-08-17 06:01:20 +08:00
|
|
|
);
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (selectedMessage) {
|
2016-08-17 06:01:20 +08:00
|
|
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
selectMessage(oMessage) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (oMessage) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.message(this.staticMessage.populateByMessageListItem(oMessage));
|
|
|
|
this.populateMessageBody(this.message());
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.message(null);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-03-06 08:42:40 +08:00
|
|
|
|
2021-09-10 22:28:29 +08:00
|
|
|
selectMessageByFolderAndUid(sFolder, iUid) {
|
|
|
|
if (sFolder && iUid) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.message(this.staticMessage.populateByMessageListItem(null));
|
2020-10-23 21:15:54 +08:00
|
|
|
this.message().folder = sFolder;
|
2021-09-10 22:28:29 +08:00
|
|
|
this.message().uid = iUid;
|
2015-07-07 02:46:44 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.populateMessageBody(this.message());
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.message(null);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-07-07 02:46:44 +08:00
|
|
|
|
2021-07-19 17:57:47 +08:00
|
|
|
populateMessageBody(oMessage, preload) {
|
2021-03-12 23:54:37 +08:00
|
|
|
if (oMessage) {
|
2021-07-19 17:57:47 +08:00
|
|
|
preload || this.hideMessageBodies();
|
|
|
|
preload || this.messageLoading(true);
|
2021-08-25 03:10:49 +08:00
|
|
|
rl.app.Remote.message((iError, oData, bCached) => {
|
2021-04-23 16:47:24 +08:00
|
|
|
if (iError) {
|
2021-07-19 17:57:47 +08:00
|
|
|
if (Notification.RequestAborted !== iError && !preload) {
|
2021-04-23 16:47:24 +08:00
|
|
|
this.message(null);
|
|
|
|
this.messageError(getNotification(iError));
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-19 17:57:47 +08:00
|
|
|
this.setMessage(oData, bCached, preload ? oMessage : null);
|
2021-04-23 16:47:24 +08:00
|
|
|
}
|
2021-07-19 17:57:47 +08:00
|
|
|
preload || this.messageLoading(false);
|
2021-04-23 16:47:24 +08:00
|
|
|
}, oMessage.folder, oMessage.uid);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
2016-12-15 05:56:17 +08:00
|
|
|
* @param {Array} list
|
2016-08-17 06:01:20 +08:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
calculateMessageListHash(list) {
|
2021-11-30 17:19:43 +08:00
|
|
|
return list.map(message => message.hash + '_' + message.threadsLen() + '_' + message.flagHash()).join(
|
2019-07-05 03:19:24 +08:00
|
|
|
'|'
|
|
|
|
);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2022-02-08 00:27:25 +08:00
|
|
|
reloadFlagsAndCachedMessage() {
|
|
|
|
this.list.forEach(message => MessageFlagsCache.initMessage(message));
|
|
|
|
MessageFlagsCache.initMessage(this.message());
|
|
|
|
this.messageViewTrigger(!this.messageViewTrigger());
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
setMessageList(data, cached) {
|
2021-03-18 19:33:13 +08:00
|
|
|
const collection = MessageCollectionModel.reviveFromJson(data.Result, cached);
|
2020-09-16 21:44:36 +08:00
|
|
|
if (collection) {
|
|
|
|
let unreadCountChange = false;
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-12-16 02:02:36 +08:00
|
|
|
const
|
2020-09-16 21:44:36 +08:00
|
|
|
folder = getFolderFromCacheList(collection.Folder);
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (folder && !cached) {
|
2021-03-18 20:52:56 +08:00
|
|
|
folder.expires = Date.now();
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2020-09-16 21:44:36 +08:00
|
|
|
setFolderHash(collection.Folder, collection.FolderHash);
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2020-09-16 21:44:36 +08:00
|
|
|
if (null != collection.MessageCount) {
|
|
|
|
folder.messageCountAll(collection.MessageCount);
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2020-09-16 21:44:36 +08:00
|
|
|
if (null != collection.MessageUnseenCount) {
|
|
|
|
if (pInt(folder.messageCountUnread()) !== pInt(collection.MessageUnseenCount)) {
|
2016-08-17 06:01:20 +08:00
|
|
|
unreadCountChange = true;
|
2021-11-23 04:01:30 +08:00
|
|
|
MessageFlagsCache.clearFolder(folder.fullName);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2020-09-16 21:44:36 +08:00
|
|
|
folder.messageCountUnread(collection.MessageUnseenCount);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-11-23 04:01:30 +08:00
|
|
|
this.initUidNextAndNewMessages(folder.fullName, collection.UidNext, collection.NewMessages);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-12-16 02:02:36 +08:00
|
|
|
this.listCount(collection.MessageResultCount);
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listSearch(pString(collection.Search));
|
2021-12-16 02:02:36 +08:00
|
|
|
this.listPage(Math.ceil(collection.Offset / SettingsUserStore.messagesPerPage() + 1));
|
2021-12-16 21:35:29 +08:00
|
|
|
this.listThreadUid(collection.ThreadUid);
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-12-16 21:35:29 +08:00
|
|
|
this.listEndHash(
|
|
|
|
collection.Folder +
|
|
|
|
'|' + collection.Search +
|
|
|
|
'|' + this.listThreadUid() +
|
|
|
|
'|' + this.listPage()
|
|
|
|
);
|
|
|
|
this.listEndThreadUid(collection.ThreadUid);
|
|
|
|
const message = this.message();
|
|
|
|
if (message && collection.Folder !== message.folder) {
|
|
|
|
this.message(null);
|
|
|
|
}
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listDisableAutoSelect(true);
|
2015-03-06 08:42:40 +08:00
|
|
|
|
2021-03-12 23:54:37 +08:00
|
|
|
this.list(collection);
|
2021-12-16 02:02:36 +08:00
|
|
|
this.listIsIncomplete(false);
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clearNewMessageCache();
|
2015-02-22 06:00:51 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
if (folder && (cached || unreadCountChange || SettingsUserStore.useThreads())) {
|
2021-11-23 04:01:30 +08:00
|
|
|
rl.app.folderInformation(folder.fullName, collection);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2021-03-12 23:54:37 +08:00
|
|
|
this.listCount(0);
|
|
|
|
this.list([]);
|
2021-03-18 19:33:13 +08:00
|
|
|
this.listError(getNotification(Notification.CantGetMessageList));
|
2015-02-22 06:00:51 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2021-03-11 05:41:35 +08:00
|
|
|
};
|