Skip to content

Commit a5498a4

Browse files
committed
fix(frontend,backend): pagination links build fix, reload message list after cache is loaded, check for deleted messages only for imap
1 parent 8e43949 commit a5498a4

File tree

3 files changed

+72
-69
lines changed

3 files changed

+72
-69
lines changed

modules/core/js_modules/Hm_MessagesStore.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Hm_MessagesStore {
99
* @property {String} 1 - The IMAP key
1010
*/
1111

12-
/**
12+
/**
1313
* @typedef {Array} RowEntry
1414
* @property {String} 0 - The IMAP key
1515
* @property {RowObject} 1 - An object containing the row message and the IMAP key
@@ -43,7 +43,7 @@ class Hm_MessagesStore {
4343
* as well as combined source paths like All email, unread, sent, trash, etc.
4444
* When it works on multiple data-sources, you can pass messagesReadyCB to refresh the UI element, so
4545
* user doesn't have to wait for all sources to be loaded to see something on screen.
46-
*
46+
*
4747
* @returns {Promise<this>}
4848
*/
4949
async load(reload = false, hideLoadingState = false, doNotFetch = false, messagesReadyCB = null) {
@@ -54,11 +54,11 @@ class Hm_MessagesStore {
5454
this.pages = parseInt(storedMessages.pages);
5555
this.count = storedMessages.count;
5656
this.flagAsReadOnOpen = storedMessages.flagAsReadOnOpen;
57+
this.sort();
58+
if (messagesReadyCB) {
59+
messagesReadyCB(this);
60+
}
5761
if (!reload) {
58-
this.sort();
59-
if (messagesReadyCB) {
60-
messagesReadyCB(this);
61-
}
6262
return this;
6363
}
6464
}
@@ -132,13 +132,13 @@ class Hm_MessagesStore {
132132
}
133133

134134
/**
135-
*
135+
*
136136
* @param {String} uid the id of the message to be marked as read
137137
* @returns {Boolean} true if the message was marked as read, false otherwise
138138
*/
139139
markRowAsRead(uid) {
140140
const row = this.getRowByUid(uid);
141-
141+
142142
if (row) {
143143
const htmlRow = $(row['0']);
144144
const wasUnseen = htmlRow.find('.unseen').length > 0 || htmlRow.hasClass('unseen');
@@ -147,7 +147,7 @@ class Hm_MessagesStore {
147147
htmlRow.find('.unseen').removeClass('unseen');
148148

149149
row['0'] = htmlRow[0].outerHTML;
150-
150+
151151
this.saveToLocalStorage();
152152

153153
return wasUnseen;
@@ -156,13 +156,13 @@ class Hm_MessagesStore {
156156
}
157157

158158
/**
159-
*
160-
* @param {*} uid
159+
*
160+
* @param {*} uid
161161
* @returns {RowObject|false} the next row entry if found, false otherwise
162162
*/
163163
getNextRowForMessage(uid) {
164164
const row = this.getRowByUid(uid);
165-
165+
166166
if (row) {
167167
const index = this.rows.indexOf(row);
168168
const nextRow = this.rows[index + 1];
@@ -174,8 +174,8 @@ class Hm_MessagesStore {
174174
}
175175

176176
/**
177-
*
178-
* @param {*} uid
177+
*
178+
* @param {*} uid
179179
* @returns {RowObject|false} the previous row entry if found, false otherwise
180180
*/
181181
getPreviousRowForMessage(uid) {
@@ -189,16 +189,16 @@ class Hm_MessagesStore {
189189
}
190190
return false;
191191
}
192-
192+
193193
removeRow(uid) {
194194
const row = this.getRowByUid(uid);
195195
if (row) {
196196
this.rows = this.rows.filter(r => r !== row);
197197
this.saveToLocalStorage();
198198
}
199-
199+
200200
}
201-
201+
202202
updateRow(uid, html) {
203203
const row = this.getRowByUid(uid);
204204
if (row) {
@@ -331,13 +331,13 @@ class Hm_MessagesStore {
331331
* @typedef {Object} RowOutput
332332
* @property {Number} index - The index of the row
333333
* @property {RowEntry} value - The row entry
334-
*
335-
* @param {String} uid
334+
*
335+
* @param {String} uid
336336
* @returns {RowOutput|false} row - The row object if found, false otherwise
337337
*/
338338
getRowByUid(uid) {
339339
const row = this.rows.find(row => $(row['0']).attr('data-uid') == uid);
340-
340+
341341
if (row) {
342342
return row;
343343
}

modules/core/js_modules/actions/pagination.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function refreshNextButton(current) {
2-
const totalPages = $(".pagination .max").text();
2+
const totalPages = $(".pagination .max:first").text();
33
if (parseInt(current) >= parseInt(totalPages)) {
44
$(".pagination .next").prop('disabled', true);
55
} else {
@@ -16,7 +16,7 @@ function refreshPreviousButton(current) {
1616
}
1717

1818
async function nextPage() {
19-
const currentPage = $(".pagination .current").text();
19+
const currentPage = $(".pagination .current:first").text();
2020

2121
const nextPage = parseInt(currentPage) + 1;
2222

@@ -27,7 +27,7 @@ async function nextPage() {
2727
}
2828

2929
async function previousPage() {
30-
const currentPage = $(".pagination .current").text();
30+
const currentPage = $(".pagination .current:first").text();
3131

3232
const previousPage = parseInt(currentPage) - 1;
3333

modules/imap/site.js

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var imap_unhide = function(event) {
5454
imap_hide_action(form, server_id, 0);
5555
};
5656

57-
var imap_test_action = function(event) {
57+
var imap_test_action = function(event) {
5858
$('.imap_folder_data').empty();
5959
event.preventDefault();
6060
var form = $(this).closest('.imap_connect');
@@ -394,7 +394,7 @@ var remove_from_cached_imap_pages = function(msg_cache_key) {
394394
});
395395
}
396396

397-
async function select_imap_folder(path, page = 1,reload, processInTheBackground = false) {
397+
async function select_imap_folder(path, page = 1, reload, processInTheBackground = false) {
398398
const messages = new Hm_MessagesStore(path, page, `${getParam('keyword')}_${getParam('filter')}`, getParam('sort'), []);
399399
await messages.load(reload, processInTheBackground, false, () => {
400400
if (processInTheBackground) {
@@ -457,7 +457,7 @@ var setup_imap_folder_page = async function(listPath, listPage = 1) {
457457
select_imap_folder(listPath, listPage, true);
458458
}
459459
});
460-
$('.imap_filter').on("change", function(e) {
460+
$('.imap_filter').on("change", function(e) {
461461
e.preventDefault();
462462
$('#imap_filter_form').trigger('submit');
463463
});
@@ -470,7 +470,8 @@ var setup_imap_folder_page = async function(listPath, listPage = 1) {
470470
$('#imap_filter_form').trigger('submit');
471471
});
472472

473-
await select_imap_folder(listPath, listPage);
473+
// try to fetch from cache but also reload messages, so user don't wait 60 seconds to see the new list (useful for read/unread UI and other updates)
474+
await select_imap_folder(listPath, listPage, true);
474475
handleMessagesDragAndDrop();
475476

476477
// Refresh in the background each 60 seconds
@@ -484,13 +485,13 @@ var setup_imap_folder_page = async function(listPath, listPage = 1) {
484485
};
485486
};
486487

487-
$(document).on('submit', '#imap_filter_form', async function(event) {
488+
$(document).on('submit', '#imap_filter_form', async function(event) {
488489
event.preventDefault();
489490
const url = new URL(location.href);
490491
url.search = $(this).serialize();
491492
history.pushState(history.state, "", url.toString());
492493
location.next = url.search;
493-
try {
494+
try {
494495
const messages = new Hm_MessagesStore(getListPathParam(), Hm_Utils.get_url_page_number(), `${getParam('keyword')}_${getParam('filter')}`, getParam('sort'));
495496
await messages.load(!messages.hasLocalData(), false, false, () => {
496497
display_imap_mailbox(messages.rows, messages.list, messages);
@@ -535,14 +536,14 @@ function getMessageStorageKey(uid) {
535536
async function markPrefetchedMessagesAsRead(uid) {
536537
const listPath = getListPathParam();
537538
const detail = Hm_Utils.parse_folder_path(listPath, 'imap');
538-
const msgId = `${detail.type}_${detail.server_id}_${uid}_${detail.folder}`;
539+
const msgId = `${detail.type}_${detail.server_id}_${uid}_${detail.folder}`;
539540

540541
const messages = new Hm_MessagesStore(listPath, Hm_Utils.get_url_page_number(), `${getParam('keyword')}_${getParam('filter')}`, getParam('sort'));
541542
await messages.load(false, true);
542543
if (!messages.flagAsReadOnOpen) {
543544
return;
544545
}
545-
546+
546547
if (messages.markRowAsRead(uid)) {
547548
const folderId = `${detail.type}_${detail.server_id}_${detail.folder}`;
548549
Hm_Folders.unread_counts[folderId] -= 1;
@@ -584,8 +585,8 @@ var prefetch_imap_folders = function() {
584585
{'name': 'imap_server_id', 'value': id},
585586
{'name': 'imap_prefetch', 'value': true},
586587
{'name': 'folder', 'value': ''}],
587-
function(res) {
588-
$('#imap_prefetch_ids').val(ids.join(','));
588+
function(res) {
589+
$('#imap_prefetch_ids').val(ids.join(','));
589590
prefetch_imap_folders();
590591
if ($('.email_folders ul.folders li').length == 1) {
591592
expand_imap_mailbox(res);
@@ -655,7 +656,7 @@ var get_message_content = function(msg_part, uid, list_path, listParent, detail,
655656
}
656657
globals.auto_advance_email_enabled = Boolean(res.auto_advance_email_enabled);
657658
};
658-
659+
659660
if (!msg_part) {
660661
var msgContent = get_local_message_content(uid, list_path);
661662
if (msgContent) {
@@ -666,38 +667,40 @@ var get_message_content = function(msg_part, uid, list_path, listParent, detail,
666667
}
667668
}
668669

669-
Hm_Ajax.request(
670-
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_message_content'},
671-
{'name': 'imap_msg_uid', 'value': uid},
672-
{'name': 'imap_msg_part', 'value': msg_part},
673-
{'name': 'imap_server_id', 'value': detail.server_id},
674-
{'name': 'folder', 'value': detail.folder}],
675-
function(res) {
676-
if (res.msg_text === '<div class="msg_text_inner"></div>' && noupdate) {
677-
const modal = new Hm_Modal({
678-
title: 'Message not found',
679-
modalId: 'messageNotFoundModal',
680-
})
681-
modal.setContent("The email you're looking for isn't here.<br>It may have been deleted, moved.");
682-
modal.addFooterBtn('Go to message list', 'btn-success', function() {
683-
Hm_Utils.redirect("?page=message_list&list_path="+listParent);
684-
});
685-
modal.addFooterBtn('Search mail', 'btn-success', function() {
686-
Hm_Utils.redirect("?page=search");
687-
});
688-
modal.open();
689-
}
690-
if (!noupdate) {
691-
onSuccess(res);
692-
}
693-
if (!noupdate && !msg_part) {
694-
Hm_Utils.save_to_local_storage(getMessageStorageKey(uid), JSON.stringify(res));
695-
}
696-
},
697-
[],
698-
false,
699-
callback
700-
);
670+
if (detail.type == 'imap') {
671+
Hm_Ajax.request(
672+
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_message_content'},
673+
{'name': 'imap_msg_uid', 'value': uid},
674+
{'name': 'imap_msg_part', 'value': msg_part},
675+
{'name': 'imap_server_id', 'value': detail.server_id},
676+
{'name': 'folder', 'value': detail.folder}],
677+
function(res) {
678+
if (res.msg_text === '<div class="msg_text_inner"></div>' && noupdate) {
679+
const modal = new Hm_Modal({
680+
title: 'Message not found',
681+
modalId: 'messageNotFoundModal',
682+
})
683+
modal.setContent("You are looking at a cached version of a message that might have been deleted or moved from the server.");
684+
modal.addFooterBtn('Go to message list', 'btn-success', function() {
685+
Hm_Utils.redirect("?page=message_list&list_path="+listParent);
686+
});
687+
modal.addFooterBtn('Search mail', 'btn-success', function() {
688+
Hm_Utils.redirect("?page=search");
689+
});
690+
modal.open();
691+
}
692+
if (!noupdate) {
693+
onSuccess(res);
694+
}
695+
if (!noupdate && !msg_part) {
696+
Hm_Utils.save_to_local_storage(getMessageStorageKey(uid), JSON.stringify(res));
697+
}
698+
},
699+
[],
700+
false,
701+
callback
702+
);
703+
}
701704
}
702705
return false;
703706
};
@@ -852,7 +855,7 @@ var imap_setup_message_view_page = function(uid, details, list_path, listParent,
852855
if (!uid) {
853856
uid = getMessageUidParam();
854857
}
855-
858+
856859
const msg_content = get_local_message_content(uid, list_path);
857860
const noupdate = Boolean(msg_content);
858861
get_message_content(false, uid, list_path, listParent, details, callback, noupdate);
@@ -988,7 +991,7 @@ var imap_perform_move_copy = function(dest_id, context, action = null) {
988991
{'name': 'imap_move_page', 'value': page},
989992
{'name': 'imap_move_action', 'value': action}],
990993
async function(res) {
991-
994+
992995
var index;
993996
const store = new Hm_MessagesStore(getListPathParam(), Hm_Utils.get_url_page_number(), `${getParam('keyword')}_${getParam('filter')}`, getParam('sort'));
994997
await store.load(false, true, true);
@@ -1191,7 +1194,7 @@ var imap_setup_snooze = function() {
11911194
const path = getParam("list_parent") || getListPathParam();
11921195
const store = new Hm_MessagesStore(path, Hm_Utils.get_url_page_number(), `${getParam('keyword')}_${getParam('filter')}`, getParam('sort'));
11931196
await store.load(false, true, true);
1194-
1197+
11951198
snoozedMessages.forEach((msg) => {
11961199
store.removeRow(msg);
11971200
});

0 commit comments

Comments
 (0)