aboutsummaryrefslogtreecommitdiffstats
path: root/util/pubsub.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2016-10-16 00:39:10 +0200
committerKim Alvefur <zash@zash.se>2016-10-16 00:39:10 +0200
commitda9ff7d218486105eb2e529cfe8e22346aa53430 (patch)
tree9b1c27bfc13dcff3fd25049c59879bd327919188 /util/pubsub.lua
parente46e48a35cd0973d6a247fc0f3e56127c50e380b (diff)
parentffbc38cfa3aafd8cfeae06a4396d5a074ffebc7e (diff)
downloadprosody-da9ff7d218486105eb2e529cfe8e22346aa53430.tar.gz
prosody-da9ff7d218486105eb2e529cfe8e22346aa53430.zip
Merge 0.10->trunk
Diffstat (limited to 'util/pubsub.lua')
-rw-r--r--util/pubsub.lua57
1 files changed, 24 insertions, 33 deletions
diff --git a/util/pubsub.lua b/util/pubsub.lua
index 6d12690a..520d80a7 100644
--- a/util/pubsub.lua
+++ b/util/pubsub.lua
@@ -1,10 +1,11 @@
local events = require "util.events";
-local t_remove = table.remove;
+local cache = require "util.cache";
local service = {};
local service_mt = { __index = service };
local default_config = { __index = {
+ itemstore = function (config) return cache.new(tonumber(config["pubsub#max_items"])) end;
broadcaster = function () end;
get_affiliation = function () end;
capabilities = {};
@@ -216,14 +217,13 @@ function service:create(node, actor, options)
return false, "conflict";
end
- self.data[node] = {};
self.nodes[node] = {
name = node;
subscribers = {};
config = setmetatable(options or {}, {__index=self.node_defaults});
affiliations = {};
};
- setmetatable(self.nodes[node], { __index = { data = self.data[node] } }); -- COMPAT
+ self.data[node] = self.config.itemstore(self.nodes[node].config);
self.events.fire_event("node-created", { node = node, actor = actor });
local ok, err = self:set_affiliation(node, true, actor, "owner");
if not ok then
@@ -250,25 +250,6 @@ function service:delete(node, actor)
return true;
end
-local function remove_item_by_id(data, id)
- if not data[id] then return end
- data[id] = nil;
- for i, _id in ipairs(data) do
- if id == _id then
- t_remove(data, i);
- return i;
- end
- 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
@@ -287,10 +268,10 @@ function service:publish(node, actor, id, item)
node_obj = self.nodes[node];
end
local node_data = self.data[node];
- 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"]);
+ local ok = node_data:set(id, item);
+ if not ok then
+ return nil, "internal-server-error";
+ end
self.events.fire_event("item-published", { node = node, actor = actor, id = id, item = item });
self.config.broadcaster("items", node, node_obj.subscribers, item, actor);
return true;
@@ -303,11 +284,14 @@ function service:retract(node, actor, id, retract)
end
--
local node_obj = self.nodes[node];
- if (not node_obj) or (not self.data[node][id]) then
+ if (not node_obj) or (not self.data[node]:get(id)) then
return false, "item-not-found";
end
+ local ok = self.data[node]:set(id, nil);
+ if not ok then
+ return nil, "internal-server-error";
+ end
self.events.fire_event("item-retracted", { node = node, actor = actor, id = id });
- remove_item_by_id(self.data[node], id);
if retract then
self.config.broadcaster("items", node, node_obj.subscribers, retract);
end
@@ -324,7 +308,7 @@ function service:purge(node, actor, notify)
if not node_obj then
return false, "item-not-found";
end
- self.data[node] = {}; -- Purge
+ self.data[node] = self.config.itemstore(self.nodes[node].config);
self.events.fire_event("node-purged", { node = node, actor = actor });
if notify then
self.config.broadcaster("purge", node, node_obj.subscribers);
@@ -343,9 +327,13 @@ function service:get_items(node, actor, id)
return false, "item-not-found";
end
if id then -- Restrict results to a single specific item
- return true, { id, [id] = self.data[node][id] };
+ return true, { id, [id] = self.data[node]:get(id) };
else
- return true, self.data[node];
+ local data = {}
+ for key, value in self.data[node]:items() do
+ data[key] = value;
+ end
+ return true, data;
end
end
@@ -435,8 +423,11 @@ 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"]);
-
+ local new_data = self.config.itemstore(self.nodes[node].config);
+ for key, value in self.data[node]:items() do
+ new_data:set(key, value);
+ end
+ self.data[node] = new_data;
return true;
end