changeset 4968:487f1eb829cf

mod_cloud_notify: Compat for prosody 0.12
author tmolitor <thilo@eightysoft.de>
date Sat, 02 Jul 2022 02:29:03 +0200
parents 1e8381f0d0a8
children 889e1695e935
files mod_cloud_notify/README.markdown mod_cloud_notify/mod_cloud_notify.lua
diffstat 2 files changed, 44 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_cloud_notify/README.markdown	Tue Jun 28 16:45:09 2022 +0100
+++ b/mod_cloud_notify/README.markdown	Sat Jul 02 02:29:03 2022 +0200
@@ -55,6 +55,7 @@
   `push_max_errors`                    `16`              How much persistent push errors are tolerated before notifications for the identifier in question are disabled
   `push_notification_important_body`   `New Message!`    The body text to use when the stanza is important (see above), no message body is sent if this is empty
   `push_max_devices`                   `5`               The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
+  `push_max_hibernation_timeout`       `6220800`         Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
 
 There are privacy implications for enabling these options because
 plaintext content and metadata will be shared with centralized servers
@@ -75,6 +76,7 @@
 
 ------ -----------------------------------------------------------------------------
   trunk  Works
+  0.12   Works
   0.11   Works
   0.10   Works
   0.9    Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua)
--- a/mod_cloud_notify/mod_cloud_notify.lua	Tue Jun 28 16:45:09 2022 +0100
+++ b/mod_cloud_notify/mod_cloud_notify.lua	Sat Jul 02 02:29:03 2022 +0200
@@ -11,6 +11,7 @@
 local hashes = require"util.hashes";
 local random = require"util.random";
 local cache = require"util.cache";
+local watchdog = require "util.watchdog";
 
 local xmlns_push = "urn:xmpp:push:0";
 
@@ -20,6 +21,7 @@
 local max_push_errors = module:get_option_number("push_max_errors", 16);
 local max_push_devices = module:get_option_number("push_max_devices", 5);
 local dummy_body = module:get_option_string("push_notification_important_body", "New Message!");
+local extended_hibernation_timeout = module:get_option_number("push_max_hibernation_timeout", 72*24*3600);		-- use same timeout like ejabberd
 
 local host_sessions = prosody.hosts[module.host].sessions;
 local push_errors = module:shared("push_errors");
@@ -98,6 +100,12 @@
 								session.push_identifier = nil;
 								session.push_settings = nil;
 								session.first_hibernated_push = nil;
+								-- check for prosody 0.12 mod_smacks
+								if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
+									-- restore old smacks watchdog
+									session.hibernating_watchdog:cancel();
+									session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
+								end
 							end
 						end
 					end
@@ -230,6 +238,12 @@
 				origin.push_identifier = nil;
 				origin.push_settings = nil;
 				origin.first_hibernated_push = nil;
+				-- check for prosody 0.12 mod_smacks
+				if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
+					-- restore old smacks watchdog
+					session.hibernating_watchdog:cancel();
+					session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
+				end
 			end
 			user_push_services[key] = nil;
 			push_errors[key] = nil;
@@ -446,6 +460,12 @@
 						-- timeout based on the value of session.first_hibernated_push
 						if not dummy_body or (dummy_body and is_important(stanza)) then
 							session.first_hibernated_push = os_time();
+							-- check for prosody 0.12 mod_smacks
+							if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
+								-- restore old smacks watchdog (--> the start of our original timeout will be delayed until first push)
+								session.hibernating_watchdog:cancel();
+								session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
+							end
 						end
 					end
 					session.log("debug", "Cloud handle_notify_request() > 0, not notifying for other %s queued stanzas of type %s", queue_type, stanza_type);
@@ -494,6 +514,20 @@
 	local session = event.origin;
 	local queue = event.queue;
 	session.first_hibernated_push = nil;
+	if session.hibernating_watchdog then		-- check for prosody 0.12 mod_smacks
+		-- save old watchdog callback and timeout
+		session.original_smacks_callback = session.hibernating_watchdog.callback;
+		session.original_smacks_timeout = session.hibernating_watchdog.timeout;
+		-- cancel old watchdog and create a new watchdog with extended timeout
+		session.hibernating_watchdog:cancel();
+		session.hibernating_watchdog = watchdog.new(extended_hibernation_timeout, function()
+			session.log("debug", "Push-extended smacks watchdog triggered");
+			if session.original_smacks_callback then
+				session.log("debug", "Calling original smacks watchdog handler");
+				session.original_smacks_callback();
+			end
+		end);
+	end
 	-- process unacked stanzas
 	process_stanza_queue(queue, session, "smacks");
 end
@@ -507,6 +541,7 @@
 			session.awaiting_push_timer = nil;
 		end
 		session.first_hibernated_push = nil;
+		-- the extended smacks watchdog will be canceled by the smacks module, no need to anything here
 	end
 end
 
@@ -594,8 +629,14 @@
 		for _, session in pairs(host_sessions[user].sessions) do
 			if session.awaiting_push_timer then session.awaiting_push_timer:stop(); end
 			session.awaiting_push_timer = nil;
+			session.push_queue = nil;
 			session.first_hibernated_push = nil;
-			session.push_queue = nil;
+			-- check for prosody 0.12 mod_smacks
+			if session.hibernating_watchdog and session.original_smacks_callback and session.original_smacks_timeout then
+				-- restore old smacks watchdog
+				session.hibernating_watchdog:cancel();
+				session.hibernating_watchdog = watchdog.new(session.original_smacks_timeout, session.original_smacks_callback);
+			end
 		end
 	end
 	module:log("info", "Module unloaded");