Add deleting contact

This commit is contained in:
Athozus 2023-03-01 16:07:01 +01:00
parent 178a101618
commit fd9b579c5f
No known key found for this signature in database
GPG key ID: B50895022E8484BF
2 changed files with 19 additions and 3 deletions

View file

@ -739,7 +739,7 @@ function mail.handle_receivefields(player, formname, fields)
selected_idxs.contacts[name] = k selected_idxs.contacts[name] = k
break break
elseif k == selected_idxs.contacts[name] then elseif k == selected_idxs.contacts[name] then
contacts[selected_idxs.contacts[name]] = nil mail.deleteContact(name, contacts[selected_idxs.contacts[name]].name)
selected_idxs.contacts[name] = nil selected_idxs.contacts[name] = nil
found = true found = true
else else
@ -750,8 +750,6 @@ function mail.handle_receivefields(player, formname, fields)
-- was the last in the list, so take the previous (new last) -- was the last in the list, so take the previous (new last)
selected_idxs.contacts[name] = last selected_idxs.contacts[name] = last
end end
mail.setContacts(name, contacts)
end end
mail.show_contacts(name) mail.show_contacts(name)

View file

@ -226,6 +226,24 @@ function mail.setContact(playername, updated_contact)
end end
end 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
return true
else
minetest.log("error","[mail] Save failed - messages may be lost!")
return false
end
end
function mail.read_json_file(path) function mail.read_json_file(path)
local file = io.open(path, "r") local file = io.open(path, "r")
local content = {} local content = {}