Changeset

2382:f159ad7ac18a

mod_pubsub_feeds: Check if node exists earlier and attempt to create it if not
author Kim Alvefur <zash@zash.se>
date Sat, 19 Nov 2016 19:37:21 +0100 (2016-11-19)
parents 2381:a4614a329bdd
children 2383:18323c6aa133
files mod_pubsub_feeds/mod_pubsub_feeds.lua
diffstat 1 files changed, 9 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pubsub_feeds/mod_pubsub_feeds.lua	Sat Nov 19 19:17:49 2016 +0100
+++ b/mod_pubsub_feeds/mod_pubsub_feeds.lua	Sat Nov 19 19:37:21 2016 +0100
@@ -83,6 +83,14 @@
 	for entry in feed:childtags("entry") do
 		table.insert(entries, entry);
 	end
+	local ok = pubsub.service:get_items(node, true);
+	if not ok then
+		local ok, err = pubsub.service:create(node, true);
+		if not ok then
+			module:log("error", "Could not create node %s: %s", node, err);
+			return;
+		end
+	end
 	for i = #entries, 1, -1 do -- Feeds are usually in reverse order
 		local entry = entries[i];
 		entry.attr.xmlns = xmlns_atom;
@@ -110,21 +118,7 @@
 			--module:log("debug", "publishing to %s, id %s", node, id);
 			local ok, err = pubsub.service:publish(node, true, id, xitem);
 			if not ok then
-				if err == "item-not-found" then -- try again
-					--module:log("debug", "got item-not-found, creating %s and trying again", node);
-					local ok, err = pubsub.service:create(node, true);
-					if not ok then
-						module:log("error", "could not create node %s: %s", node, err);
-						return;
-					end
-					local ok, err = pubsub.service:publish(node, true, id, xitem);
-					if not ok then
-						module:log("error", "could not create or publish node %s: %s", node, err);
-						return
-					end
-				else
-					module:log("error", "publishing %s failed: %s", node, err);
-				end
+				module:log("error", "Publishing to node %s failed: %s", node, err);
 			end
 		end
 	end