Changeset

9200:249d90ff992e

pubsub.lib, util.pubsub: Move precondition checks to util.pubsub
author Matthew Wild <mwild1@gmail.com>
date Sat, 18 Aug 2018 14:06:56 +0100
parents 9199:596d0d82a113
children 9201:1555ea0d6f61
files plugins/mod_pubsub/pubsub.lib.lua util/pubsub.lua
diffstat 2 files changed, 20 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_pubsub/pubsub.lib.lua	Sat Aug 18 14:38:49 2018 +0200
+++ b/plugins/mod_pubsub/pubsub.lib.lua	Sat Aug 18 14:06:56 2018 +0100
@@ -218,19 +218,6 @@
 	return ret;
 end
 
--- Used to check that the config of a node is as expected (i.e. 'publish-options')
-local function check_preconditions(node_config, required_config)
-	if not (node_config and required_config) then
-		return false;
-	end
-	for config_field, value in pairs(required_config) do
-		if node_config[config_field] ~= value then
-			return false;
-		end
-	end
-	return true;
-end
-
 local service_method_feature_map = {
 	add_subscription = { "subscribe", "subscription-options" };
 	create = { "create-nodes", "instant-nodes", "item-ids", "create-and-configure" };
@@ -603,27 +590,12 @@
 		origin.send(pubsub_error_reply(stanza, "nodeid-required"));
 		return true;
 	end
+	local required_config = nil;
 	local publish_options = stanza.tags[1]:get_child("publish-options");
 	if publish_options then
 		-- Ensure that the node configuration matches the values in publish-options
 		local publish_options_form = publish_options:get_child("x", "jabber:x:data");
-		local required_config = config_from_xep0060(node_config_form:data(publish_options_form), true);
-		local node_accessible, node_config = service:get_node_config(node, stanza.attr.from);
-		if node_accessible == false and service.config.autocreate_on_publish then
-			module:log("debug", "creating node %s with publish-options", node)
-			-- we need to create the node here so that it is configured
-			-- correctly
-			local created, err = service:create(node, stanza.attr.from, required_config)
-			if not created then
-				local reply = pubsub_error_reply(stanza, err);
-				origin.send(reply);
-				return true;
-			end
-		elseif not check_preconditions(node_config, required_config) then
-			local reply = pubsub_error_reply(stanza, "precondition-not-met");
-			origin.send(reply);
-			return true;
-		end
+		required_config = config_from_xep0060(node_config_form:data(publish_options_form), true);
 	end
 	local item = publish:get_child("item");
 	local id = (item and item.attr.id);
@@ -633,7 +605,7 @@
 			item.attr.id = id;
 		end
 	end
-	local ok, ret = service:publish(node, stanza.attr.from, id, item);
+	local ok, ret = service:publish(node, stanza.attr.from, id, item, required_config);
 	local reply;
 	if ok then
 		if type(ok) == "string" then
--- a/util/pubsub.lua	Sat Aug 18 14:38:49 2018 +0200
+++ b/util/pubsub.lua	Sat Aug 18 14:06:56 2018 +0100
@@ -464,7 +464,20 @@
 	return true;
 end
 
-function service:publish(node, actor, id, item)
+-- Used to check that the config of a node is as expected (i.e. 'publish-options')
+local function check_preconditions(node_config, required_config)
+	if not (node_config and required_config) then
+		return false;
+	end
+	for config_field, value in pairs(required_config) do
+		if node_config[config_field] ~= value then
+			return false;
+		end
+	end
+	return true;
+end
+
+function service:publish(node, actor, id, item, required_config)
 	-- Access checking
 	local may_publish = false;
 
@@ -487,11 +500,13 @@
 		if not self.config.autocreate_on_publish then
 			return false, "item-not-found";
 		end
-		local ok, err = self:create(node, true);
+		local ok, err = self:create(node, true, required_config);
 		if not ok then
 			return ok, err;
 		end
 		node_obj = self.nodes[node];
+	elseif required_config and not check_preconditions(node_obj.config, required_config) then
+		return false, "precondition-not-met";
 	end
 	if not self.config.itemcheck(item) then
 		return nil, "internal-server-error";