Fix lag with multiple selection action (#69)

Reduce significantly time of action on several messages (20 sec to less than 0.5) (#69)
This commit is contained in:
Athozus 2023-04-15 15:39:36 +02:00
parent ccd65d72f7
commit 7e17ff4805
2 changed files with 45 additions and 38 deletions

View File

@ -73,55 +73,70 @@ function mail.sort_messages(messages, sortfield, descending, filter)
end end
-- marks a mail read by its id -- marks a mail read by its id
function mail.mark_read(playername, msg_id) function mail.mark_read(playername, msg_ids)
local entry = mail.get_storage_entry(playername) local entry = mail.get_storage_entry(playername)
for _, msg in ipairs(entry.inbox) do if type(msg_ids) ~= "table" then -- if this is not a table
if msg.id == msg_id then msg_ids = { msg_ids }
msg.read = true end
for _, read_msg_id in ipairs(msg_ids) do
for _, entry_msg in ipairs(entry.inbox) do
if entry_msg.id == read_msg_id then
entry_msg.read = true
end
end
end
mail.set_storage_entry(playername, entry) mail.set_storage_entry(playername, entry)
mail.hud_update(playername, entry.inbox) mail.hud_update(playername, entry.inbox)
return return
end end
end
end
-- marks a mail unread by its id -- marks a mail unread by its id
function mail.mark_unread(playername, msg_id) function mail.mark_unread(playername, msg_ids)
local entry = mail.get_storage_entry(playername) local entry = mail.get_storage_entry(playername)
for _, msg in ipairs(entry.inbox) do if type(msg_ids) ~= "table" then -- if this is not a table
if msg.id == msg_id then msg_ids = { msg_ids }
msg.read = false end
for _, unread_msg_id in ipairs(msg_ids) do
for _, entry_msg in ipairs(entry.inbox) do
if entry_msg.id == unread_msg_id then
entry_msg.read = false
end
end
end
mail.set_storage_entry(playername, entry) mail.set_storage_entry(playername, entry)
return return
end end
end
end
-- deletes a mail by its id -- deletes a mail by its id
function mail.delete_mail(playername, msg_id) function mail.delete_mail(playername, msg_ids)
local entry = mail.get_storage_entry(playername) local entry = mail.get_storage_entry(playername)
if type(msg_ids) ~= "table" then -- if this is not a table
msg_ids = { msg_ids }
end
for i, msg in ipairs(entry.inbox) do for i, msg in ipairs(entry.inbox) do
if msg.id == msg_id then for _, deleted_msg in ipairs(msg_ids) do
if msg.id == deleted_msg then
table.remove(entry.inbox, i) table.remove(entry.inbox, i)
mail.set_storage_entry(playername, entry) end
return
end end
end end
for i, msg in ipairs(entry.outbox) do for i, msg in ipairs(entry.outbox) do
if msg.id == msg_id then for _, deleted_msg in ipairs(msg_ids) do
if msg.id == deleted_msg then
table.remove(entry.outbox, i) table.remove(entry.outbox, i)
mail.set_storage_entry(playername, entry) end
return
end end
end end
for i, msg in ipairs(entry.drafts) do for i, msg in ipairs(entry.drafts) do
if msg.id == msg_id then for _, deleted_msg in ipairs(msg_ids) do
if msg.id == deleted_msg then
table.remove(entry.drafts, i) table.remove(entry.drafts, i)
end
end
end
mail.set_storage_entry(playername, entry) mail.set_storage_entry(playername, entry)
return return
end end
end
end
-- add or update a contact -- add or update a contact
function mail.update_contact(playername, contact) function mail.update_contact(playername, contact)

View File

@ -183,13 +183,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
elseif fields.delete then elseif fields.delete then
if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then -- inbox table if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then -- inbox table
for _, msg_id in ipairs(mail.selected_idxs.inbox[name]) do mail.delete_mail(name, mail.selected_idxs.inbox[name])
mail.delete_mail(name, msg_id)
end
elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then -- sent table elseif formname == "mail:sent" and mail.selected_idxs.sent[name] then -- sent table
for _, msg_id in ipairs(mail.selected_idxs.sent[name]) do mail.delete_mail(name, mail.selected_idxs.sent[name])
mail.delete_mail(name, msg_id)
end
elseif formname == "mail:drafts" and messagesDrafts[mail.selected_idxs.drafts[name]] then -- drafts table elseif formname == "mail:drafts" and messagesDrafts[mail.selected_idxs.drafts[name]] then -- drafts table
mail.delete_mail(name, messagesDrafts[mail.selected_idxs.drafts[name]].id) mail.delete_mail(name, messagesDrafts[mail.selected_idxs.drafts[name]].id)
end end
@ -225,18 +221,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
elseif fields.markread then elseif fields.markread then
if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
for _, msg_id in ipairs(mail.selected_idxs.inbox[name]) do mail.mark_read(name, mail.selected_idxs.inbox[name])
mail.mark_read(name, msg_id)
end
end end
mail.show_mail_menu(name, sortfieldindex, sortdirection, filter) mail.show_mail_menu(name, sortfieldindex, sortdirection, filter)
elseif fields.markunread then elseif fields.markunread then
if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then if formname == "mail:inbox" and mail.selected_idxs.inbox[name] then
for _, msg_id in ipairs(mail.selected_idxs.inbox[name]) do mail.mark_unread(name, mail.selected_idxs.inbox[name])
mail.mark_unread(name, msg_id)
end
end end
mail.show_mail_menu(name, sortfieldindex, sortdirection, filter) mail.show_mail_menu(name, sortfieldindex, sortdirection, filter)