util.pubsub: Restore subscription index from stored data (fixes #1281) 0.11
authorKim Alvefur <zash@zash.se>
Sat, 29 Dec 2018 21:47:51 +0100
branch0.11
changeset 9746 18eca6afb367
parent 9733 4f6413ec08a8
child 9747 73e8668321e9
child 9766 34988a408b74
util.pubsub: Restore subscription index from stored data (fixes #1281)
spec/util_pubsub_spec.lua
util/pubsub.lua
--- a/spec/util_pubsub_spec.lua	Sun Dec 23 15:22:49 2018 +0100
+++ b/spec/util_pubsub_spec.lua	Sat Dec 29 21:47:51 2018 +0100
@@ -375,4 +375,34 @@
 			end);
 		end);
 	end);
+
+	describe("restoring data from nodestore", function ()
+		local nodestore = {
+			data = {
+				test = {
+					name = "test";
+					config = {};
+					affiliations = {};
+					subscribers = {
+						["someone"] = true;
+					};
+				}
+			}
+		};
+		function nodestore:users()
+			return pairs(self.data)
+		end
+		function nodestore:get(key)
+			return self.data[key];
+		end
+		local service = pubsub.new({
+			nodestore = nodestore;
+		});
+		it("subscriptions", function ()
+			local ok, ret = service:get_subscriptions(nil, true, nil)
+			assert.is_true(ok);
+			assert.same({ { node = "test", jid = "someone", subscription = true, } }, ret);
+		end);
+	end);
+
 end);
--- a/util/pubsub.lua	Sun Dec 23 15:22:49 2018 +0100
+++ b/util/pubsub.lua	Sat Dec 29 21:47:51 2018 +0100
@@ -177,6 +177,20 @@
 		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);
+
+			for jid in pairs(service.nodes[node_name].subscribers) do
+				local normal_jid = service.config.normalize_jid(jid);
+				local subs = service.subscriptions[normal_jid];
+				if subs then
+					if not subs[jid] then
+						subs[jid] = { [node_name] = true };
+					else
+						subs[jid][node_name] = true;
+					end
+				else
+					service.subscriptions[normal_jid] = { [jid] = { [node_name] = true } };
+				end
+			end
 		end
 	end