comparison plugins/mod_pubsub/pubsub.lib.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parents 76e135fbca0f
children ef623d719894
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
5 local set = require "util.set"; 5 local set = require "util.set";
6 local st = require "util.stanza"; 6 local st = require "util.stanza";
7 local it = require "util.iterators"; 7 local it = require "util.iterators";
8 local uuid_generate = require "util.uuid".generate; 8 local uuid_generate = require "util.uuid".generate;
9 local dataform = require"util.dataforms".new; 9 local dataform = require"util.dataforms".new;
10 local errors = require "util.error";
10 11
11 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; 12 local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
12 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors"; 13 local xmlns_pubsub_errors = "http://jabber.org/protocol/pubsub#errors";
13 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner"; 14 local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
14 15
32 ["precondition-not-met"] = { "cancel", "conflict", nil, "precondition-not-met" }; 33 ["precondition-not-met"] = { "cancel", "conflict", nil, "precondition-not-met" };
33 ["invalid-item"] = { "modify", "bad-request", "invalid item" }; 34 ["invalid-item"] = { "modify", "bad-request", "invalid item" };
34 }; 35 };
35 local function pubsub_error_reply(stanza, error) 36 local function pubsub_error_reply(stanza, error)
36 local e = pubsub_errors[error]; 37 local e = pubsub_errors[error];
38 if not e and errors.is_err(error) then
39 e = { error.type, error.condition, error.text, error.pubsub_condition };
40 end
37 local reply = st.error_reply(stanza, t_unpack(e, 1, 3)); 41 local reply = st.error_reply(stanza, t_unpack(e, 1, 3));
38 if e[4] then 42 if e[4] then
39 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up(); 43 reply:tag(e[4], { xmlns = xmlns_pubsub_errors }):up();
40 end 44 end
41 return reply; 45 return reply;
182 name = "pubsub#description"; 186 name = "pubsub#description";
183 }; 187 };
184 { 188 {
185 type = "text-single"; 189 type = "text-single";
186 name = "pubsub#type"; 190 name = "pubsub#type";
191 };
192 {
193 type = "text-single";
194 name = "pubsub#access_model";
195 };
196 {
197 type = "text-single";
198 name = "pubsub#publish_model";
187 }; 199 };
188 }; 200 };
189 201
190 local service_method_feature_map = { 202 local service_method_feature_map = {
191 add_subscription = { "subscribe", "subscription-options" }; 203 add_subscription = { "subscribe", "subscription-options" };
256 if node_obj.config then 268 if node_obj.config then
257 reply:add_child(node_metadata_form:form({ 269 reply:add_child(node_metadata_form:form({
258 ["pubsub#title"] = node_obj.config.title; 270 ["pubsub#title"] = node_obj.config.title;
259 ["pubsub#description"] = node_obj.config.description; 271 ["pubsub#description"] = node_obj.config.description;
260 ["pubsub#type"] = node_obj.config.payload_type; 272 ["pubsub#type"] = node_obj.config.payload_type;
273 ["pubsub#access_model"] = node_obj.config.access_model;
274 ["pubsub#publish_model"] = node_obj.config.publish_model;
261 }, "result")); 275 }, "result"));
262 end 276 end
263 end 277 end
264 278
265 function _M.handle_disco_items_node(event, service) 279 function _M.handle_disco_items_node(event, service)
316 330
317 local data = st.stanza("items", { node = node }); 331 local data = st.stanza("items", { node = node });
318 for _, id in ipairs(results) do 332 for _, id in ipairs(results) do
319 data:add_child(results[id]); 333 data:add_child(results[id]);
320 end 334 end
321 local reply; 335 local reply = st.reply(stanza)
322 if data then 336 :tag("pubsub", { xmlns = xmlns_pubsub })
323 reply = st.reply(stanza) 337 :add_child(data);
324 :tag("pubsub", { xmlns = xmlns_pubsub })
325 :add_child(data);
326 else
327 reply = pubsub_error_reply(stanza, "item-not-found");
328 end
329 origin.send(reply); 338 origin.send(reply);
330 return true; 339 return true;
331 end 340 end
332 341
333 function handlers.get_subscriptions(origin, stanza, subscriptions, service) 342 function handlers.get_subscriptions(origin, stanza, subscriptions, service)
631 origin.send(reply); 640 origin.send(reply);
632 return true; 641 return true;
633 end 642 end
634 643
635 function handlers.owner_set_purge(origin, stanza, purge, service) 644 function handlers.owner_set_purge(origin, stanza, purge, service)
636 local node, notify = purge.attr.node, purge.attr.notify; 645 local node = purge.attr.node;
637 notify = (notify == "1") or (notify == "true");
638 local reply; 646 local reply;
639 if not node then 647 if not node then
640 origin.send(pubsub_error_reply(stanza, "nodeid-required")); 648 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
641 return true; 649 return true;
642 end 650 end
643 local ok, ret = service:purge(node, stanza.attr.from, notify); 651 local ok, ret = service:purge(node, stanza.attr.from, true);
644 if ok then 652 if ok then
645 reply = st.reply(stanza); 653 reply = st.reply(stanza);
646 else 654 else
647 reply = pubsub_error_reply(stanza, ret); 655 reply = pubsub_error_reply(stanza, ret);
648 end 656 end
786 item:add_child(payload); 794 item:add_child(payload);
787 return item; 795 return item;
788 end 796 end
789 797
790 local function archive_itemstore(archive, config, user, node) 798 local function archive_itemstore(archive, config, user, node)
791 module:log("debug", "Creation of itemstore for node %s with config %s", node, config); 799 module:log("debug", "Creation of archive itemstore for node %s with config %q", node, config);
792 local get_set = {}; 800 local get_set = {};
793 local max_items = config["max_items"]; 801 local max_items = config["max_items"];
794 function get_set:items() -- luacheck: ignore 212/self 802 function get_set:items() -- luacheck: ignore 212/self
795 local data, err = archive:find(user, { 803 local data, err = archive:find(user, {
796 limit = tonumber(max_items); 804 limit = tonumber(max_items);
800 module:log("error", "Unable to get items: %s", err); 808 module:log("error", "Unable to get items: %s", err);
801 return true; 809 return true;
802 end 810 end
803 module:log("debug", "Listed items %s", data); 811 module:log("debug", "Listed items %s", data);
804 return it.reverse(function() 812 return it.reverse(function()
813 -- luacheck: ignore 211/when
805 local id, payload, when, publisher = data(); 814 local id, payload, when, publisher = data();
806 if id == nil then 815 if id == nil then
807 return; 816 return;
808 end 817 end
809 local item = create_encapsulating_item(id, payload, publisher); 818 local item = create_encapsulating_item(id, payload, publisher);
861 local item = self:get(nil); 870 local item = self:get(nil);
862 if item then 871 if item then
863 return item.attr.id, item; 872 return item.attr.id, item;
864 end 873 end
865 end 874 end
866 return setmetatable(get_set, archive); 875 return get_set;
867 end 876 end
868 _M.archive_itemstore = archive_itemstore; 877 _M.archive_itemstore = archive_itemstore;
869 878
870 return _M; 879 return _M;