diff mod_pubsub_subscription/mod_pubsub_subscription.lua @ 6110:9db1529c06c2

Merge upstream
author tmolitor <thilo@eightysoft.de>
date Sun, 05 Jan 2025 17:50:02 +0100
parents 6dca425eea15
children 1c0e9486ba23
line wrap: on
line diff
--- a/mod_pubsub_subscription/mod_pubsub_subscription.lua	Wed Nov 20 05:07:11 2024 +0100
+++ b/mod_pubsub_subscription/mod_pubsub_subscription.lua	Sun Jan 05 17:50:02 2025 +0100
@@ -1,3 +1,4 @@
+local id = require "util.id";
 local st = require "util.stanza";
 local uuid = require "util.uuid";
 local mt = require "util.multitable";
@@ -37,7 +38,7 @@
 	end
 
 	item._id = uuid.generate();
-	local iq_id = uuid.generate();
+	local iq_id = "pubsub-sub-"..id.short();
 	pending_subscription:set(iq_id, item._id);
 	active_subscriptions:set(item.service, item.node, item.from, item._id, item);
 
@@ -51,12 +52,14 @@
 for _, event_name in ipairs(valid_events) do
 	module:hook("pubsub-event/host/"..event_name, function (event)
 		for _, _, _, _, _, cb in active_subscriptions:iter(event.service, event.node, event.stanza.attr.to, nil, "on_"..event_name) do
+			event.handled = true;
 			pcall(cb, event);
 		end
 	end);
 
 	module:hook("pubsub-event/bare/"..event_name, function (event)
 		for _, _, _, _, _, cb in active_subscriptions:iter(event.service, event.node, event.stanza.attr.to, nil, "on_"..event_name) do
+			event.handled = true;
 			pcall(cb, event);
 		end
 	end);
@@ -67,6 +70,7 @@
 	local service = stanza.attr.from;
 
 	if not stanza.attr.id then return end -- shouldn't be possible
+	if not stanza.attr.id:match("^pubsub%-sub%-") then return end
 
 	local subscribed_node = pending_subscription:get(stanza.attr.id);
 	pending_subscription:set(stanza.attr.id, nil);
@@ -118,7 +122,7 @@
 	local node_subs = active_subscriptions:get(item.service, item.node, item.from);
 	if node_subs and next(node_subs) then return end
 
-	local iq_id = uuid.generate();
+	local iq_id = "pubsub-sub-"..id.short();
 	pending_unsubscription:set(iq_id, item._id);
 
 	module:send(st.iq({ type = "set", id = iq_id, from = item.from, to = item.service })
@@ -130,24 +134,33 @@
 
 function handle_message(context, event)
 	local origin, stanza = event.origin, event.stanza;
-	local ret = nil;
+	local handled = nil;
 	local service = stanza.attr.from;
 	module:log("debug", "Got message/%s: %s", context, stanza:top_tag());
 	for event_container in stanza:childtags("event", xmlns_pubsub_event) do
 		for pubsub_event in event_container:childtags() do
 			module:log("debug", "Got pubsub event %s", pubsub_event:top_tag());
 			local node = pubsub_event.attr.node;
-			module:fire_event("pubsub-event/" .. context .. "/"..pubsub_event.name, {
-					stanza = stanza;
-					origin = origin;
-					event = pubsub_event;
-					service = service;
-					node = node;
-				});
-			ret = true;
+			local event_data = {
+				stanza = stanza;
+				origin = origin;
+				event = pubsub_event;
+				service = service;
+				node = node;
+				handled = false;
+			};
+			module:fire_event("pubsub-event/" .. context .. "/"..pubsub_event.name, event_data);
+			if not handled and event_data.handled then
+				handled = true;
+			end
 		end
 	end
-	return ret;
+	-- If not addressed to the host, let it fall through to normal handling
+	-- (it may be on its way to a local client), otherwise, we'll mark the
+	-- event as handled to suppress an error response if we handled it.
+	if context == "host" and handled then
+		return true;
+	end
 end
 
 module:hook("message/host", function(event)