From d76cfb4b2de54dedf5c8dffa301b6baa4d54f327 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 27 Sep 2014 19:57:33 +0200 Subject: util.pubsub: Use local --- util/pubsub.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/pubsub.lua b/util/pubsub.lua index fc67cb1f..afab634a 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -278,7 +278,7 @@ function service:publish(node, actor, id, item) end local node_data = self.data[node]; remove_item_by_id(node_data, id); - node_data[#self.data[node] + 1] = id; + node_data[#node_data + 1] = id; node_data[id] = item; self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item }); self.config.broadcaster("items", node, node_obj.subscribers, item); -- cgit v1.2.3 From 2d58001a25c924d5e77e568c9984981443aee5ad Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 28 Sep 2014 00:05:21 +0200 Subject: util.pubsub: One less table allocated per pubsub object created --- util/pubsub.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'util') diff --git a/util/pubsub.lua b/util/pubsub.lua index afab634a..3c67be90 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -6,16 +6,16 @@ module("pubsub", package.seeall); local service = {}; local service_mt = { __index = service }; -local default_config = { +local default_config = { __index = { broadcaster = function () end; get_affiliation = function () end; capabilities = {}; -}; +} }; function new(config) config = config or {}; return setmetatable({ - config = setmetatable(config, { __index = default_config }); + config = setmetatable(config, default_config); affiliations = {}; subscriptions = {}; nodes = {}; -- cgit v1.2.3 From 4f74defed4aa265350c0465c63b4698eaab9814c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 28 Sep 2014 01:45:59 +0200 Subject: util.pubsub: Add support for node configuration --- util/pubsub.lua | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'util') diff --git a/util/pubsub.lua b/util/pubsub.lua index 3c67be90..424a48c6 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -11,11 +11,14 @@ local default_config = { __index = { get_affiliation = function () end; capabilities = {}; } }; +local default_node_config = { __index = { +} }; function new(config) config = config or {}; return setmetatable({ config = setmetatable(config, default_config); + node_defaults = setmetatable(config.node_defaults or {}, default_node_config); affiliations = {}; subscriptions = {}; nodes = {}; @@ -204,7 +207,7 @@ function service:get_subscription(node, actor, jid) return true, node_obj.subscribers[jid]; end -function service:create(node, actor) +function service:create(node, actor, options) -- Access checking if not self:may(node, actor, "create") then return false, "forbidden"; @@ -218,7 +221,7 @@ function service:create(node, actor) self.nodes[node] = { name = node; subscribers = {}; - config = {}; + config = setmetatable(options or {}, {__index=self.node_defaults}); affiliations = {}; }; setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT @@ -411,4 +414,21 @@ function service:set_node_capabilities(node, actor, capabilities) return true; end +function service:set_node_config(node, actor, new_config) + if not self:may(node, actor, "configure") then + return false, "forbidden"; + end + + local node_obj = self.nodes[node]; + if not node_obj then + return false, "item-not-found"; + end + + for k,v in pairs(new_config) do + node_obj.config[k] = v; + end + + return true; +end + return _M; -- cgit v1.2.3 From 1d050ce4e500189e7dd6c0bb4a298afc45cbd1bb Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 28 Sep 2014 01:50:00 +0200 Subject: util.pubsub: Add support for limiting the number of item in a node (default to 20) --- util/pubsub.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'util') diff --git a/util/pubsub.lua b/util/pubsub.lua index 424a48c6..2f8525b6 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -12,6 +12,7 @@ local default_config = { __index = { capabilities = {}; } }; local default_node_config = { __index = { + ["pubsub#max_items"] = "20"; } }; function new(config) @@ -262,6 +263,14 @@ local function remove_item_by_id(data, id) end end +local function trim_items(data, max) + max = tonumber(max); + if not max or #data <= max then return end + repeat + data[t_remove(data, 1)] = nil; + until #data <= max +end + function service:publish(node, actor, id, item) -- Access checking if not self:may(node, actor, "publish") then @@ -283,6 +292,7 @@ function service:publish(node, actor, id, item) remove_item_by_id(node_data, id); node_data[#node_data + 1] = id; node_data[id] = item; + trim_items(node_data, node_obj.config["pubsub#max_items"]); self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item }); self.config.broadcaster("items", node, node_obj.subscribers, item); return true; @@ -427,6 +437,7 @@ function service:set_node_config(node, actor, new_config) for k,v in pairs(new_config) do node_obj.config[k] = v; end + trim_items(self.data[node], node_obj.config["pubsub#max_items"]); return true; end -- cgit v1.2.3