Diff

mod_cloud_notify/mod_cloud_notify.lua @ 2791:008cf272b7ea

mod_cloud_notify: Fix regression in error handling The last commit made push error handling not working at all.
author tmolitor <thilo@eightysoft.de>
date Wed, 11 Oct 2017 03:10:40 +0200
parent 2751:6b710a8bdf03
child 2792:7622ec165cdd
line wrap: on
line diff
--- a/mod_cloud_notify/mod_cloud_notify.lua	Tue Oct 10 00:50:40 2017 +0200
+++ b/mod_cloud_notify/mod_cloud_notify.lua	Wed Oct 11 03:10:40 2017 +0200
@@ -20,6 +20,7 @@
 
 local host_sessions = prosody.hosts[module.host].sessions;
 local push_errors = {};
+local id2node = {};
 
 -- For keeping state across reloads while caching reads
 local push_store = (function()
@@ -62,10 +63,11 @@
 function handle_push_error(event)
 	local stanza = event.stanza;
 	local error_type, condition = stanza:get_error();
-	local node = jid.split(stanza.attr.to);
+	local node = id2node[stanza.attr.id];
+	if node == nil then return false; end		-- unknown stanza? Ignore for now!
 	local from = stanza.attr.from;
 	local user_push_services = push_store:get(node);
-
+	
 	for push_identifier, _ in pairs(user_push_services) do
 		local stanza_id = hashes.sha256(push_identifier, true);
 		if stanza_id == stanza.attr.id then
@@ -90,8 +92,9 @@
 					push_errors[push_identifier] = nil;
 					-- unhook iq handlers for this identifier (if possible)
 					if module.unhook then
-						module:unhook("iq-error/bare/"..stanza_id, handle_push_error);
-						module:unhook("iq-result/bare/"..stanza_id, handle_push_success);
+						module:unhook("iq-error/host/"..stanza_id, handle_push_error);
+						module:unhook("iq-result/host/"..stanza_id, handle_push_success);
+						id2node[stanza_id] = nil;
 					end
 				end
 			elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then
@@ -105,10 +108,11 @@
 
 function handle_push_success(event)
 	local stanza = event.stanza;
-	local node = jid.split(stanza.attr.to);
+	local node = id2node[stanza.attr.id];
+	if node == nil then return false; end		-- unknown stanza? Ignore for now!
 	local from = stanza.attr.from;
 	local user_push_services = push_store:get(node);
-
+	
 	for push_identifier, _ in pairs(user_push_services) do
 		if hashes.sha256(push_identifier, true) == stanza.attr.id then
 			if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and push_errors[push_identifier] > 0 then
@@ -188,8 +192,9 @@
 			user_push_services[key] = nil;
 			push_errors[key] = nil;
 			if module.unhook then
-				module:unhook("iq-error/bare/"..key, handle_push_error);
-				module:unhook("iq-result/bare/"..key, handle_push_success);
+				module:unhook("iq-error/host/"..key, handle_push_error);
+				module:unhook("iq-result/host/"..key, handle_push_success);
+				id2node[key] = nil;
 			end
 		end
 	end
@@ -226,6 +231,7 @@
 	{ name = "pending-subscription-count"; type = "text-single"; };
 	{ name = "last-message-sender"; type = "jid-single"; };
 	{ name = "last-message-body"; type = "text-single"; };
+	{ name = "last-message-priority"; type = "text-single"; };
 };
 
 -- http://xmpp.org/extensions/xep-0357.html#publishing
@@ -264,6 +270,13 @@
 			if stanza and include_body then
 				form_data["last-message-body"] = stanza:get_child_text("body");
 			end
+			if stanza then
+				if stanza:get_child("body") or stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") then
+					form_data["last-message-priority"] = "high";
+				else
+					form_data["last-message-priority"] = "low";
+				end
+			end
 			push_publish:add_child(push_form:form(form_data));
 			if stanza and push_info.include_payload == "stripped" then
 				push_publish:tag("payload", { type = "stripped" })
@@ -287,8 +300,9 @@
 			-- handle push errors for this node
 			if push_errors[push_identifier] == nil then
 				push_errors[push_identifier] = 0;
-				module:hook("iq-error/bare/"..stanza_id, handle_push_error);
-				module:hook("iq-result/bare/"..stanza_id, handle_push_success);
+				module:hook("iq-error/host/"..stanza_id, handle_push_error);
+				module:hook("iq-result/host/"..stanza_id, handle_push_success);
+				id2node[stanza_id] = node;
 			end
 			module:send(push_publish);
 			pushes = pushes + 1;
@@ -434,8 +448,9 @@
 
 		for push_identifier, _ in pairs(push_errors) do
 			local stanza_id = hashes.sha256(push_identifier, true);
-			module:unhook("iq-error/bare/"..stanza_id, handle_push_error);
-			module:unhook("iq-result/bare/"..stanza_id, handle_push_success);
+			module:unhook("iq-error/host/"..stanza_id, handle_push_error);
+			module:unhook("iq-result/host/"..stanza_id, handle_push_success);
+			id2node[stanza_id] = nil;
 		end
 	end