Comparison

plugins/mod_pubsub/pubsub.lib.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 9199:596d0d82a113
child 9206:33ee40dc3e25
comparison
equal deleted inserted replaced
9199:596d0d82a113 9200:249d90ff992e
216 end 216 end
217 end 217 end
218 return ret; 218 return ret;
219 end 219 end
220 220
221 -- Used to check that the config of a node is as expected (i.e. 'publish-options')
222 local function check_preconditions(node_config, required_config)
223 if not (node_config and required_config) then
224 return false;
225 end
226 for config_field, value in pairs(required_config) do
227 if node_config[config_field] ~= value then
228 return false;
229 end
230 end
231 return true;
232 end
233
234 local service_method_feature_map = { 221 local service_method_feature_map = {
235 add_subscription = { "subscribe", "subscription-options" }; 222 add_subscription = { "subscribe", "subscription-options" };
236 create = { "create-nodes", "instant-nodes", "item-ids", "create-and-configure" }; 223 create = { "create-nodes", "instant-nodes", "item-ids", "create-and-configure" };
237 delete = { "delete-nodes" }; 224 delete = { "delete-nodes" };
238 get_items = { "retrieve-items" }; 225 get_items = { "retrieve-items" };
601 local node = publish.attr.node; 588 local node = publish.attr.node;
602 if not node then 589 if not node then
603 origin.send(pubsub_error_reply(stanza, "nodeid-required")); 590 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
604 return true; 591 return true;
605 end 592 end
593 local required_config = nil;
606 local publish_options = stanza.tags[1]:get_child("publish-options"); 594 local publish_options = stanza.tags[1]:get_child("publish-options");
607 if publish_options then 595 if publish_options then
608 -- Ensure that the node configuration matches the values in publish-options 596 -- Ensure that the node configuration matches the values in publish-options
609 local publish_options_form = publish_options:get_child("x", "jabber:x:data"); 597 local publish_options_form = publish_options:get_child("x", "jabber:x:data");
610 local required_config = config_from_xep0060(node_config_form:data(publish_options_form), true); 598 required_config = config_from_xep0060(node_config_form:data(publish_options_form), true);
611 local node_accessible, node_config = service:get_node_config(node, stanza.attr.from);
612 if node_accessible == false and service.config.autocreate_on_publish then
613 module:log("debug", "creating node %s with publish-options", node)
614 -- we need to create the node here so that it is configured
615 -- correctly
616 local created, err = service:create(node, stanza.attr.from, required_config)
617 if not created then
618 local reply = pubsub_error_reply(stanza, err);
619 origin.send(reply);
620 return true;
621 end
622 elseif not check_preconditions(node_config, required_config) then
623 local reply = pubsub_error_reply(stanza, "precondition-not-met");
624 origin.send(reply);
625 return true;
626 end
627 end 599 end
628 local item = publish:get_child("item"); 600 local item = publish:get_child("item");
629 local id = (item and item.attr.id); 601 local id = (item and item.attr.id);
630 if not id then 602 if not id then
631 id = uuid_generate(); 603 id = uuid_generate();
632 if item then 604 if item then
633 item.attr.id = id; 605 item.attr.id = id;
634 end 606 end
635 end 607 end
636 local ok, ret = service:publish(node, stanza.attr.from, id, item); 608 local ok, ret = service:publish(node, stanza.attr.from, id, item, required_config);
637 local reply; 609 local reply;
638 if ok then 610 if ok then
639 if type(ok) == "string" then 611 if type(ok) == "string" then
640 id = ok; 612 id = ok;
641 end 613 end