Skip to content

Commit

Permalink
Add message id in player selections storage (fix #110, fix #114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Athozus committed Oct 16, 2023
1 parent 8d7c8c6 commit 80ae9c3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mail = {
outbox = {},
drafts = {},
trash = {},
message = {},
contacts = {},
maillists = {},
to = {},
Expand Down
13 changes: 5 additions & 8 deletions ui/compose.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
local S = minetest.get_translator("mail")

local FORMNAME = "mail:compose"
local msg_id = {}

function mail.show_compose(name, to, subject, body, cc, bcc, id)
local formspec = [[
Expand All @@ -28,9 +27,7 @@ function mail.show_compose(name, to, subject, body, cc, bcc, id)
minetest.formspec_escape(body) or "")

if id then
msg_id[name] = id
else
msg_id[name] = nil
mail.selected_idxs.message[name] = id
end

minetest.show_formspec(name, FORMNAME, formspec)
Expand All @@ -44,8 +41,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if fields.send then
local id = mail.new_uuid()
if msg_id[name] then
id = msg_id[name]
if mail.selected_idxs.message[name] then
id = mail.selected_idxs.message[name]
end
if (fields.to == "" and fields.cc == "" and fields.bcc == "") or fields.body == "" then
-- if mail is invalid then store it as a draft
Expand Down Expand Up @@ -117,8 +114,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)

elseif fields.draft then
local id = mail.new_uuid()
if msg_id[name] then
id = msg_id[name]
if mail.selected_idxs.message[name] then
id = mail.selected_idxs.message[name]
end
mail.save_draft({
id = id,
Expand Down
15 changes: 12 additions & 3 deletions ui/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
if selected_id == 0 then
table.insert(mail.selected_idxs.inbox[name], inbox.id)
mail.selected_idxs.message[name] = inbox.id
end
else
mail.selected_idxs.inbox[name] = { inbox.id }
mail.selected_idxs.message[name] = inbox.id
end
if evt.type == "DCL" then
mail.selected_idxs.message[name] = inbox.id
mail.show_message(name, inbox.id)
else
mail.show_mail_menu(name)
Expand Down Expand Up @@ -132,11 +135,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
if selected_id == 0 then
table.insert(mail.selected_idxs.outbox[name], outbox.id)
mail.selected_idxs.message[name] = outbox.id
end
else
mail.selected_idxs.outbox[name] = { outbox.id }
mail.selected_idxs.message[name] = outbox.id
end
if evt.type == "DCL" then
mail.selected_idxs.message[name] = outbox.id
mail.show_message(name, outbox.id)
else
mail.show_mail_menu(name)
Expand All @@ -156,6 +162,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
mail.selected_idxs.drafts[name] = evt.row - 1
if evt.type == "DCL" and messagesDrafts[mail.selected_idxs.drafts[name]] then
mail.selected_idxs.message[name] = messagesDrafts[mail.selected_idxs.drafts[name]].id
mail.show_compose(name,
messagesDrafts[mail.selected_idxs.drafts[name]].to,
messagesDrafts[mail.selected_idxs.drafts[name]].subject,
Expand All @@ -180,6 +187,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
mail.selected_idxs.trash[name] = evt.row - 1
if evt.type == "DCL" and messagesTrash[mail.selected_idxs.trash[name]] then
mail.selected_idxs.message[name] = messagesTrash[mail.selected_idxs.trash[name]].id
mail.show_message(name, messagesTrash[mail.selected_idxs.trash[name]].id)
end
return true
Expand All @@ -203,12 +211,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)

elseif fields.read then
if formname == "mail:inbox" and nonempty(mail.selected_idxs.inbox[name]) then -- inbox table
mail.show_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
mail.selected_idxs.message[name] = mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]]
elseif formname == "mail:outbox" and nonempty(mail.selected_idxs.outbox[name]) then -- outbox table
mail.show_message(name, mail.selected_idxs.outbox[name][#mail.selected_idxs.outbox[name]])
mail.selected_idxs.message[name] = mail.selected_idxs.outbox[name][#mail.selected_idxs.outbox[name]]
elseif formname == "mail:trash" and messagesTrash[mail.selected_idxs.trash[name]] then
mail.show_message(name, messagesTrash[mail.selected_idxs.trash[name]].id)
mail.selected_idxs.message[name] = messagesTrash[mail.selected_idxs.trash[name]].id
end
mail.show_message(name, mail.selected_idxs.message[name])

elseif fields.edit then
if formname == "mail:drafts" and messagesDrafts[mail.selected_idxs.drafts[name]] then
Expand Down
13 changes: 3 additions & 10 deletions ui/message.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local FORMNAME = "mail:message"

function mail.show_message(name, id)
local message = mail.get_message(name, id)
mail.selected_idxs.message[name] = id

local formspec = [[
size[8,9]
Expand Down Expand Up @@ -111,16 +112,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end

local name = player:get_player_name()
local entry = mail.get_storage_entry(name)

local message = ""
if mail.selected_idxs.inbox[name] and mail.selected_idxs.boxtab[name] == 1 then
message = mail.get_message(name, mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]])
elseif mail.selected_idxs.outbox[name] and mail.selected_idxs.boxtab[name] == 2 then
message = mail.get_message(name, mail.selected_idxs.outbox[name][#mail.selected_idxs.outbox[name]])
elseif mail.selected_idxs.trash[name] and mail.selected_idxs.boxtab[name] == 4 then
message = mail.get_message(name, entry.trash[mail.selected_idxs.trash[name]].id)
end

local message = mail.get_message(name, mail.selected_idxs.message[name])

if fields.back then
mail.show_mail_menu(name)
Expand Down
12 changes: 2 additions & 10 deletions ui/receivers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end

local name = player:get_player_name()
local entry = mail.get_storage_entry(name)

local message_id = ""
if mail.selected_idxs.inbox[name] and mail.selected_idxs.boxtab[name] == 1 then
message_id = mail.selected_idxs.inbox[name][#mail.selected_idxs.inbox[name]]
elseif mail.selected_idxs.outbox[name] and mail.selected_idxs.boxtab[name] == 2 then
message_id = mail.selected_idxs.outbox[name][#mail.selected_idxs.outbox[name]]
elseif mail.selected_idxs.trash[name] and mail.selected_idxs.boxtab[name] == 4 then
message_id = entry.trash[mail.selected_idxs.trash[name]].id
end

local message_id = mail.selected_idxs.message[name]

if fields.back then
mail.show_message(name, message_id)
Expand Down

0 comments on commit 80ae9c3

Please sign in to comment.