mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Use global contacts functions and reconfigure add/remove functions
This commit is contained in:
parent
b5285fbff1
commit
addc9eb9bc
2 changed files with 63 additions and 29 deletions
41
gui.lua
41
gui.lua
|
@ -236,7 +236,7 @@ end
|
|||
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.getContacts(name)
|
||||
local contacts = mail.getPlayerContacts(name)
|
||||
|
||||
if playernames == nil then
|
||||
local length = 0
|
||||
|
@ -535,8 +535,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
elseif fields.delete then
|
||||
if messages[selected_idxs.messages[name]] then
|
||||
table.remove(messages,selected_idxs.messages[name])
|
||||
mail.setMessages(name, messages)
|
||||
mail.setStatus(name, messages[selected_idxs.messages[name]].id, "deleted")
|
||||
end
|
||||
|
||||
if boxtab_index == 1 then
|
||||
|
@ -564,20 +563,20 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
return
|
||||
end
|
||||
|
||||
local contacts = mail.getContacts(name)
|
||||
-- add new contacts if some receivers aren't registered
|
||||
local contacts = mail.getPlayerContacts(name)
|
||||
local recipients = mail.parse_player_list(fields.to)
|
||||
local changed = false
|
||||
for _,v in pairs(recipients) do
|
||||
if contacts[string.lower(v)] == nil then
|
||||
contacts[string.lower(v)] = {
|
||||
name = v,
|
||||
note = "",
|
||||
}
|
||||
changed = true
|
||||
local isNew = true
|
||||
for r_,recipient in ipairs(recipients) do
|
||||
for c_,contact in ipairs(contacts) do
|
||||
if contact.name == recipient then
|
||||
isNew = false
|
||||
break
|
||||
end
|
||||
end
|
||||
if isNew then
|
||||
mail.addContact(name, {name = recipient, note = ""})
|
||||
end
|
||||
end
|
||||
if changed then
|
||||
mail.setContacts(name, contacts)
|
||||
end
|
||||
|
||||
minetest.after(0.5, function()
|
||||
|
@ -612,7 +611,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
elseif formname == "mail:selectcontact" then
|
||||
local name = player:get_player_name()
|
||||
local contacts = mail.getContacts(name)
|
||||
local contacts = mail.getPlayerContacts(name)
|
||||
local draft = message_drafts[name]
|
||||
|
||||
-- get indexes for fields with selected rows
|
||||
|
@ -682,7 +681,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
elseif formname == "mail:contacts" then
|
||||
local name = player:get_player_name()
|
||||
local contacts = mail.getContacts(name)
|
||||
local contacts = mail.getPlayerContacts(name)
|
||||
|
||||
if fields.contacts then
|
||||
local evt = minetest.explode_table_event(fields.contacts)
|
||||
|
@ -752,7 +751,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
elseif formname == "mail:editcontact" then
|
||||
local name = player:get_player_name()
|
||||
local contacts = mail.getContacts(name)
|
||||
local contacts = mail.getPlayerContacts(name)
|
||||
|
||||
if fields.save then
|
||||
if selected_idxs.contacts[name] and selected_idxs.contacts[name] ~= "#NEW#" then
|
||||
|
@ -768,7 +767,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
return true
|
||||
|
||||
else
|
||||
contacts[string.lower(fields.name)] = contact
|
||||
mail.setContact(name, contact)
|
||||
contacts[selected_idxs.contacts[name]] = nil
|
||||
end
|
||||
end
|
||||
|
@ -780,10 +779,8 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
name = fields.name,
|
||||
note = fields.note,
|
||||
}
|
||||
contacts[string.lower(contact.name)] = contact
|
||||
mail.addContact(name, contact)
|
||||
end
|
||||
|
||||
mail.setContacts(name, contacts)
|
||||
mail.show_contacts(name)
|
||||
|
||||
elseif fields.back then
|
||||
|
|
51
storage.lua
51
storage.lua
|
@ -3,11 +3,6 @@ function mail.getMailFile(playername)
|
|||
return mail.maildir .. "/" .. saneplayername .. ".json"
|
||||
end
|
||||
|
||||
function mail.getContactsFile(playername)
|
||||
local saneplayername = string.gsub(playername, "[.|/]", "")
|
||||
return mail.maildir .. "/contacts/" .. saneplayername .. ".json"
|
||||
end
|
||||
|
||||
function mail.getMessages()
|
||||
local messages = mail.read_json_file(mail.maildir .. "/mail.messages.json")
|
||||
if messages then
|
||||
|
@ -156,8 +151,24 @@ function mail.setStatus(player, msg_id, status)
|
|||
end
|
||||
end
|
||||
|
||||
function mail.getContacts(playername)
|
||||
return mail.read_json_file(mail.getContactsFile(playername))
|
||||
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})
|
||||
end
|
||||
end
|
||||
return playerContacts
|
||||
end
|
||||
|
||||
function mail.pairsByKeys(t, f)
|
||||
|
@ -188,6 +199,32 @@ function mail.setContacts(playername, contacts)
|
|||
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 - messages 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 - messages may be lost!")
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function mail.read_json_file(path)
|
||||
local file = io.open(path, "r")
|
||||
|
|
Loading…
Add table
Reference in a new issue