Diff

util/pubsub.lua @ 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
parent 9195:b6ffd4f951b9
child 9201:1555ea0d6f61
line wrap: on
line diff
--- 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";