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)
|
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)
|
-- TODO: refactor this - not just compiles *a* list, but *the* list for the contacts screen (too inflexible)
|
||||||
local formspec = {}
|
local formspec = {}
|
||||||
local contacts = mail.getPlayerContacts(name)
|
local contacts = mail.get_contacts(name)
|
||||||
|
|
||||||
if playernames == nil then
|
if playernames == nil then
|
||||||
local length = 0
|
local length = 0
|
||||||
|
|
103
storage.lua
103
storage.lua
|
@ -117,27 +117,44 @@ function mail.delete_mail(playername, msg_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- add or update a contact
|
||||||
function mail.getContactsFile()
|
function mail.update_contact(playername, contact)
|
||||||
return mail.maildir .. "/mail.contacts.json"
|
local entry = mail.get_storage_entry(playername)
|
||||||
end
|
local existing_updated = false
|
||||||
|
for _, existing_contact in ipairs(entry.contacts) do
|
||||||
function mail.getContacts()
|
if existing_contact.name == contact.name then
|
||||||
local contacts = mail.read_json_file(mail.maildir .. "/mail.contacts.json")
|
-- update
|
||||||
return contacts
|
existing_contact.note = contact.note
|
||||||
end
|
existing_updated = true
|
||||||
|
|
||||||
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
|
||||||
end
|
end
|
||||||
return playerContacts
|
if not existing_updated then
|
||||||
|
-- insert
|
||||||
|
table.insert(entry.contacts, contact)
|
||||||
|
end
|
||||||
|
mail.set_storage_entry(playername, entry)
|
||||||
end
|
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()
|
function mail.getMaillists()
|
||||||
local maillists = mail.read_json_file(mail.maildir .. "/mail.maillists.json")
|
local maillists = mail.read_json_file(mail.maildir .. "/mail.maillists.json")
|
||||||
return maillists
|
return maillists
|
||||||
|
@ -340,58 +357,6 @@ function mail.pairsByKeys(t, f)
|
||||||
return iter
|
return iter
|
||||||
end
|
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)
|
function mail.read_json_file(path)
|
||||||
local file = io.open(path, "r")
|
local file = io.open(path, "r")
|
||||||
local content = {}
|
local content = {}
|
||||||
|
|
|
@ -46,7 +46,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- add new contacts if some receivers aren't registered
|
-- 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 recipients = mail.parse_player_list(fields.to)
|
||||||
local isNew = true
|
local isNew = true
|
||||||
for _,recipient in ipairs(recipients) do
|
for _,recipient in ipairs(recipients) do
|
||||||
|
@ -61,7 +61,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if isNew then
|
if isNew then
|
||||||
mail.addContact(name, {name = recipient, note = ""})
|
mail.update_contact(name, {name = recipient, note = ""})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local contacts = mail.getPlayerContacts(name)
|
local contacts = mail.get_contacts(name)
|
||||||
|
|
||||||
if fields.contacts then
|
if fields.contacts then
|
||||||
local evt = minetest.explode_table_event(fields.contacts)
|
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
|
mail.selected_idxs.contacts[name] = k
|
||||||
break
|
break
|
||||||
elseif k == mail.selected_idxs.contacts[name] then
|
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
|
mail.selected_idxs.contacts[name] = nil
|
||||||
found = true
|
found = true
|
||||||
else
|
else
|
||||||
|
|
|
@ -34,7 +34,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local contacts = mail.getPlayerContacts(name)
|
local contacts = mail.get_contacts(name)
|
||||||
|
|
||||||
if fields.save then
|
if fields.save then
|
||||||
if mail.selected_idxs.contacts[name] and mail.selected_idxs.contacts[name] ~= "#NEW#" 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
|
return true
|
||||||
|
|
||||||
else
|
else
|
||||||
mail.setContact(name, contact)
|
mail.update_contact(name, contact)
|
||||||
contacts[mail.selected_idxs.contacts[name]] = nil
|
contacts[mail.selected_idxs.contacts[name]] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
contact.name = fields.name
|
contact.name = fields.name
|
||||||
contact.note = fields.note
|
contact.note = fields.note
|
||||||
mail.setContact(name, contact)
|
mail.update_contact(name, contact)
|
||||||
|
|
||||||
else
|
else
|
||||||
local contact = {
|
mail.update_contact(name, {
|
||||||
name = fields.name,
|
name = fields.name,
|
||||||
note = fields.note,
|
note = fields.note,
|
||||||
}
|
})
|
||||||
mail.addContact(name, contact)
|
|
||||||
end
|
end
|
||||||
mail.show_contacts(name)
|
mail.show_contacts(name)
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local contacts = mail.getPlayerContacts(name)
|
local contacts = mail.get_contacts(name)
|
||||||
local draft = mail.message_drafts[name]
|
local draft = mail.message_drafts[name]
|
||||||
|
|
||||||
-- get indexes for fields with selected rows
|
-- get indexes for fields with selected rows
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue