Skip to content

Commit

Permalink
fix(frontend): Remove delete message from different local storage items
Browse files Browse the repository at this point in the history
  • Loading branch information
josaphatim committed Feb 11, 2025
1 parent 18965ef commit bc5c515
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
11 changes: 6 additions & 5 deletions modules/core/js_modules/Hm_MessagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class Hm_MessagesStore {
* @property {RowObject} 1 - An object containing the row message and the IMAP key
*/

constructor(path, page = 1, rows = {}, abortController = new AbortController()) {
constructor(path, page = 1, rows = {}, abortController = new AbortController(), count = 0, pages = 0, offsets = '') {
this.path = path;
this.list = path + '_' + page;
this.rows = rows;
this.count = 0;
this.count = count;
this.flagAsReadOnOpen = true;
this.abortController = abortController;
this.pages = 0;
this.pages = pages;
this.page = page;
this.offsets = '';
this.offsets = offsets;
}

/**
Expand Down Expand Up @@ -137,8 +137,9 @@ class Hm_MessagesStore {
const newRows = rows.filter((_, i) => i !== row.index);
this.rows = Object.fromEntries(newRows);
this.#saveToLocalStorage();
return true;
}

return false;
}

#fetch(hideLoadingState = false) {
Expand Down
24 changes: 18 additions & 6 deletions modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1437,9 +1437,11 @@ var Hm_Utils = {
return false;
},

get_from_local_storage: function(key) {
var prefix = window.location.pathname;
key = prefix+key;
get_from_local_storage: function(key, prefix=true) {
if (prefix) {
const prefix = window.location.pathname;
key = prefix+key;
}
var res = false;
if (hm_encrypt_local_storage()) {
res = Hm_Crypt.decrypt(sessionStorage.getItem(key));
Expand All @@ -1456,16 +1458,19 @@ var Hm_Utils = {
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
if (key_pattern.test(key)) {
const value = get_from_local_storage(key);
const value = Hm_Utils.get_from_local_storage(key, false);
results.push({ key: key, value: value });
}
}
return results;
},

get_local_storage_prefix: function() {
return window.location.pathname;
},

save_to_local_storage: function(key, val) {
var prefix = window.location.pathname;
key = prefix+key;
key = Hm_Utils.get_local_storage_prefix()+key;
if (hm_encrypt_local_storage()) {
val = Hm_Crypt.encrypt(val);
}
Expand All @@ -1482,6 +1487,13 @@ var Hm_Utils = {
return false;
},

delete_from_local_storage: function(key) {
key = Hm_Utils.get_local_storage_prefix()+key;
if ('sessionStorage' in window) {
sessionStorage.removeItem(key);
}
},

clean_selector: function(str) {
return str.replace(/(:|\.|\[|\]|\/)/g, "\\$1");
},
Expand Down
57 changes: 41 additions & 16 deletions modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ var imap_delete_message = function(state, supplied_uid, supplied_detail) {
if (Hm_Utils.get_from_global('msg_uid', false)) {
return;
}
var msg_cache_key = 'imap_'+detail.server_id+'_'+getMessageUidParam()+'_'+detail.folder;
remove_from_cached_imap_pages(msg_cache_key);
remove_from_cached_imap_pages();
var nlink = $('.nlink');
if (nlink.length && Hm_Utils.get_from_global('auto_advance_email_enabled')) {
Hm_Utils.redirect(nlink.attr('href'));
Expand Down Expand Up @@ -363,20 +362,46 @@ var fetch_cached_imap_page = function() {
return [ page, links ];
}

var remove_from_cached_imap_pages = function(msg_cache_key) {
var keys = ['imap_'+Hm_Utils.get_url_page_number()+'_'+getListPathParam()];
if (hm_list_parent()) {
keys.push('imap_'+Hm_Utils.get_url_page_number()+'_'+hm_list_parent());
if (['combined_inbox', 'unread', 'flagged', 'advanced_search', 'search', 'sent'].includes(hm_list_parent())) {
keys.push('formatted_'+hm_list_parent());
}
}
keys.forEach(function(key) {
var data = Hm_Utils.get_from_local_storage(key);
if (data) {
var page_data = $('<div></div>').append(data);
page_data.find('.'+msg_cache_key).remove();
Hm_Utils.save_to_local_storage(key, page_data.html());
var remove_from_cached_imap_pages = async function() {
// Delete the message
const store = new Hm_MessagesStore(getListPathParam(), Hm_Utils.get_url_page_number());
await store.load(false, true);
store.removeRow(getMessageUidParam());

// Delete the message content itself from storage
Hm_Utils.delete_from_local_storage(getMessageUidParam()+'_'+getListPathParam());

// The same message can be in combined inbox, unread so we need to remove it from there as well
const sources = [
'combined_inbox',
'unread',
'flagged',
'advanced_search',
'search',
'sent',
'junk',
'snoozed',
'trash'
];
sources.forEach((source) => {
const localStorageEntries = Hm_Utils.search_from_local_storage(source);
for (let i = 0; i < localStorageEntries.length; i++) {
const entryData = JSON.parse(localStorageEntries[i].value);
const prefix = Hm_Utils.get_local_storage_prefix();
let entryKey = localStorageEntries[i].key;
entryKey = entryKey.slice(prefix.length);

const lastUnderscore = entryKey.lastIndexOf("_");
const path = entryKey.slice(0, lastUnderscore);
const pageNumber = entryKey.slice(lastUnderscore + 1);
if (isNaN(parseInt(pageNumber))) {
continue;
}

const store = new Hm_MessagesStore(path, pageNumber, entryData.rows, new AbortController(), entryData.count, entryData.pages, entryData.offsets);
if (store.removeRow(getMessageUidParam())) {
break;
}
}
});
}
Expand Down

0 comments on commit bc5c515

Please sign in to comment.