Changeset

9816:7f84d7f77a00 0.11

util.pubsub: Add support for requesting multiple specific items (needed for #1305)
author Kim Alvefur <zash@zash.se>
date Mon, 28 Jan 2019 01:41:01 +0100
parents 9814:5eb4ef537e98
children 9817:7aad9eb7f050
files spec/util_pubsub_spec.lua util/pubsub.lua
diffstat 2 files changed, 44 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_pubsub_spec.lua	Sat Jan 26 13:32:26 2019 +0100
+++ b/spec/util_pubsub_spec.lua	Mon Jan 28 01:41:01 2019 +0100
@@ -170,6 +170,37 @@
 
 	end);
 
+	describe("the thing", function ()
+		randomize(false); -- These tests are ordered
+
+		local service = pubsub.new();
+
+		it("creates a node with some items", function ()
+			assert.truthy(service:create("node", true, { max_items = 3 }));
+			assert.truthy(service:publish("node", true, "1", "item 1"));
+			assert.truthy(service:publish("node", true, "2", "item 2"));
+			assert.truthy(service:publish("node", true, "3", "item 3"));
+		end);
+
+		it("should return the requested item", function ()
+			local ok, ret = service:get_items("node", true, "1");
+			assert.truthy(ok);
+			assert.same({ "1", ["1"] = "item 1" }, ret);
+		end);
+
+		it("should return multiple requested items", function ()
+			local ok, ret = service:get_items("node", true, { "1", "2" });
+			assert.truthy(ok);
+			assert.same({
+				"1",
+				"2",
+				["1"] = "item 1",
+				["2"] = "item 2",
+			}, ret);
+		end);
+	end);
+
+
 	describe("node config", function ()
 		local service;
 		before_each(function ()
--- a/util/pubsub.lua	Sat Jan 26 13:32:26 2019 +0100
+++ b/util/pubsub.lua	Mon Jan 28 01:41:01 2019 +0100
@@ -600,7 +600,7 @@
 	return true
 end
 
-function service:get_items(node, actor, id) --> (true, { id, [id] = node }) or (false, err)
+function service:get_items(node, actor, ids) --> (true, { id, [id] = node }) or (false, err)
 	-- Access checking
 	if not self:may(node, actor, "get_items") then
 		return false, "forbidden";
@@ -610,20 +610,25 @@
 	if not node_obj then
 		return false, "item-not-found";
 	end
-	if id then -- Restrict results to a single specific item
-		local with_id = self.data[node]:get(id);
-		if not with_id then
-			return true, { };
+	if type(ids) == "string" then -- COMPAT see #1305
+		ids = { ids };
+	end
+	local data = {};
+	if ids then
+		for _, key in ipairs(ids) do
+			local value = self.data[node]:get(key);
+			if value then
+				data[#data+1] = key;
+				data[key] = value;
+			end
 		end
-		return true, { id, [id] = with_id };
 	else
-		local data = {}
 		for key, value in self.data[node]:items() do
 			data[#data+1] = key;
 			data[key] = value;
 		end
-		return true, data;
 	end
+	return true, data;
 end
 
 function service:get_last_item(node, actor) --> (true, id, node) or (false, err)