Mark read/unread/delete a message

This commit is contained in:
Athozus 2023-03-01 13:41:05 +01:00
parent 7ede5864be
commit 1441c28ccf
No known key found for this signature in database
GPG key ID: B50895022E8484BF
2 changed files with 22 additions and 16 deletions

20
gui.lua
View file

@ -104,7 +104,7 @@ function mail.show_inbox(name)
if messages[1] then if messages[1] then
for _, message in ipairs(messages) do for _, message in ipairs(messages) do
mail.ensure_new_format(message, name) mail.ensure_new_format(message, name)
if message.unread then if mail.getMessageStatus(name, message.id) == "unread" then
if not mail.player_in_list(name, message.to) then if not mail.player_in_list(name, message.to) then
formspec[#formspec + 1] = ",#FFD788" formspec[#formspec + 1] = ",#FFD788"
else else
@ -349,9 +349,10 @@ function mail.show_message(name, msgnumber)
local body = minetest.formspec_escape(message.body) or "" local body = minetest.formspec_escape(message.body) or ""
formspec = string.format(formspec, from, to, cc, date, subject, body) formspec = string.format(formspec, from, to, cc, date, subject, body)
if message.unread then message_status = mail.getMessageStatus(name, message.id)
message.unread = false
mail.setMessages(name, messages) if message_status == "unread" then
mail.setStatus(name, message.id, "read")
end end
minetest.show_formspec(name,"mail:message",formspec) minetest.show_formspec(name,"mail:message",formspec)
@ -470,8 +471,7 @@ function mail.handle_receivefields(player, formname, fields)
elseif fields.delete then elseif fields.delete then
if messages[selected_idxs.messages[name]] then if messages[selected_idxs.messages[name]] then
table.remove(messages, selected_idxs.messages[name]) mail.setStatus(name, messages[selected_idxs.messages[name]].id, "deleted")
mail.setMessages(name, messages)
end end
if boxtab_index == 1 then if boxtab_index == 1 then
@ -494,18 +494,14 @@ function mail.handle_receivefields(player, formname, fields)
elseif fields.markread then elseif fields.markread then
if messages[selected_idxs.messages[name]] then if messages[selected_idxs.messages[name]] then
messages[selected_idxs.messages[name]].unread = false mail.setStatus(name, messages[selected_idxs.messages[name]].id, "read")
-- set messages immediately, so it shows up already when updating the inbox
mail.setMessages(name, messages)
end end
mail.show_inbox(name) mail.show_inbox(name)
elseif fields.markunread then elseif fields.markunread then
if messages[selected_idxs.messages[name]] then if messages[selected_idxs.messages[name]] then
messages[selected_idxs.messages[name]].unread = true mail.setStatus(name, messages[selected_idxs.messages[name]].id, "unread")
-- set messages immediately, so it shows up already when updating the inbox
mail.setMessages(name, messages)
end end
mail.show_inbox(name) mail.show_inbox(name)

View file

@ -32,8 +32,10 @@ function mail.getPlayerMessages(playername)
for _, msg in ipairs(messages) do for _, msg in ipairs(messages) do
local receivers = mail.split((msg.to .. ", " .. (msg.cc or "") .. ", " .. (msg.bcc or "")),",") local receivers = mail.split((msg.to .. ", " .. (msg.cc or "") .. ", " .. (msg.bcc or "")),",")
for _, receiver in ipairs(receivers) do for _, receiver in ipairs(receivers) do
if receiver == playername then if receiver == playername then -- check if player is a receiver
table.insert(playerMessages, msg) if mail.getStatus(receiver, msg.id) ~= "deleted" then -- do not return if the message was deleted from player
table.insert(playerMessages, msg)
end
end end
end end
end end
@ -92,6 +94,15 @@ function mail.getStatus()
return messagesStatus return messagesStatus
end end
function mail.getMessageStatus(player, msg_id)
local messagesStatus = mail.getStatus()
for _, msg in ipairs(messagesStatus) do
if msg.id == msg_id and msg.player == player then
return msg.status
end
end
end
function mail.addStatus(player, msg_id, status) function mail.addStatus(player, msg_id, status)
local messagesStatus = mail.getStatus() local messagesStatus = mail.getStatus()
local msg_status = {id = msg_id, player = player, status = status} local msg_status = {id = msg_id, player = player, status = status}
@ -108,10 +119,9 @@ function mail.setStatus(player, msg_id, status)
local messagesStatus = mail.getStatus() local messagesStatus = mail.getStatus()
for _, msg_status in ipairs(messagesStatus) do for _, msg_status in ipairs(messagesStatus) do
if msg_status.id == msg_id and msg_status.player == player then if msg_status.id == msg_id and msg_status.player == player then
msg_status = {id = msg_id, player = player, status = status} messagesStatus[_] = {id = msg_id, player = player, status = status}
end end
end end
table.insert(messagesStatus, 1, status)
if mail.write_json_file(mail.maildir .. "/mail.status.json", messagesStatus) then if mail.write_json_file(mail.maildir .. "/mail.status.json", messagesStatus) then
return true return true
else else