From 716c68f983333e3890a4deb0c5151af1b9d77b0e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 18 Aug 2018 14:06:56 +0100 Subject: pubsub.lib, util.pubsub: Move precondition checks to util.pubsub --- util/pubsub.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'util/pubsub.lua') diff --git a/util/pubsub.lua b/util/pubsub.lua index f9ae0998..628fdc59 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -464,7 +464,20 @@ function service:delete(node, actor) 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 @@ function service:publish(node, actor, id, item) 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"; -- cgit v1.2.3