Diff

mod_push2/mod_push2.lua @ 6216:2f2539ce8f3b

mod_push2: implement grace period
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Mon, 24 Mar 2025 22:18:47 -0500
parent 6215:e53f0967520c
child 6217:8ecd53452af8
line wrap: on
line diff
--- a/mod_push2/mod_push2.lua	Mon Mar 24 13:01:05 2025 -0500
+++ b/mod_push2/mod_push2.lua	Mon Mar 24 22:18:47 2025 -0500
@@ -44,6 +44,9 @@
 			match.chats[chatel.attr.jid] = chat
 		end
 
+		match.grace = matchel:get_child_text("grace")
+		if match.grace then match.grace = tonumber(match.grace) end
+
 		local send = matchel:get_child("send", "urn:xmpp:push2:send:notify-only:0")
 		if send then
 			match.send = send.attr.xmlns
@@ -367,13 +370,24 @@
 					does_match = stanza:get_child("stana-id", "urn:xmpp:sid:0") and has_body(stanza)
 				end
 
+				local to_user, to_host = jid.split(stanza.attr.to)
+				to_user = to_user or session.username
+				to_host = to_host or module.host
+
+				-- If another session has recent activity within configured grace period, don't send push
+				if does_match and match.grace and to_host == module.host and host_sessions[to_user] then
+					local now = os_time()
+					for _, session in pairs(host_sessions[to_user].sessions) do
+						if session.last_activity and session.push_registration_id ~= push_registration_id and (now - session.last_activity) < match.grace then
+							does_match = false
+						end
+					end
+				end
+
 				local chat = match.chats and (match.chats[stanza.attr.from] or match.chats[jid.bare(stanza.attr.from)] or match.chats[jid.host(stanza.attr.from)])
 				if does_match and chat then
 					does_match = false
 
-					local to_user, to_host = jid.split(stanza.attr.to)
-					to_user = to_user or session.username
-					to_host = to_host or module.host
 					local nick = session.rooms_joined[jid.bare(stanza.attr.from)] or to_user
 
 					if not does_match and chat.mention then
@@ -612,6 +626,11 @@
 
 		handle_notify_request(stanza, to_user, notify_push_services, event.origin, true);
 	end
+
+	-- This is a message the user has sent, indicates activity on a session
+	if event.for_user == jid.node(stanza.attr.from) and module.host == jid.host(stanza.attr.from) then
+		event.origin.last_activity = os_time()
+	end
 end
 
 module:hook("smacks-hibernation-start", hibernate_session);