From 56e86cd5e1456e9d3b9f11cff8cd959fa509c250 Mon Sep 17 00:00:00 2001
From: Athozus <athozus@gmail.com>
Date: Sat, 1 Apr 2023 19:14:53 +0200
Subject: [PATCH] Fix message notifications (#46)

* Fix mail notification on login

* Fix HUD notification
---
 api.lua    | 3 +++
 hud.lua    | 2 +-
 onjoin.lua | 3 ++-
 3 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/api.lua b/api.lua
index c7ceb79..544e164 100644
--- a/api.lua
+++ b/api.lua
@@ -99,6 +99,9 @@ function mail.send(m)
 		local name = player:get_player_name()
 		if recipients[name] then
 			minetest.chat_send_player(name, mail_alert)
+			local receiver_entry = mail.get_storage_entry(name)
+			local receiver_messages = receiver_entry.inbox
+			mail.hud_update(name, receiver_messages)
 		end
 	end
 
diff --git a/hud.lua b/hud.lua
index 462bad1..592be35 100644
--- a/hud.lua
+++ b/hud.lua
@@ -43,7 +43,7 @@ function mail.hud_update(playername, messages)
 
 	local unreadcount = 0
 	for _, message in ipairs(messages) do
-		if message.unread then
+		if not message.read then
 			unreadcount = unreadcount + 1
 		end
 	end
diff --git a/onjoin.lua b/onjoin.lua
index 0bf36d2..a93ea83 100644
--- a/onjoin.lua
+++ b/onjoin.lua
@@ -2,11 +2,12 @@ minetest.register_on_joinplayer(function(player)
 	minetest.after(2, function(name)
 		local entry = mail.get_storage_entry(name)
 		local messages = entry.inbox
+		mail.hud_update(name, messages)
 
 		local unreadcount = 0
 
 		for _, message in pairs(messages) do
-			if message.unread then
+			if not message.read then
 				unreadcount = unreadcount + 1
 			end
 		end