changeset 110:d2637760cd00 default tip

better notification
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 15 Sep 2025 13:29:10 -0600
parents b86bb25fb416
children
files src/active.js.luan src/add_post.js.luan src/lib/Notify.luan
diffstat 3 files changed, 31 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/active.js.luan	Sun Sep 14 15:58:52 2025 -0600
+++ b/src/active.js.luan	Mon Sep 15 13:29:10 2025 -0600
@@ -4,7 +4,7 @@
 local User = require "site:/lib/User.luan"
 local current_user = User.current or error()
 local Notify = require "site:/lib/Notify.luan"
-local remove_notify = Notify.remove or error()
+local notify_active = Notify.active or error()
 local Chat = require "site:/lib/Chat.luan"
 local get_chat_by_id = Chat.get_by_id or error()
 local Logging = require "luan:logging/Logging.luan"
@@ -21,7 +21,7 @@
 		logger.warn("no user")
 		return
 	end
-	remove_notify(user)
+	notify_active(user)
 	local chat = Http.request.parameters.chat
 	if chat ~= nil then
 		chat = get_chat_by_id(chat) or error()
--- a/src/add_post.js.luan	Sun Sep 14 15:58:52 2025 -0600
+++ b/src/add_post.js.luan	Mon Sep 15 13:29:10 2025 -0600
@@ -21,6 +21,7 @@
 local post_html = Shared.post_html or error()
 local http_push_to_users = Shared.http_push_to_users or error()
 local Notify = require "site:/lib/Notify.luan"
+local notify = Notify.notify or error()
 
 
 return function()
@@ -46,7 +47,7 @@
 		chat.updated = now
 		chat.save()
 	end )
-	Notify.add(chat)
+	notify(chat,post,user)
 	local html = `post_html(post)`
 	local js = "added("..json_string(html)..","..now..")"
 	chat.http_push(js)
--- 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