mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Mark read/unread/delete a message
This commit is contained in:
parent
7ede5864be
commit
1441c28ccf
2 changed files with 22 additions and 16 deletions
20
gui.lua
20
gui.lua
|
@ -104,7 +104,7 @@ function mail.show_inbox(name)
|
|||
if messages[1] then
|
||||
for _, message in ipairs(messages) do
|
||||
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
|
||||
formspec[#formspec + 1] = ",#FFD788"
|
||||
else
|
||||
|
@ -349,9 +349,10 @@ function mail.show_message(name, msgnumber)
|
|||
local body = minetest.formspec_escape(message.body) or ""
|
||||
formspec = string.format(formspec, from, to, cc, date, subject, body)
|
||||
|
||||
if message.unread then
|
||||
message.unread = false
|
||||
mail.setMessages(name, messages)
|
||||
message_status = mail.getMessageStatus(name, message.id)
|
||||
|
||||
if message_status == "unread" then
|
||||
mail.setStatus(name, message.id, "read")
|
||||
end
|
||||
|
||||
minetest.show_formspec(name,"mail:message",formspec)
|
||||
|
@ -470,8 +471,7 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
elseif fields.delete then
|
||||
if messages[selected_idxs.messages[name]] then
|
||||
table.remove(messages, selected_idxs.messages[name])
|
||||
mail.setMessages(name, messages)
|
||||
mail.setStatus(name, messages[selected_idxs.messages[name]].id, "deleted")
|
||||
end
|
||||
|
||||
if boxtab_index == 1 then
|
||||
|
@ -494,18 +494,14 @@ function mail.handle_receivefields(player, formname, fields)
|
|||
|
||||
elseif fields.markread then
|
||||
if messages[selected_idxs.messages[name]] then
|
||||
messages[selected_idxs.messages[name]].unread = false
|
||||
-- set messages immediately, so it shows up already when updating the inbox
|
||||
mail.setMessages(name, messages)
|
||||
mail.setStatus(name, messages[selected_idxs.messages[name]].id, "read")
|
||||
end
|
||||
|
||||
mail.show_inbox(name)
|
||||
|
||||
elseif fields.markunread then
|
||||
if messages[selected_idxs.messages[name]] then
|
||||
messages[selected_idxs.messages[name]].unread = true
|
||||
-- set messages immediately, so it shows up already when updating the inbox
|
||||
mail.setMessages(name, messages)
|
||||
mail.setStatus(name, messages[selected_idxs.messages[name]].id, "unread")
|
||||
end
|
||||
|
||||
mail.show_inbox(name)
|
||||
|
|
18
storage.lua
18
storage.lua
|
@ -32,8 +32,10 @@ function mail.getPlayerMessages(playername)
|
|||
for _, msg in ipairs(messages) do
|
||||
local receivers = mail.split((msg.to .. ", " .. (msg.cc or "") .. ", " .. (msg.bcc or "")),",")
|
||||
for _, receiver in ipairs(receivers) do
|
||||
if receiver == playername then
|
||||
table.insert(playerMessages, msg)
|
||||
if receiver == playername then -- check if player is a receiver
|
||||
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
|
||||
|
@ -92,6 +94,15 @@ function mail.getStatus()
|
|||
return messagesStatus
|
||||
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)
|
||||
local messagesStatus = mail.getStatus()
|
||||
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()
|
||||
for _, msg_status in ipairs(messagesStatus) do
|
||||
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
|
||||
table.insert(messagesStatus, 1, status)
|
||||
if mail.write_json_file(mail.maildir .. "/mail.status.json", messagesStatus) then
|
||||
return true
|
||||
else
|
||||
|
|
Loading…
Add table
Reference in a new issue