From a690fc721dc4226ed6e035c83e0f802f2cbf0224 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 28 Mar 2023 14:36:20 +0200 Subject: [PATCH] contacts refactoring --- gui.lua | 2 +- storage.lua | 103 ++++++++++++++---------------------------- ui/compose.lua | 4 +- ui/contacts.lua | 4 +- ui/edit_contact.lua | 11 ++--- ui/select_contact.lua | 2 +- 6 files changed, 45 insertions(+), 81 deletions(-) diff --git a/gui.lua b/gui.lua index 27d26dd..ae8fa03 100644 --- a/gui.lua +++ b/gui.lua @@ -2,7 +2,7 @@ function mail.compile_contact_list(name, selected, playernames) -- TODO: refactor this - not just compiles *a* list, but *the* list for the contacts screen (too inflexible) local formspec = {} - local contacts = mail.getPlayerContacts(name) + local contacts = mail.get_contacts(name) if playernames == nil then local length = 0 diff --git a/storage.lua b/storage.lua index 7e9f195..57a6dbd 100644 --- a/storage.lua +++ b/storage.lua @@ -117,27 +117,44 @@ function mail.delete_mail(playername, msg_id) end end - -function mail.getContactsFile() - return mail.maildir .. "/mail.contacts.json" -end - -function mail.getContacts() - local contacts = mail.read_json_file(mail.maildir .. "/mail.contacts.json") - return contacts -end - -function mail.getPlayerContacts(playername) - local contacts = mail.getContacts() - local playerContacts = {} - for _, contact in ipairs(contacts) do - if contact.owner == playername then - table.insert(playerContacts, {name = contact.name, note = contact.note}) +-- add or update a contact +function mail.update_contact(playername, contact) + local entry = mail.get_storage_entry(playername) + local existing_updated = false + for _, existing_contact in ipairs(entry.contacts) do + if existing_contact.name == contact.name then + -- update + existing_contact.note = contact.note + existing_updated = true end end - return playerContacts + if not existing_updated then + -- insert + table.insert(entry.contacts, contact) + end + mail.set_storage_entry(playername, entry) end +-- deletes a contact +function mail.delete_contact(playername, contactname) + local entry = mail.get_storage_entry(playername) + for i, existing_contact in ipairs(entry.contacts) do + if existing_contact.name == contactname then + -- delete + table.remove(entry.contacts, i) + mail.set_storage_entry(playername, entry) + return + end + end +end + +-- get all contacts +function mail.get_contacts(playername) + local entry = mail.get_storage_entry(playername) + return entry.contact +end + + function mail.getMaillists() local maillists = mail.read_json_file(mail.maildir .. "/mail.maillists.json") return maillists @@ -340,58 +357,6 @@ function mail.pairsByKeys(t, f) return iter end -function mail.setContacts(playername, contacts) - if mail.write_json_file(mail.getContactsFile(playername), contacts) then - return true - else - minetest.log("error","[mail] Save failed - contacts may be lost! ("..playername..")") - return false - end -end - -function mail.addContact(playername, contact) - local contacts = mail.getContacts() - local newContact = {owner = playername, name = contact.name, note = contact.note} - table.insert(contacts, 1, newContact) - if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then - return true - else - minetest.log("error","[mail] Save failed - contact may be lost!") - return false - end -end - -function mail.setContact(playername, updated_contact) - local contacts = mail.getContacts() - for _, contact in ipairs(contacts) do - if contact.owner == playername and contact.name == updated_contact.name then - contacts[_] = {owner = playername, name = updated_contact.name, note = updated_contact.note} - end - end - if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then - return true - else - minetest.log("error","[mail] Save failed - contact may be lost!") - return false - end -end - -function mail.deleteContact(owner, name) - local contacts = mail.getContacts() - for i=#contacts,1,-1 do - local contact = contacts[i] - if contact.owner == owner and contact.name == name then - table.remove(contacts, i) - end - end - if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then - return true - else - minetest.log("error","[mail] Save failed - contact may be lost!") - return false - end -end - function mail.read_json_file(path) local file = io.open(path, "r") local content = {} diff --git a/ui/compose.lua b/ui/compose.lua index 9d1a9a0..172ca40 100644 --- a/ui/compose.lua +++ b/ui/compose.lua @@ -46,7 +46,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end -- add new contacts if some receivers aren't registered - local contacts = mail.getPlayerContacts(name) + local contacts = mail.get_contacts(name) local recipients = mail.parse_player_list(fields.to) local isNew = true for _,recipient in ipairs(recipients) do @@ -61,7 +61,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end end if isNew then - mail.addContact(name, {name = recipient, note = ""}) + mail.update_contact(name, {name = recipient, note = ""}) end end diff --git a/ui/contacts.lua b/ui/contacts.lua index 5e2780f..1ea642a 100644 --- a/ui/contacts.lua +++ b/ui/contacts.lua @@ -20,7 +20,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end local name = player:get_player_name() - local contacts = mail.getPlayerContacts(name) + local contacts = mail.get_contacts(name) if fields.contacts then local evt = minetest.explode_table_event(fields.contacts) @@ -60,7 +60,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) mail.selected_idxs.contacts[name] = k break elseif k == mail.selected_idxs.contacts[name] then - mail.deleteContact(name, contacts[mail.selected_idxs.contacts[name]].name) + mail.delete_contact(name, contacts[mail.selected_idxs.contacts[name]].name) mail.selected_idxs.contacts[name] = nil found = true else diff --git a/ui/edit_contact.lua b/ui/edit_contact.lua index 0cef891..15aa50a 100644 --- a/ui/edit_contact.lua +++ b/ui/edit_contact.lua @@ -34,7 +34,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end local name = player:get_player_name() - local contacts = mail.getPlayerContacts(name) + local contacts = mail.get_contacts(name) if fields.save then if mail.selected_idxs.contacts[name] and mail.selected_idxs.contacts[name] ~= "#NEW#" then @@ -50,20 +50,19 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return true else - mail.setContact(name, contact) + mail.update_contact(name, contact) contacts[mail.selected_idxs.contacts[name]] = nil end end contact.name = fields.name contact.note = fields.note - mail.setContact(name, contact) + mail.update_contact(name, contact) else - local contact = { + mail.update_contact(name, { name = fields.name, note = fields.note, - } - mail.addContact(name, contact) + }) end mail.show_contacts(name) diff --git a/ui/select_contact.lua b/ui/select_contact.lua index e6c0564..74494a5 100644 --- a/ui/select_contact.lua +++ b/ui/select_contact.lua @@ -45,7 +45,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end local name = player:get_player_name() - local contacts = mail.getPlayerContacts(name) + local contacts = mail.get_contacts(name) local draft = mail.message_drafts[name] -- get indexes for fields with selected rows