mod_storage_xep0227: Add API to iterate all stores of a user
authorMatthew Wild <mwild1@gmail.com>
Fri, 14 Jan 2022 16:57:19 +0000
changeset 12188 326f5466ddc7
parent 12187 e77c938ed92b
child 12189 708769a4c5da
mod_storage_xep0227: Add API to iterate all stores of a user
plugins/mod_storage_xep0227.lua
--- a/plugins/mod_storage_xep0227.lua	Fri Jan 14 16:55:18 2022 +0000
+++ b/plugins/mod_storage_xep0227.lua	Fri Jan 14 16:57:19 2022 +0000
@@ -688,6 +688,18 @@
 	return instance;
 end
 
+local function get_store_names_from_xml(self, user_xml)
+	local stores = set.new();
+	for handler_name, handler_funcs in pairs(handlers) do
+		if handler_funcs._stores then
+			stores:include(handler_funcs._stores(self, user_xml));
+		else
+			stores:include(handler_name);
+		end
+	end
+	return stores;
+end
+
 local function get_store_names(self, path)
 	local stores = set.new();
 	local f, err = io_open(paths.join(prosody.paths.data, path));
@@ -698,13 +710,8 @@
 	module:log("info", "Loaded %s", path);
 	local s = f:read("*a");
 	f:close();
-	local xml = parse_xml_real(s);
-	for _, handler_funcs in pairs(handlers) do
-		if handler_funcs._stores then
-			stores:include(handler_funcs._stores(self, xml));
-		end
-	end
-	return stores;
+	local user_xml = parse_xml_real(s);
+	return get_store_names_from_xml(self, user_xml);
 end
 
 function driver:stores(username)
@@ -733,4 +740,13 @@
 	return store_names:items();
 end
 
+function driver:xep0227_user_stores(username, host)
+	local user_xml = self:_get_user_xml(username, host);
+	if not user_xml then
+		return nil;
+	end
+	local store_names = get_store_names_from_xml(username, host);
+	return store_names:items();
+end
+
 module:provides("storage", driver);