diff --git a/API.md b/API.md index c6a9832..770a0a3 100644 --- a/API.md +++ b/API.md @@ -57,3 +57,4 @@ echo.send_event_to(player_name, { * `echo.register_on_send_event(function(event, notification))`: Called when an event is sent * You can get the target player in `event.to`. * `echo.register_on_read_message(function(event, notification))`: Called when an notification is read +* `echo.register_on_delete_message(function(event, notification))`: Called when an notification is deleted diff --git a/src/api.lua b/src/api.lua index ed044ef..65a1ba5 100644 --- a/src/api.lua +++ b/src/api.lua @@ -50,6 +50,14 @@ function echo.register_on_read_message(func) echo.registered_on_read_message[#echo.registered_on_read_message + 1] = func end +echo.registered_on_delete_message = {} +function echo.register_on_delete_message(func) + assert(type(func) == "function", string.format( + "Invalid type fo argument func (function expected, got %s)", type(func))) + + echo.registered_on_delete_message[#echo.registered_on_delete_message + 1] = func +end + -- source: https://gist.github.com/jrus/3197011 local random = math.random local function new_uuid() diff --git a/src/gui.lua b/src/gui.lua index 1f2e250..6e65993 100644 --- a/src/gui.lua +++ b/src/gui.lua @@ -104,10 +104,14 @@ local function render_notif_row(event, notification) on_event = function(e_player) local e_name = e_player:get_player_name() local storage = echo.get_notifications_entry(e_name) + event = echo.get_event(event) for i, s_event in ipairs(storage) do if s_event == event then table.remove(storage, i) + for _, func in ipairs(echo.registered_on_delete_message) do + func(event, echo.event_to_notification(event)) + end echo.log_notifications_modification(e_name) return true end @@ -185,6 +189,9 @@ echo.echo_gui = flow.make_gui(function(player, ctx) for i=#e_storage, 1, -1 do local e_event = e_storage[i] + for _, func in ipairs(echo.registered_on_delete_message) do + func(e_event, echo.event_to_notification(e_event)) + end if os.date("%Y/%m/%d", e_event.time) == event_day then table.remove(e_storage, i) end diff --git a/src/on_send.lua b/src/on_send.lua index eb6c2d8..c42d648 100644 --- a/src/on_send.lua +++ b/src/on_send.lua @@ -64,10 +64,13 @@ echo.register_on_send_event(function(event, notification) end end) -echo.register_on_read_message(function(event) +local function simple_update(event) local name = event.to local player = core.get_player_by_name(name) if player and huds[name] then update_hud(player) end -end) +end + +echo.register_on_read_message(simple_update) +echo.register_on_delete_message(simple_update) diff --git a/src/storage.lua b/src/storage.lua index 7aaa778..17bf082 100644 --- a/src/storage.lua +++ b/src/storage.lua @@ -60,7 +60,10 @@ end ---@param name string The name of the player function echo.clear_notifications_entry(name) local storage = echo.get_notifications_entry(name) - for i in ipairs(storage) do + for i, event in ipairs(storage) do + for _, func in ipairs(echo.registered_on_delete_message) do + func(event, echo.event_to_notification(event)) + end storage[i] = nil end echo.log_notifications_modification(name)