diff src/lib/Notify.luan @ 110:d2637760cd00

better notification
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 15 Sep 2025 13:29:10 -0600
parents 46418395c860
children
line wrap: on
line diff
--- a/src/lib/Notify.luan	Sun Sep 14 15:58:52 2025 -0600
+++ b/src/lib/Notify.luan	Mon Sep 15 13:29:10 2025 -0600
@@ -38,7 +38,7 @@
 	local users = {}
 	local fns = {}
 
-	function fns.add(user_ids,mute_ids)
+	function fns.notify(user_ids,mute_ids,user_name,post_content,chat_id)
 		local now = time_now()
 		for _, user_id in ipairs(user_ids) do
 			local user = get_user_by_id(user_id)
@@ -47,40 +47,36 @@
 				and not mute_ids[user_id] \
 				and (user.multi_notify or not user.was_notified) \
 			then
-				users[user_id] = now
-				-- logger.info("add "..user_id)
-			end
-		end
-	end
-
-	function fns.remove(user_id)
-		users[user_id] = nil
-		-- logger.info("remove "..user_id)
-	end
-
-	function fns.notify()
-		-- logger.info("notify")
-		local now = time_now()
-		for user_id, when in pairs(shallow_copy(users)) do
-			if now - when > wait then
-				local user = get_user_by_id(user_id)
-				local name = user.name
-				local you = name and name..", you" or "You"
-				-- logger.info("notify "..user.notify_email.." "..user_id)
 				send_mail {
 					To = user.notify_email
-					Subject = "New Messages"
+					Subject = "Message from "..user_name
 					body = `%>
-<%=you%> have received new messages.
+<%= post_content %>
 
-<%= base_url %>/
+<%= base_url %>/?chat=<%= chat_id %>
 
 To unsubscribe:
 <%= base_url %>/unsubscribe.html?user=<%=user_id%>&password=<%=user.password%>
 <%					`
 				}
+				if not user.multi_notify then
+					set_notified(user,true)
+				end
+			end
+		end
+	end
+
+	function fns.active(user_id)
+		users[user_id] = time_now()
+		-- logger.info("active "..user_id)
+	end
+
+	function fns.remove()
+		-- logger.info("remove")
+		local now = time_now()
+		for user_id, when in pairs(shallow_copy(users)) do
+			if now - when > wait then
 				users[user_id] = nil
-				set_notified(user,true)
 			end
 		end
 	end
@@ -90,21 +86,22 @@
 
 local glob = Thread.global_callable("notify",init)
 
-function Notify.add(chat)
+function Notify.notify(chat,post,user)
 	Thread.run(function()
-		glob.add(chat.user_ids,chat.mute_ids)
+		local user_name = user.name or user.email
+		glob.notify(chat.user_ids,chat.mute_ids,user_name,post.content,chat.id)
 	end)
 end
 
-function Notify.remove(user)
+function Notify.active(user)
 	Thread.run(function()
-		glob.remove(user.id)
+		glob.active(user.id)
 		if user.was_notified then
 			set_notified(user,false)
 		end
 	end)
 end
 
-Thread.schedule( glob.notify, { repeating_delay=Time.period{seconds=10} } )
+Thread.schedule( glob.remove, { repeating_delay=Time.period{seconds=10} } )
 
 return Notify