Implement non-player recipients (#131)

* Implement non-player recipients

* Add API callback specifically for players receiving mail

* Exclude sender from (mailing list) recipients

* Complement test

* Fixup typos in complemented test

* Expand aliases at toplevel if the current expansion is at toplevel

This should allow players to send mail to their own aliases

* Also test on_(player_)receive callbacks

* Fix oversight in test case
This commit is contained in:
y5nw 2024-02-01 20:46:26 +01:00 committed by GitHub
parent fcca0b7511
commit 570cf788ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 185 additions and 64 deletions

View file

@ -1,12 +1,56 @@
mail.register_recipient_handler(function(_, name)
if name:sub(1, 6) == "alias/" then
return true, name:sub(7)
elseif name == "list/test" then
return true, {"alias/player1", "alias/player2"}
elseif name == "list/reject" then
return false, "It works (?)"
end
end)
local received_count = {}
mail.register_on_player_receive(function(player)
received_count[player] = (received_count[player] or 0) + 1
end)
local sent_count = 0
mail.register_on_receive(function()
sent_count = sent_count+1
end)
local function assert_inbox_count(player_name, count)
local entry = mail.get_storage_entry(player_name)
assert(entry, player_name .. " has no mail entry")
local actual_count = #entry.inbox
assert(actual_count == count, ("incorrect mail count: %d expected, got %d"):format(count, actual_count))
local player_received = received_count[player_name] or 0
assert(player_received == count, ("incorrect receive count: %d expected, got %d"):format(count, player_received))
end
mtt.register("send mail", function(callback)
-- send a mail
local success, err = mail.send({from = "player1", to = "player2", subject = "something", body = "blah"})
-- send a mail to a list
local success, err = mail.send({from = "player1", to = "list/test", subject = "something", body = "blah"})
assert(success)
assert(not err)
assert_inbox_count("player2", 1)
assert_inbox_count("player1", 0)
assert(sent_count == 1)
-- send a second mail to the list and also the sender
success, err = mail.send({from = "player1", to = "list/test, alias/player1", subject = "something", body = "blah"})
assert(success)
assert(not err)
assert_inbox_count("player2", 2)
assert_inbox_count("player1", 1)
assert(sent_count == 2)
-- send a mail to list/reject - the mail should be rejected
success, err = mail.send({from = "player1", to = "list/reject", subject = "something", body = "NO"})
assert(not success)
assert(type(err) == "string")
assert_inbox_count("player2", 2)
assert_inbox_count("player1", 1)
assert(sent_count == 2)
-- check the receivers inbox
local entry = mail.get_storage_entry("player2")
assert(entry)
assert(#entry.inbox > 0)
callback()
end)