Mercurial > prosody-modules
comparison mod_pubsub_feeds/mod_pubsub_feeds.lua @ 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 |
parents | a4614a329bdd |
children | 18323c6aa133 |
comparison
equal
deleted
inserted
replaced
2381:a4614a329bdd | 2382:f159ad7ac18a |
---|---|
81 local feed = parse_feed(item.data); | 81 local feed = parse_feed(item.data); |
82 local entries = {}; | 82 local entries = {}; |
83 for entry in feed:childtags("entry") do | 83 for entry in feed:childtags("entry") do |
84 table.insert(entries, entry); | 84 table.insert(entries, entry); |
85 end | 85 end |
86 local ok = pubsub.service:get_items(node, true); | |
87 if not ok then | |
88 local ok, err = pubsub.service:create(node, true); | |
89 if not ok then | |
90 module:log("error", "Could not create node %s: %s", node, err); | |
91 return; | |
92 end | |
93 end | |
86 for i = #entries, 1, -1 do -- Feeds are usually in reverse order | 94 for i = #entries, 1, -1 do -- Feeds are usually in reverse order |
87 local entry = entries[i]; | 95 local entry = entries[i]; |
88 entry.attr.xmlns = xmlns_atom; | 96 entry.attr.xmlns = xmlns_atom; |
89 | 97 |
90 local e_published = entry:get_child_text("published"); | 98 local e_published = entry:get_child_text("published"); |
108 -- TODO Put data from /feed into item/source | 116 -- TODO Put data from /feed into item/source |
109 | 117 |
110 --module:log("debug", "publishing to %s, id %s", node, id); | 118 --module:log("debug", "publishing to %s, id %s", node, id); |
111 local ok, err = pubsub.service:publish(node, true, id, xitem); | 119 local ok, err = pubsub.service:publish(node, true, id, xitem); |
112 if not ok then | 120 if not ok then |
113 if err == "item-not-found" then -- try again | 121 module:log("error", "Publishing to node %s failed: %s", node, err); |
114 --module:log("debug", "got item-not-found, creating %s and trying again", node); | |
115 local ok, err = pubsub.service:create(node, true); | |
116 if not ok then | |
117 module:log("error", "could not create node %s: %s", node, err); | |
118 return; | |
119 end | |
120 local ok, err = pubsub.service:publish(node, true, id, xitem); | |
121 if not ok then | |
122 module:log("error", "could not create or publish node %s: %s", node, err); | |
123 return | |
124 end | |
125 else | |
126 module:log("error", "publishing %s failed: %s", node, err); | |
127 end | |
128 end | 122 end |
129 end | 123 end |
130 end | 124 end |
131 | 125 |
132 if item.lease_expires and item.lease_expires > time() then | 126 if item.lease_expires and item.lease_expires > time() then |