Comparison

plugins/mod_pubsub/pubsub.lib.lua @ 8213:e1272aeef31c

mod_pubsub: Add item persistence using mod_storage_*’s archive store.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 15 Apr 2017 01:21:55 +0100
parent 8210:352d605b1178
child 8216:d80233ca006d
comparison
equal deleted inserted replaced
8212:66173e4b355a 8213:e1272aeef31c
1 local t_unpack = table.unpack or unpack; -- luacheck: ignore 113 1 local t_unpack = table.unpack or unpack; -- luacheck: ignore 113
2 local time_now = os.time;
2 3
3 local st = require "util.stanza"; 4 local st = require "util.stanza";
4 local uuid_generate = require "util.uuid".generate; 5 local uuid_generate = require "util.uuid".generate;
5 local dataform = require"util.dataforms".new; 6 local dataform = require"util.dataforms".new;
6 7
314 :add_child(node_config_form:form(service.node_defaults)); 315 :add_child(node_config_form:form(service.node_defaults));
315 origin.send(reply); 316 origin.send(reply);
316 return true; 317 return true;
317 end 318 end
318 319
320 local function create_encapsulating_item(id, payload, publisher, expose_publisher)
321 local item = st.stanza("item", { id = id }, xmlns_pubsub);
322 item:add_child(payload);
323 if expose_publisher then
324 item.attr.publisher = publisher;
325 end
326 return item;
327 end
328
329 local function simple_itemstore(archive, config, node, expose_publisher)
330 module:log("debug", "Creation of itemstore for node %s with config %s", node, config);
331 local get_set = {};
332 function get_set:items()
333 local store = self.store;
334 local data, err = archive:find(node);
335 if not data then
336 module:log("error", "Unable to get items: %s", err);
337 return true;
338 end
339 module:log("debug", "Listed items %s from store %s", data, store);
340 return function()
341 local id, payload, when, publisher = data();
342 if id == nil then
343 return;
344 end
345 local item = create_encapsulating_item(id, payload, publisher, expose_publisher);
346 return id, item;
347 end;
348 end
349 function get_set:get(key)
350 local store = self.store;
351 local data, err = archive:find(node, {
352 key = key;
353 });
354 if not data then
355 module:log("error", "Unable to get item: %s", err);
356 return nil, err;
357 end
358 -- Workaround for buggy SQL drivers which require iterating until we get a nil.
359 local id, payload, when, publisher;
360 for a, b, c, d in data() do
361 id, payload, when, publisher = a, b, c, d;
362 end
363 module:log("debug", "Get item %s (published at %s by %s) from store %s", id, when, publisher, store);
364 if id == nil then
365 return nil;
366 end
367 return create_encapsulating_item(id, payload, publisher, expose_publisher);
368 end
369 function get_set:set(key, value)
370 local store = self.store;
371 module:log("debug", "Set item %s to %s for %s in store %s", key, value, node, store);
372 local data, err;
373 if value ~= nil then
374 local publisher = value.attr.publisher;
375 local payload = value.tags[1];
376 data, err = archive:append(node, key, payload, time_now(), publisher);
377 else
378 data, err = archive:delete(node, {
379 key = key;
380 });
381 end
382 if not data then
383 module:log("error", "Unable to set item: %s", err);
384 return nil, err;
385 end
386 return true;
387 end
388 return setmetatable(get_set, archive);
389 end
390 _M.simple_itemstore = simple_itemstore;
391
319 return _M; 392 return _M;