Add drafts

This commit is contained in:
Athozus 2023-03-31 17:04:49 +02:00
parent eae547b2f7
commit a8fc125db3
No known key found for this signature in database
GPG key ID: B50895022E8484BF
9 changed files with 174 additions and 13 deletions

View file

@ -1,6 +1,7 @@
local FORMNAME = "mail:compose"
local msg_id = nil
function mail.show_compose(name, to, subject, body, cc, bcc)
function mail.show_compose(name, to, subject, body, cc, bcc, id)
local formspec = [[
size[8,9]
button[0,0;1,1;tocontacts;]] .. S("To") .. [[:]
@ -11,16 +12,21 @@ function mail.show_compose(name, to, subject, body, cc, bcc)
field[5.1,1.05;3.1,1;bcc;;%s]
field[0.25,2;8,1;subject;]] .. S("Subject") .. [[:;%s]
textarea[0.25,2.5;8,6;body;;%s]
button[0.5,8.5;3,1;cancel;]] .. S("Cancel") .. [[]
button[4.5,8.5;3,1;send;]] .. S("Send") .. [[]
button[0.1,8.5;2.5,1;cancel;]] .. S("Cancel") .. [[]
button[2.7,8.5;2.5,1;draft;]] .. S("Save draft") .. [[]
button[5.3,8.5;2.5,1;send;]] .. S("Send") .. [[]
]] .. mail.theme
formspec = string.format(formspec,
minetest.formspec_escape(to or ""),
minetest.formspec_escape(cc or ""),
minetest.formspec_escape(bcc or ""),
minetest.formspec_escape(subject or ""),
minetest.formspec_escape(body or ""))
minetest.formspec_escape(to) or "",
minetest.formspec_escape(cc) or "",
minetest.formspec_escape(bcc) or "",
minetest.formspec_escape(subject) or "",
minetest.formspec_escape(body) or "")
if id then
msg_id = id
end
minetest.show_formspec(name, FORMNAME, formspec)
end
@ -32,7 +38,12 @@ 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 then
id = msg_id
end
local success, err = mail.send({
id = id,
from = name,
to = fields.to,
cc = fields.cc,
@ -82,6 +93,23 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
elseif fields.cancel then
mail.message_drafts[name] = nil
mail.show_mail_menu(name)
elseif fields.draft then
local id = mail.new_uuid()
if msg_id then
id = msg_id
end
mail.save_draft({
id = id,
from = name,
to = fields.to,
cc = fields.cc,
bcc = fields.bcc,
subject = fields.subject,
body = fields.body
})
mail.show_mail_menu(name)
end

49
ui/drafts.lua Normal file
View file

@ -0,0 +1,49 @@
local drafts_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;3;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;edit;]] .. S("Edit") .. [[]
button[6,1.70;2.5,0.5;delete;]] .. S("Delete") .. [[]
button[6,6.8;2.5,0.5;contacts;]] .. S("Contacts") .. [[]
button[6,7.6;2.5,0.5;maillists;]] .. S("Mail lists") .. [[]
button[6,8.7;2.5,0.5;about;]] .. S("About") .. [[]
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
tablecolumns[color;text;text]
table[0,0.7;5.75,9.35;drafts;#999,]] .. S("To") .. "," .. S("Subject")
function mail.show_drafts(name)
local formspec = { drafts_formspec }
local entry = mail.get_storage_entry(name)
local messages = entry.drafts
mail.message_drafts[name] = nil
if messages[1] then
for _, message in ipairs(messages) do
formspec[#formspec + 1] = ","
formspec[#formspec + 1] = ","
formspec[#formspec + 1] = minetest.formspec_escape(message.to)
formspec[#formspec + 1] = ","
if message.subject ~= "" then
if string.len(message.subject) > 30 then
formspec[#formspec + 1] = minetest.formspec_escape(string.sub(message.subject, 1, 27))
formspec[#formspec + 1] = "..."
else
formspec[#formspec + 1] = minetest.formspec_escape(message.subject)
end
else
formspec[#formspec + 1] = S("(No subject)")
end
end
if mail.selected_idxs.sent[name] then
formspec[#formspec + 1] = ";"
formspec[#formspec + 1] = tostring(mail.selected_idxs.sent[name] + 1)
end
formspec[#formspec + 1] = "]"
else
formspec[#formspec + 1] = "]label[2.25,4.5;" .. S("No drafts") .. "]"
end
minetest.show_formspec(name, "mail:drafts", table.concat(formspec, ""))
end

View file

@ -1,5 +1,5 @@
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "mail:inbox" and formname ~= "mail:sent" then
if formname ~= "mail:inbox" and formname ~= "mail:sent" and formname ~= "mail:drafts" then
return
end
@ -10,6 +10,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local messagesInbox = entry.inbox
local messagesSent = entry.outbox
local messagesDrafts = entry.drafts
if fields.inbox then -- inbox table
local evt = minetest.explode_table_event(fields.inbox)
@ -29,6 +30,22 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return true
end
if fields.drafts then -- drafts table
local evt = minetest.explode_table_event(fields.drafts)
mail.selected_idxs.drafts[name] = evt.row - 1
if evt.type == "DCL" and messagesDrafts[mail.selected_idxs.drafts[name]] then
mail.show_compose(name,
messagesDrafts[mail.selected_idxs.drafts[name]].to,
messagesDrafts[mail.selected_idxs.drafts[name]].subject,
messagesDrafts[mail.selected_idxs.drafts[name]].body,
messagesDrafts[mail.selected_idxs.drafts[name]].cc,
messagesDrafts[mail.selected_idxs.drafts[name]].bcc,
messagesDrafts[mail.selected_idxs.drafts[name]].id
)
end
return true
end
if fields.boxtab == "1" then
mail.selected_idxs.boxtab[name] = 1
mail.show_inbox(name)
@ -37,6 +54,10 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
mail.selected_idxs.boxtab[name] = 2
mail.show_sent(name)
elseif fields.boxtab == "3" then
mail.selected_idxs.boxtab[name] = 3
mail.show_drafts(name)
elseif fields.read then
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then -- inbox table
mail.show_message(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
@ -49,6 +70,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
mail.delete_mail(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then -- sent table
mail.delete_mail(name, messagesSent[mail.selected_idxs.sent[name]].id)
elseif formname == "mail:drafts" and messagesDrafts[mail.selected_idxs.drafts[name]] then -- drafts table
mail.delete_mail(name, messagesDrafts[mail.selected_idxs.drafts[name]].id)
end
mail.show_mail_menu(name)

View file

@ -1,5 +1,5 @@
local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages") .. [[;1;false;false]
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;1;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;read;]] .. S("Read") .. [[]

View file

@ -6,5 +6,7 @@ function mail.show_mail_menu(playername)
mail.show_inbox(playername)
elseif index == 2 then
mail.show_sent(playername)
elseif index == 3 then
mail.show_drafts(playername)
end
end
end

View file

@ -1,5 +1,5 @@
local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages") .. [[;2;false;false]
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;2;false;false]
button[6,0.10;2.5,0.5;new;]] .. S("New") .. [[]
button[6,0.95;2.5,0.5;read;]] .. S("Read") .. [[]