mirror of
https://github.com/bjc/prosody.git
synced 2025-04-04 13:47:41 +03:00
util.pubsub: Add support for limiting number of items to retrieve
Hopefully this will eventually be upgraded to RSM, which is why the argument is called 'resultspec' and is a table.
This commit is contained in:
parent
e3c0a877bf
commit
1546e59310
2 changed files with 63 additions and 1 deletions
|
@ -528,4 +528,61 @@ describe("util.pubsub", function ()
|
||||||
end);
|
end);
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("max_items", function ()
|
||||||
|
it("works", function ()
|
||||||
|
local service = pubsub.new { };
|
||||||
|
|
||||||
|
local ok = service:create("node", true)
|
||||||
|
assert.truthy(ok);
|
||||||
|
|
||||||
|
for i = 1, 20 do
|
||||||
|
assert.truthy(service:publish("node", true, "item"..tostring(i), "data"..tostring(i)));
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local ok, items = service:get_items("node", true, nil, { max = 3 });
|
||||||
|
assert.truthy(ok, items);
|
||||||
|
assert.equal(3, #items);
|
||||||
|
assert.same({
|
||||||
|
"item20",
|
||||||
|
"item19",
|
||||||
|
"item18",
|
||||||
|
item20 = "data20",
|
||||||
|
item19 = "data19",
|
||||||
|
item18 = "data18",
|
||||||
|
}, items, "items should be ordered by oldest first");
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local ok, items = service:get_items("node", true, nil, { max = 10 });
|
||||||
|
assert.truthy(ok, items);
|
||||||
|
assert.equal(10, #items);
|
||||||
|
assert.same({
|
||||||
|
"item20",
|
||||||
|
"item19",
|
||||||
|
"item18",
|
||||||
|
"item17",
|
||||||
|
"item16",
|
||||||
|
"item15",
|
||||||
|
"item14",
|
||||||
|
"item13",
|
||||||
|
"item12",
|
||||||
|
"item11",
|
||||||
|
item20 = "data20",
|
||||||
|
item19 = "data19",
|
||||||
|
item18 = "data18",
|
||||||
|
item17 = "data17",
|
||||||
|
item16 = "data16",
|
||||||
|
item15 = "data15",
|
||||||
|
item14 = "data14",
|
||||||
|
item13 = "data13",
|
||||||
|
item12 = "data12",
|
||||||
|
item11 = "data11",
|
||||||
|
}, items, "items should be ordered by oldest first");
|
||||||
|
end
|
||||||
|
|
||||||
|
end);
|
||||||
|
|
||||||
|
end)
|
||||||
end);
|
end);
|
||||||
|
|
|
@ -642,7 +642,7 @@ function service:purge(node, actor, notify) --> ok, err
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or (false, err)
|
function service:get_items(node, actor, ids, resultspec) --> (true, { id, [id] = node }) or (false, err)
|
||||||
-- Access checking
|
-- Access checking
|
||||||
if not self:may(node, actor, "get_items") then
|
if not self:may(node, actor, "get_items") then
|
||||||
return false, "forbidden";
|
return false, "forbidden";
|
||||||
|
@ -660,18 +660,23 @@ function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or
|
||||||
ids = { ids };
|
ids = { ids };
|
||||||
end
|
end
|
||||||
local data = {};
|
local data = {};
|
||||||
|
local limit = resultspec and resultspec.max;
|
||||||
if ids then
|
if ids then
|
||||||
for _, key in ipairs(ids) do
|
for _, key in ipairs(ids) do
|
||||||
local value = self.data[node]:get(key);
|
local value = self.data[node]:get(key);
|
||||||
if value then
|
if value then
|
||||||
data[#data+1] = key;
|
data[#data+1] = key;
|
||||||
data[key] = value;
|
data[key] = value;
|
||||||
|
-- Limits and ids seem like a problematic combination.
|
||||||
|
if limit and #data >= limit then break end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
for key, value in self.data[node]:items() do
|
for key, value in self.data[node]:items() do
|
||||||
data[#data+1] = key;
|
data[#data+1] = key;
|
||||||
data[key] = value;
|
data[key] = value;
|
||||||
|
if limit and #data >= limit then break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return true, data;
|
return true, data;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue