mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-06 06:30:34 -04:00
Add filters
This commit is contained in:
parent
618b338295
commit
1a1989a01d
6 changed files with 58 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
|||
minetest.register_chatcommand("mail",{
|
||||
description = "Open the mail interface",
|
||||
func = function(name)
|
||||
mail.show_inbox(name)
|
||||
mail.show_inbox(name, "3", "1", "")
|
||||
end
|
||||
})
|
||||
|
|
16
storage.lua
16
storage.lua
|
@ -98,6 +98,22 @@ function mail.sort_messages(unsorted_messages, sortfield, sortdirection)
|
|||
return sorted_messages
|
||||
end
|
||||
|
||||
function mail.filter_messages(unfiltered_messages, filter)
|
||||
if not filter or filter == "" then
|
||||
return unfiltered_messages
|
||||
end
|
||||
|
||||
local filtered_messages = {}
|
||||
|
||||
for _, msg in ipairs(unfiltered_messages) do
|
||||
if string.find(msg.from, filter) or string.find(msg.to, filter) or string.find(msg.subject, filter) then
|
||||
table.insert(filtered_messages, msg)
|
||||
end
|
||||
end
|
||||
|
||||
return filtered_messages
|
||||
end
|
||||
|
||||
-- marks a mail read by its id
|
||||
function mail.mark_read(playername, msg_id)
|
||||
local entry = mail.get_storage_entry(playername)
|
||||
|
|
|
@ -8,6 +8,22 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
-- split inbox and sent msgs for different tests
|
||||
local entry = mail.get_storage_entry(name)
|
||||
|
||||
local messagesInboxUnAnalyzed = entry.inbox
|
||||
local messagesOutBoxUnAnalyzed = entry.outbox
|
||||
local messagesDrafts = entry.drafts
|
||||
|
||||
-- filter inbox/outbox messages
|
||||
|
||||
local filter = fields.filter
|
||||
if not filter then
|
||||
filter = ""
|
||||
end
|
||||
|
||||
local messagesInboxFiltered = mail.filter_messages(messagesInboxUnAnalyzed, filter)
|
||||
local messagesOutboxFiltered = mail.filter_messages(messagesOutBoxUnAnalyzed, filter)
|
||||
|
||||
-- then sort them
|
||||
|
||||
local sortfield = fields.sortfield
|
||||
local sortdirection = fields.sortdirection
|
||||
if not sortfield or sortfield == "" or sortfield == "0" then
|
||||
|
@ -17,9 +33,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
sortdirection = 1
|
||||
end
|
||||
|
||||
local messagesInbox = mail.sort_messages(entry.inbox, tostring(sortfield), tostring(sortdirection))
|
||||
local messagesSent = mail.sort_messages(entry.outbox, tostring(sortfield), tostring(sortdirection))
|
||||
local messagesDrafts = entry.drafts
|
||||
local messagesInbox = mail.sort_messages(messagesInboxFiltered, tostring(sortfield), tostring(sortdirection), filter)
|
||||
local messagesSent = mail.sort_messages(messagesOutboxFiltered, tostring(sortfield), tostring(sortdirection), filter)
|
||||
|
||||
if fields.inbox then -- inbox table
|
||||
local evt = minetest.explode_table_event(fields.inbox)
|
||||
|
@ -154,8 +169,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
elseif fields.about then
|
||||
mail.show_about(name)
|
||||
|
||||
elseif fields.sortfield or fields.sortdirection then
|
||||
mail.show_mail_menu(name, fields.sortfield, fields.sortdirection)
|
||||
elseif fields.sortfield or fields.sortdirection or fields.filter then
|
||||
mail.show_mail_menu(name, sortfield, sortdirection, filter)
|
||||
end
|
||||
|
||||
return true
|
||||
|
|
12
ui/inbox.lua
12
ui/inbox.lua
|
@ -2,7 +2,7 @@
|
|||
local S = minetest.get_translator("mail")
|
||||
|
||||
|
||||
function mail.show_inbox(name, sortfield, sortdirection)
|
||||
function mail.show_inbox(name, sortfield, sortdirection, filter)
|
||||
if not sortfield or sortfield == "" or sortfield == "0" then
|
||||
sortfield = 3
|
||||
end
|
||||
|
@ -10,6 +10,10 @@ function mail.show_inbox(name, sortfield, sortdirection)
|
|||
sortdirection = 1
|
||||
end
|
||||
|
||||
if not filter then
|
||||
filter = ""
|
||||
end
|
||||
|
||||
local inbox_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
||||
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;1;false;false]
|
||||
|
||||
|
@ -27,13 +31,15 @@ function mail.show_inbox(name, sortfield, sortdirection)
|
|||
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
|
||||
|
||||
dropdown[0,9.4;2,0.5;sortfield;]] .. S("From") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. tostring(sortfield) .. [[;1]
|
||||
dropdown[2.2,9.4;2,0.5;sortdirection;]] .. S("Ascending") .. "," .. S("Descending") .. [[;]] .. tostring(sortdirection) .. [[;1]
|
||||
dropdown[2.0,9.4;2,0.5;sortdirection;]] .. S("Ascending") .. "," .. S("Descending") .. [[;]] .. tostring(sortdirection) .. [[;1]
|
||||
field[4.25,9.85;1.4,0.5;filter;]] .. S("Filter") .. [[:;]] .. filter .. [[]
|
||||
button[5.14,9.52;0.85,0.5;search;Q]
|
||||
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0.7;5.75,8.35;inbox;#999,]] .. S("From") .. "," .. S("Subject")
|
||||
local formspec = { inbox_formspec }
|
||||
local entry = mail.get_storage_entry(name)
|
||||
local messages = mail.sort_messages(entry.inbox, tostring(sortfield), tostring(sortdirection))
|
||||
local messages = mail.sort_messages(mail.filter_messages(entry.inbox, filter), tostring(sortfield), tostring(sortdirection))
|
||||
|
||||
mail.message_drafts[name] = nil
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
-- helper function for tabbed overview
|
||||
|
||||
function mail.show_mail_menu(playername, sortfield, sortdirection)
|
||||
function mail.show_mail_menu(playername, sortfield, sortdirection, filter)
|
||||
local index = mail.selected_idxs.boxtab[playername] or 1
|
||||
if index == 1 then
|
||||
mail.show_inbox(playername, sortfield, sortdirection)
|
||||
mail.show_inbox(playername, sortfield, sortdirection, filter)
|
||||
elseif index == 2 then
|
||||
mail.show_sent(playername, sortfield, sortdirection)
|
||||
mail.show_sent(playername, sortfield, sortdirection, filter)
|
||||
elseif index == 3 then
|
||||
mail.show_drafts(playername)
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
local S = minetest.get_translator("mail")
|
||||
|
||||
|
||||
function mail.show_sent(name, sortfield, sortdirection)
|
||||
function mail.show_sent(name, sortfield, sortdirection, filter)
|
||||
if not sortfield or sortfield == "" or sortfield == "0" then
|
||||
sortfield = 3
|
||||
end
|
||||
|
@ -10,6 +10,10 @@ function mail.show_sent(name, sortfield, sortdirection)
|
|||
sortdirection = 1
|
||||
end
|
||||
|
||||
if not filter then
|
||||
filter = ""
|
||||
end
|
||||
|
||||
local sent_formspec = "size[8.5,10;]" .. mail.theme .. [[
|
||||
tabheader[0.3,1;boxtab;]] .. S("Inbox") .. "," .. S("Sent messages").. "," .. S("Drafts") .. [[;2;false;false]
|
||||
|
||||
|
@ -25,13 +29,15 @@ function mail.show_sent(name, sortfield, sortdirection)
|
|||
button_exit[6,9.5;2.5,0.5;quit;]] .. S("Close") .. [[]
|
||||
|
||||
dropdown[0,9.4;2,0.5;sortfield;]] .. S("To") .. "," .. S("Subject") .. "," .. S("Date") .. [[;]] .. tostring(sortfield) .. [[;1]
|
||||
dropdown[2.2,9.4;2,0.5;sortdirection;]] .. S("Ascending") .. "," .. S("Descending") .. [[;]] .. tostring(sortdirection) .. [[;1]
|
||||
dropdown[2.0,9.4;2,0.5;sortdirection;]] .. S("Ascending") .. "," .. S("Descending") .. [[;]] .. tostring(sortdirection) .. [[;1]
|
||||
field[4.25,9.85;2,0.5;filter;]] .. S("Filter") .. [[:;]] .. filter .. [[]
|
||||
button[5.75,9.4;0.5,0.5;search;Q]
|
||||
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0.7;5.75,8.35;sent;#999,]] .. S("To") .. "," .. S("Subject")
|
||||
local formspec = { sent_formspec }
|
||||
local entry = mail.get_storage_entry(name)
|
||||
local messages = mail.sort_messages(entry.outbox, tostring(sortfield), tostring(sortdirection))
|
||||
local messages = mail.sort_messages(mail.filter_messages(entry.outbox, filter), tostring(sortfield), tostring(sortdirection))
|
||||
|
||||
mail.message_drafts[name] = nil
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue