Comparison

plugins/mod_pubsub/pubsub.lib.lua @ 9037:e3c8274427d3

mod_pubsub: Refactor translation to/from XEP-0060-specific node configuration format
author Kim Alvefur <zash@zash.se>
date Sat, 14 Jul 2018 18:42:36 +0200
parent 9033:f1b6efd5b379
child 9038:fd053fcaf9bc
comparison
equal deleted inserted replaced
9036:bb4dcc555091 9037:e3c8274427d3
36 end 36 end
37 return reply; 37 return reply;
38 end 38 end
39 _M.pubsub_error_reply = pubsub_error_reply; 39 _M.pubsub_error_reply = pubsub_error_reply;
40 40
41 -- util.pubsub is meant to be agnostic to XEP-0060
42 local function config_to_xep0060(node_config)
43 return {
44 ["pubsub#title"] = node_config["title"];
45 ["pubsub#description"] = node_config["description"];
46 ["pubsub#max_items"] = tostring(node_config["max_items"]);
47 ["pubsub#persist_items"] = node_config["persist_items"];
48 ["pubsub#notification_type"] = node_config["notification_type"];
49 ["pubsub#include_body"] = node_config["include_body"];
50 }
51 end
52
53 local function config_from_xep0060(config)
54 return {
55 ["title"] = config["pubsub#title"];
56 ["description"] = config["pubsub#description"];
57 ["max_items"] = tonumber(config["pubsub#max_items"]);
58 ["persist_items"] = config["pubsub#persist_items"];
59 ["notification_type"] = config["pubsub#notification_type"];
60 ["include_body"] = config["pubsub#include_body"];
61 }
62 end
63
41 local node_config_form = dataform { 64 local node_config_form = dataform {
42 { 65 {
43 type = "hidden"; 66 type = "hidden";
44 name = "FORM_TYPE"; 67 name = "FORM_TYPE";
45 value = "http://jabber.org/protocol/pubsub#node_config"; 68 value = "http://jabber.org/protocol/pubsub#node_config";
162 return; 185 return;
163 end 186 end
164 event.exists = true; 187 event.exists = true;
165 reply:tag("identity", { category = "pubsub", type = "leaf" }):up(); 188 reply:tag("identity", { category = "pubsub", type = "leaf" }):up();
166 if node_obj.config then 189 if node_obj.config then
167 reply:add_child(node_metadata_form:form({ 190 reply:add_child(node_metadata_form:form(config_to_xep0060(node_obj.config), "result"));
168 ["pubsub#title"] = node_obj.config.title;
169 ["pubsub#description"] = node_obj.config.description;
170 }, "result"));
171 end 191 end
172 end 192 end
173 193
174 function _M.handle_disco_items_node(event, service) 194 function _M.handle_disco_items_node(event, service)
175 local stanza, reply, node = event.stanza, event.reply, event.node; 195 local stanza, reply, node = event.stanza, event.reply, event.node;
319 local form_data, err = node_config_form:data(config_form); 339 local form_data, err = node_config_form:data(config_form);
320 if not form_data then 340 if not form_data then
321 origin.send(st.error_reply(stanza, "modify", "bad-request", err)); 341 origin.send(st.error_reply(stanza, "modify", "bad-request", err));
322 return true; 342 return true;
323 end 343 end
324 config = { 344 config = config_from_xep0060(form_data);
325 ["max_items"] = tonumber(form_data["pubsub#max_items"]);
326 ["persist_items"] = form_data["pubsub#persist_items"];
327 ["notification_type"] = form_data["pubsub#notification_type"];
328 ["include_body"] = form_data["pubsub#include_body"];
329 };
330 end 345 end
331 if node then 346 if node then
332 ok, ret = service:create(node, stanza.attr.from, config); 347 ok, ret = service:create(node, stanza.attr.from, config);
333 if ok then 348 if ok then
334 reply = st.reply(stanza); 349 reply = st.reply(stanza);
507 origin.send(pubsub_error_reply(stanza, "item-not-found")); 522 origin.send(pubsub_error_reply(stanza, "item-not-found"));
508 return true; 523 return true;
509 end 524 end
510 525
511 local node_config = node_obj.config; 526 local node_config = node_obj.config;
512 local pubsub_form_data = { 527 local pubsub_form_data = config_to_xep0060(node_config);
513 ["pubsub#title"] = node_config["title"];
514 ["pubsub#description"] = node_config["description"];
515 ["pubsub#max_items"] = tostring(node_config["max_items"]);
516 ["pubsub#persist_items"] = node_config["persist_items"];
517 ["pubsub#notification_type"] = node_config["notification_type"];
518 ["pubsub#include_body"] = node_config["include_body"];
519 }
520 local reply = st.reply(stanza) 528 local reply = st.reply(stanza)
521 :tag("pubsub", { xmlns = xmlns_pubsub_owner }) 529 :tag("pubsub", { xmlns = xmlns_pubsub_owner })
522 :tag("configure", { node = node }) 530 :tag("configure", { node = node })
523 :add_child(node_config_form:form(pubsub_form_data)); 531 :add_child(node_config_form:form(pubsub_form_data));
524 origin.send(reply); 532 origin.send(reply);
543 local form_data, err = node_config_form:data(config_form); 551 local form_data, err = node_config_form:data(config_form);
544 if not form_data then 552 if not form_data then
545 origin.send(st.error_reply(stanza, "modify", "bad-request", err)); 553 origin.send(st.error_reply(stanza, "modify", "bad-request", err));
546 return true; 554 return true;
547 end 555 end
548 local new_config = { 556 local new_config = config_from_xep0060(form_data);
549 ["title"] = form_data["pubsub#title"];
550 ["description"] = form_data["pubsub#description"];
551 ["max_items"] = tonumber(form_data["pubsub#max_items"]);
552 ["persist_items"] = form_data["pubsub#persist_items"];
553 ["notification_type"] = form_data["pubsub#notification_type"];
554 ["include_body"] = form_data["pubsub#include_body"];
555 };
556 local ok, err = service:set_node_config(node, stanza.attr.from, new_config); 557 local ok, err = service:set_node_config(node, stanza.attr.from, new_config);
557 if not ok then 558 if not ok then
558 origin.send(pubsub_error_reply(stanza, err)); 559 origin.send(pubsub_error_reply(stanza, err));
559 return true; 560 return true;
560 end 561 end
561 origin.send(st.reply(stanza)); 562 origin.send(st.reply(stanza));
562 return true; 563 return true;
563 end 564 end
564 565
565 function handlers.owner_get_default(origin, stanza, default, service) -- luacheck: ignore 212/default 566 function handlers.owner_get_default(origin, stanza, default, service) -- luacheck: ignore 212/default
566 local pubsub_form_data = { 567 local pubsub_form_data = config_to_xep0060(service.node_defaults);
567 ["pubsub#max_items"] = tostring(service.node_defaults["max_items"]);
568 ["pubsub#persist_items"] = service.node_defaults["persist_items"]
569 }
570 local reply = st.reply(stanza) 568 local reply = st.reply(stanza)
571 :tag("pubsub", { xmlns = xmlns_pubsub_owner }) 569 :tag("pubsub", { xmlns = xmlns_pubsub_owner })
572 :tag("default") 570 :tag("default")
573 :add_child(node_config_form:form(pubsub_form_data)); 571 :add_child(node_config_form:form(pubsub_form_data));
574 origin.send(reply); 572 origin.send(reply);