mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-07 15:10:37 -04:00
contacts refactoring
This commit is contained in:
parent
b414ace620
commit
a690fc721d
6 changed files with 45 additions and 81 deletions
2
gui.lua
2
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
|
||||
|
|
99
storage.lua
99
storage.lua
|
@ -117,27 +117,44 @@ function mail.delete_mail(playername, msg_id)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
function mail.getContactsFile()
|
||||
return mail.maildir .. "/mail.contacts.json"
|
||||
-- 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
|
||||
if not existing_updated then
|
||||
-- insert
|
||||
table.insert(entry.contacts, contact)
|
||||
end
|
||||
mail.set_storage_entry(playername, entry)
|
||||
end
|
||||
|
||||
function mail.getContacts()
|
||||
local contacts = mail.read_json_file(mail.maildir .. "/mail.contacts.json")
|
||||
return contacts
|
||||
-- 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
|
||||
|
||||
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})
|
||||
end
|
||||
end
|
||||
return playerContacts
|
||||
-- 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 = {}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue