Changeset

4871:029ae3c29683

mod_http_xep227: Handle nil/errors opening archive stores This matches how mod_mam opens archive stores. nil can be returned when there is no data in the store yet (it doesn't exist).
author Matthew Wild <mwild1@gmail.com>
date Sun, 16 Jan 2022 15:01:20 +0000
parents 4870:d8a0a8dcdc0d
children 4872:bc54f651b5a4
files mod_http_xep227/mod_http_xep227.lua
diffstat 1 files changed, 34 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mod_http_xep227/mod_http_xep227.lua	Sun Jan 16 13:44:26 2022 +0000
+++ b/mod_http_xep227/mod_http_xep227.lua	Sun Jan 16 15:01:20 2022 +0000
@@ -120,18 +120,24 @@
 		local source_driver = get_config_driver(store_name, session.host);
 		local source_archive = source_driver:open(store_name, "archive");
 		local dest_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml);
-		local count, errs = 0, 0;
-		for id, item, when, with in source_archive:find(username) do
-			local ok, err = dest_archive:append(username, id, item, when, with);
-			if ok then
-				count = count + 1;
-			else
-				module:log("warn", "Error: %s", err);
-				errs = errs + 1;
+		local results_iter, results_err = source_archive:find(username);
+		if results_iter then
+			local count, errs = 0, 0;
+			for id, item, when, with in results_iter do
+				local ok, err = dest_archive:append(username, id, item, when, with);
+				if ok then
+					count = count + 1;
+				else
+					module:log("warn", "Error: %s", err);
+					errs = errs + 1;
+				end
+				if ( count + errs ) % 100 == 0 then
+					module:log("info", "%d items migrated, %d errors", count, errs);
+				end
 			end
-			if ( count + errs ) % 100 == 0 then
-				module:log("info", "%d items migrated, %d errors", count, errs);
-			end
+		elseif results_err then
+			module:log("warn", "Unable to read from '%s': %s", store_name, results_err);
+			return 500;
 		end
 	end
 
@@ -211,18 +217,24 @@
 		local source_archive = xep227_driver:open_xep0227(store_name, "archive", user_xml);
 		local dest_driver = get_config_driver(store_name, session.host);
 		local dest_archive = dest_driver:open(store_name, "archive");
-		local count, errs = 0, 0;
-		for id, item, when, with in source_archive:find(username) do
-			local ok, err = dest_archive:append(username, id, item, when, with);
-			if ok then
-				count = count + 1;
-			else
-				module:log("warn", "Error: %s", err);
-				errs = errs + 1;
+		local results_iter, results_err = source_archive:find(username);
+		if results_iter then
+			local count, errs = 0, 0;
+			for id, item, when, with in source_archive:find(username) do
+				local ok, err = dest_archive:append(username, id, item, when, with);
+				if ok then
+					count = count + 1;
+				else
+					module:log("warn", "Error: %s", err);
+					errs = errs + 1;
+				end
+				if ( count + errs ) % 100 == 0 then
+					module:log("info", "%d items migrated, %d errors", count, errs);
+				end
 			end
-			if ( count + errs ) % 100 == 0 then
-				module:log("info", "%d items migrated, %d errors", count, errs);
-			end
+		elseif results_err then
+			module:log("warn", "Unable to read from '%s': %s", store_name, results_err);
+			return 500;
 		end
 	end