Comparison

util/pubsub.lua @ 6437:3f1f11dfdf10

util.pubsub: Add support for node configuration
author Kim Alvefur <zash@zash.se>
date Sun, 28 Sep 2014 01:45:59 +0200
parent 6436:31ca87ea8d46
child 6439:d58ad8bd244b
comparison
equal deleted inserted replaced
6436:31ca87ea8d46 6437:3f1f11dfdf10
9 local default_config = { __index = { 9 local default_config = { __index = {
10 broadcaster = function () end; 10 broadcaster = function () end;
11 get_affiliation = function () end; 11 get_affiliation = function () end;
12 capabilities = {}; 12 capabilities = {};
13 } }; 13 } };
14 local default_node_config = { __index = {
15 } };
14 16
15 function new(config) 17 function new(config)
16 config = config or {}; 18 config = config or {};
17 return setmetatable({ 19 return setmetatable({
18 config = setmetatable(config, default_config); 20 config = setmetatable(config, default_config);
21 node_defaults = setmetatable(config.node_defaults or {}, default_node_config);
19 affiliations = {}; 22 affiliations = {};
20 subscriptions = {}; 23 subscriptions = {};
21 nodes = {}; 24 nodes = {};
22 data = {}; 25 data = {};
23 events = events.new(); 26 events = events.new();
202 return false, "item-not-found"; 205 return false, "item-not-found";
203 end 206 end
204 return true, node_obj.subscribers[jid]; 207 return true, node_obj.subscribers[jid];
205 end 208 end
206 209
207 function service:create(node, actor) 210 function service:create(node, actor, options)
208 -- Access checking 211 -- Access checking
209 if not self:may(node, actor, "create") then 212 if not self:may(node, actor, "create") then
210 return false, "forbidden"; 213 return false, "forbidden";
211 end 214 end
212 -- 215 --
216 219
217 self.data[node] = {}; 220 self.data[node] = {};
218 self.nodes[node] = { 221 self.nodes[node] = {
219 name = node; 222 name = node;
220 subscribers = {}; 223 subscribers = {};
221 config = {}; 224 config = setmetatable(options or {}, {__index=self.node_defaults});
222 affiliations = {}; 225 affiliations = {};
223 }; 226 };
224 setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT 227 setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT
225 self.events.fire_event("node-created", { node = node, actor = actor }); 228 self.events.fire_event("node-created", { node = node, actor = actor });
226 local ok, err = self:set_affiliation(node, true, actor, "owner"); 229 local ok, err = self:set_affiliation(node, true, actor, "owner");
409 end 412 end
410 node_obj.capabilities = capabilities; 413 node_obj.capabilities = capabilities;
411 return true; 414 return true;
412 end 415 end
413 416
417 function service:set_node_config(node, actor, new_config)
418 if not self:may(node, actor, "configure") then
419 return false, "forbidden";
420 end
421
422 local node_obj = self.nodes[node];
423 if not node_obj then
424 return false, "item-not-found";
425 end
426
427 for k,v in pairs(new_config) do
428 node_obj.config[k] = v;
429 end
430
431 return true;
432 end
433
414 return _M; 434 return _M;