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

View file

@ -227,6 +227,36 @@ function mail.addPlayerToMaillist(player, ml_id, status)
end end
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) function mail.pairsByKeys(t, f)
-- http://www.lua.org/pil/19.3.html -- http://www.lua.org/pil/19.3.html
local a = {} local a = {}
@ -284,15 +314,12 @@ end
function mail.deleteContact(owner, name) function mail.deleteContact(owner, name)
local contacts = mail.getContacts() local contacts = mail.getContacts()
local newContacts = {}
for _, contact in ipairs(contacts) do for _, contact in ipairs(contacts) do
if contact.owner == owner and contact.name == name then if contact.owner == owner and contact.name == name then
table.remove(contacts, _) table.remove(contacts, _)
else
table.insert(newContacts, contact)
end end
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 return true
else else
minetest.log("error","[mail] Save failed - messages may be lost!") minetest.log("error","[mail] Save failed - messages may be lost!")