Comparison

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
comparison
equal deleted inserted replaced
9199:596d0d82a113 9200:249d90ff992e
462 self.events.fire_event("node-deleted", { node = node, actor = actor }); 462 self.events.fire_event("node-deleted", { node = node, actor = actor });
463 self.config.broadcaster("delete", node, node_obj.subscribers, nil, actor, node_obj, self); 463 self.config.broadcaster("delete", node, node_obj.subscribers, nil, actor, node_obj, self);
464 return true; 464 return true;
465 end 465 end
466 466
467 function service:publish(node, actor, id, item) 467 -- Used to check that the config of a node is as expected (i.e. 'publish-options')
468 local function check_preconditions(node_config, required_config)
469 if not (node_config and required_config) then
470 return false;
471 end
472 for config_field, value in pairs(required_config) do
473 if node_config[config_field] ~= value then
474 return false;
475 end
476 end
477 return true;
478 end
479
480 function service:publish(node, actor, id, item, required_config)
468 -- Access checking 481 -- Access checking
469 local may_publish = false; 482 local may_publish = false;
470 483
471 if self:may(node, actor, "publish") then 484 if self:may(node, actor, "publish") then
472 may_publish = true; 485 may_publish = true;
485 local node_obj = self.nodes[node]; 498 local node_obj = self.nodes[node];
486 if not node_obj then 499 if not node_obj then
487 if not self.config.autocreate_on_publish then 500 if not self.config.autocreate_on_publish then
488 return false, "item-not-found"; 501 return false, "item-not-found";
489 end 502 end
490 local ok, err = self:create(node, true); 503 local ok, err = self:create(node, true, required_config);
491 if not ok then 504 if not ok then
492 return ok, err; 505 return ok, err;
493 end 506 end
494 node_obj = self.nodes[node]; 507 node_obj = self.nodes[node];
508 elseif required_config and not check_preconditions(node_obj.config, required_config) then
509 return false, "precondition-not-met";
495 end 510 end
496 if not self.config.itemcheck(item) then 511 if not self.config.itemcheck(item) then
497 return nil, "internal-server-error"; 512 return nil, "internal-server-error";
498 end 513 end
499 local node_data = self.data[node]; 514 local node_data = self.data[node];