util.pubsub: Fix behavior of persist_items disabled
authorKim Alvefur <zash@zash.se>
Thu, 22 Jul 2021 21:01:11 +0200
changeset 11725 7a77f0c05382
parent 11724 72512c0858b3
child 11726 bc2d3f110a39
util.pubsub: Fix behavior of persist_items disabled When set to 'false' there is no need for a persistence interface at all, since items are not persisted after being broadcast. Had started wondering if maybe the behavior was wrong, after reading parts of XEP-0060 that pointed in that direction. Some discussion of this can be found in logs of xmpp:xsf@muc.xmpp.org?join from around 2021-07-20 Thanks to Ralph for confirming.
spec/util_pubsub_spec.lua
util/pubsub.lua
--- a/spec/util_pubsub_spec.lua	Wed Jul 21 23:35:08 2021 +0200
+++ b/spec/util_pubsub_spec.lua	Thu Jul 22 21:01:11 2021 +0200
@@ -509,4 +509,23 @@
 		end);
 	end);
 
+	describe("persist_items", function()
+		it("can be disabled", function()
+			local broadcaster = spy.new(function(notif_type, node_name, subscribers, item) -- luacheck: ignore 212
+			end);
+			local service = pubsub.new { node_defaults = { persist_items = false }, broadcaster = broadcaster }
+
+			local ok = service:create("node", true)
+			assert.truthy(ok);
+
+			local ok = service:publish("node", true, "1", "item");
+			assert.truthy(ok);
+			assert.spy(broadcaster).was_called();
+
+			local ok, items = service:get_items("node", true);
+			assert.truthy(ok);
+			assert.same(items, {});
+		end);
+
+	end)
 end);
--- a/util/pubsub.lua	Wed Jul 21 23:35:08 2021 +0200
+++ b/util/pubsub.lua	Thu Jul 22 21:01:11 2021 +0200
@@ -177,8 +177,11 @@
 	-- Load nodes from storage, if we have a store and it supports iterating over stored items
 	if config.nodestore and config.nodestore.users then
 		for node_name in config.nodestore:users() do
-			service.nodes[node_name] = load_node_from_store(service, node_name);
-			service.data[node_name] = config.itemstore(service.nodes[node_name].config, node_name);
+			local node = load_node_from_store(service, node_name);
+			service.nodes[node_name] = node;
+			if node.config.persist_items then
+				service.data[node_name] = config.itemstore(service.nodes[node_name].config, node_name);
+			end
 
 			for jid in pairs(service.nodes[node_name].subscribers) do
 				local normal_jid = service.config.normalize_jid(jid);
@@ -466,7 +469,10 @@
 		end
 	end
 
-	self.data[node] = self.config.itemstore(self.nodes[node].config, node);
+	if config.persist_items then
+		self.data[node] = self.config.itemstore(self.nodes[node].config, node);
+	end
+
 	self.events.fire_event("node-created", { service = self, node = node, actor = actor });
 	if actor ~= true then
 		local ok, err = self:set_affiliation(node, true, actor, "owner");
@@ -564,9 +570,11 @@
 	if not self.config.itemcheck(item) then
 		return nil, "invalid-item";
 	end
-	local node_data = self.data[node];
-	if node_data then
-		local ok = node_data:set(id, item);
+	if node_obj.config.persist_items then
+		if not self.data[node] then
+			self.data[node] = self.config.itemstore(self.nodes[node].config, node);
+		end
+		local ok = self.data[node]:set(id, item);
 		if not ok then
 			return nil, "internal-server-error";
 		end
@@ -816,7 +824,14 @@
 	end
 
 	if old_config["persist_items"] ~= node_obj.config["persist_items"] then
-		self.data[node] = self.config.itemstore(self.nodes[node].config, node);
+		if node_obj.config["persist_items"] then
+			self.data[node] = self.config.itemstore(self.nodes[node].config, node);
+		elseif self.data[node] then
+			if self.data[node].clear then
+				self.data[node]:clear()
+			end
+			self.data[node] = nil;
+		end
 	elseif old_config["max_items"] ~= node_obj.config["max_items"] then
 		if self.data[node] then
 			self.data[node]:resize(self.nodes[node].config["max_items"]);