From 6453c56def224ab11e1dae43a82fe8faee960082 Mon Sep 17 00:00:00 2001 From: Athozus Date: Wed, 1 Mar 2023 18:21:33 +0100 Subject: [PATCH] Rework on editing/deletion of contacts/maillists --- gui.lua | 57 +++++++++++++++++++++++++++++++++++++---------------- storage.lua | 35 ++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 21 deletions(-) diff --git a/gui.lua b/gui.lua index 2169d6a..809c197 100644 --- a/gui.lua +++ b/gui.lua @@ -215,11 +215,11 @@ function mail.show_maillists(name) end if selected_idxs.maillists[name] then formspec[#formspec + 1] = ";" - formspec[#formspec + 1] = tostring(selected_idxs.maillists[name] + 1) + formspec[#formspec + 1] = selected_idxs.maillists[name] end formspec[#formspec + 1] = "]" else - formspec[#formspec + 1] = "]label[2.25,4.5;No mail]" + formspec[#formspec + 1] = "]label[2.25,4.5;No maillist]" end minetest.show_formspec(name, "mail:maillists", table.concat(formspec, "")) end @@ -376,6 +376,7 @@ function mail.compile_contact_list(name, selected, playernames) formspec[#formspec + 1] = "]" end return table.concat(formspec, "") + end function mail.show_message(name, msgnumber) @@ -866,51 +867,73 @@ function mail.handle_receivefields(player, formname, fields) elseif formname == "mail:maillists" then local name = player:get_player_name() - - if fields.new then + local maillists = mail.getPlayerMaillists(name) + + if fields.maillists then + local evt = minetest.explode_table_event(fields.maillists) + for k, _, i in mail.pairsByKeys(maillists) do + if i == evt.row - 1 then + selected_idxs.maillists[name] = k + break + end + end + if evt.type == "DCL" and maillists[selected_idxs.maillists[name]] then + mail.show_edit_maillist( + name, + maillists[selected_idxs.maillists[name]].name, + maillists[selected_idxs.maillists[name]].desc, + "" + ) + end + + elseif fields.new then selected_idxs.maillists[name] = "#NEW#" mail.show_edit_maillist(name, "", "", "") - + elseif fields.edit and selected_idxs.maillists[name] and maillists[selected_idxs.maillists[name]] then - mail.show_edit_contact( + mail.show_edit_maillist( name, maillists[selected_idxs.maillists[name]].name, - maillists[selected_idxs.maillists[name]].desc + maillists[selected_idxs.maillists[name]].desc, + "" ) elseif fields.delete then - if contacts[selected_idxs.contacts[name]] then - -- delete the contact and set the selected to the next in the list, + if maillists[selected_idxs.maillists[name]] then + -- delete the maillist and set the selected to the next in the list, -- except if it was the last. Then determine the new last local found = false local last = nil - for k in mail.pairsByKeys(contacts) do + for k in mail.pairsByKeys(maillists) do if found then - selected_idxs.contacts[name] = k + selected_idxs.maillists[name] = k break - elseif k == selected_idxs.contacts[name] then - mail.deleteContact(name, contacts[selected_idxs.contacts[name]].name) - selected_idxs.contacts[name] = nil + elseif k == selected_idxs.maillists[name] then + mail.deleteMaillist(name, maillists[selected_idxs.maillists[name]].id) + selected_idxs.maillists[name] = nil found = true else last = k end end - if found and not selected_idxs.contacts[name] then + if found and not selected_idxs.maillists[name] then -- was the last in the list, so take the previous (new last) - selected_idxs.contacts[name] = last + selected_idxs.maillists[name] = last end end mail.show_maillists(name) - + elseif fields.back then + if boxtab_index == 1 then mail.show_inbox(name) elseif boxtab_index == 2 then mail.show_sent(name) end end + + return true elseif formname == "mail:editmaillist" then local name = player:get_player_name() diff --git a/storage.lua b/storage.lua index d0d8c69..6c7c0dc 100644 --- a/storage.lua +++ b/storage.lua @@ -227,6 +227,36 @@ function mail.addPlayerToMaillist(player, ml_id, status) end end +function mail.deleteMaillist(ml_id) + -- remove players attached to the maillist + local maillists_players = mail.getPlayersInMaillists() + for _, player in ipairs(maillist_players) do + if player.id then + table.remove(maillist_players, _) + end + end + if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", maillist_players) then + return true + else + minetest.log("error","[mail] Save failed!") + return false + end + + --then remove the maillist itself + local maillists = mail.getMaillists() + for _, maillist in ipairs(maillists) do + if maillist.id then + table.remove(maillists, _) + end + end + if mail.write_json_file(mail.maildir .. "/mail.maillists.json", maillists) then + return true + else + minetest.log("error","[mail] Save failed!") + return false + end +end + function mail.pairsByKeys(t, f) -- http://www.lua.org/pil/19.3.html local a = {} @@ -284,15 +314,12 @@ end function mail.deleteContact(owner, name) local contacts = mail.getContacts() - local newContacts = {} for _, contact in ipairs(contacts) do if contact.owner == owner and contact.name == name then table.remove(contacts, _) - else - table.insert(newContacts, contact) end end - if mail.write_json_file(mail.maildir .. "/mail.contacts.json", newContacts) then + if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then return true else minetest.log("error","[mail] Save failed - messages may be lost!")