Rework on editing/deletion of contacts/maillists

This commit is contained in:
Athozus 2023-03-01 18:21:33 +01:00
parent 770b7b69cb
commit 6453c56def
No known key found for this signature in database
GPG key ID: B50895022E8484BF
2 changed files with 71 additions and 21 deletions

51
gui.lua
View file

@ -215,11 +215,11 @@ function mail.show_maillists(name)
end
if selected_idxs.maillists[name] then
formspec[#formspec + 1] = ";"
formspec[#formspec + 1] = tostring(selected_idxs.maillists[name] + 1)
formspec[#formspec + 1] = selected_idxs.maillists[name]
end
formspec[#formspec + 1] = "]"
else
formspec[#formspec + 1] = "]label[2.25,4.5;No mail]"
formspec[#formspec + 1] = "]label[2.25,4.5;No maillist]"
end
minetest.show_formspec(name, "mail:maillists", table.concat(formspec, ""))
end
@ -376,6 +376,7 @@ function mail.compile_contact_list(name, selected, playernames)
formspec[#formspec + 1] = "]"
end
return table.concat(formspec, "")
end
function mail.show_message(name, msgnumber)
@ -866,45 +867,65 @@ function mail.handle_receivefields(player, formname, fields)
elseif formname == "mail:maillists" then
local name = player:get_player_name()
local maillists = mail.getPlayerMaillists(name)
if fields.new then
if fields.maillists then
local evt = minetest.explode_table_event(fields.maillists)
for k, _, i in mail.pairsByKeys(maillists) do
if i == evt.row - 1 then
selected_idxs.maillists[name] = k
break
end
end
if evt.type == "DCL" and maillists[selected_idxs.maillists[name]] then
mail.show_edit_maillist(
name,
maillists[selected_idxs.maillists[name]].name,
maillists[selected_idxs.maillists[name]].desc,
""
)
end
elseif fields.new then
selected_idxs.maillists[name] = "#NEW#"
mail.show_edit_maillist(name, "", "", "")
elseif fields.edit and selected_idxs.maillists[name] and maillists[selected_idxs.maillists[name]] then
mail.show_edit_contact(
mail.show_edit_maillist(
name,
maillists[selected_idxs.maillists[name]].name,
maillists[selected_idxs.maillists[name]].desc
maillists[selected_idxs.maillists[name]].desc,
""
)
elseif fields.delete then
if contacts[selected_idxs.contacts[name]] then
-- delete the contact and set the selected to the next in the list,
if maillists[selected_idxs.maillists[name]] then
-- delete the maillist and set the selected to the next in the list,
-- except if it was the last. Then determine the new last
local found = false
local last = nil
for k in mail.pairsByKeys(contacts) do
for k in mail.pairsByKeys(maillists) do
if found then
selected_idxs.contacts[name] = k
selected_idxs.maillists[name] = k
break
elseif k == selected_idxs.contacts[name] then
mail.deleteContact(name, contacts[selected_idxs.contacts[name]].name)
selected_idxs.contacts[name] = nil
elseif k == selected_idxs.maillists[name] then
mail.deleteMaillist(name, maillists[selected_idxs.maillists[name]].id)
selected_idxs.maillists[name] = nil
found = true
else
last = k
end
end
if found and not selected_idxs.contacts[name] then
if found and not selected_idxs.maillists[name] then
-- was the last in the list, so take the previous (new last)
selected_idxs.contacts[name] = last
selected_idxs.maillists[name] = last
end
end
mail.show_maillists(name)
elseif fields.back then
if boxtab_index == 1 then
mail.show_inbox(name)
elseif boxtab_index == 2 then
@ -912,6 +933,8 @@ function mail.handle_receivefields(player, formname, fields)
end
end
return true
elseif formname == "mail:editmaillist" then
local name = player:get_player_name()
local maillists = mail.getPlayerMaillists(name)

View file

@ -227,6 +227,36 @@ function mail.addPlayerToMaillist(player, ml_id, status)
end
end
function mail.deleteMaillist(ml_id)
-- remove players attached to the maillist
local maillists_players = mail.getPlayersInMaillists()
for _, player in ipairs(maillist_players) do
if player.id then
table.remove(maillist_players, _)
end
end
if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", maillist_players) then
return true
else
minetest.log("error","[mail] Save failed!")
return false
end
--then remove the maillist itself
local maillists = mail.getMaillists()
for _, maillist in ipairs(maillists) do
if maillist.id then
table.remove(maillists, _)
end
end
if mail.write_json_file(mail.maildir .. "/mail.maillists.json", maillists) then
return true
else
minetest.log("error","[mail] Save failed!")
return false
end
end
function mail.pairsByKeys(t, f)
-- http://www.lua.org/pil/19.3.html
local a = {}
@ -284,15 +314,12 @@ 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
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!")