comparison mod_rest/jsonmap.lib.lua @ 3888:04ea96a0488d

mod_rest: Allow passing form data in a more compact format
author Kim Alvefur <zash@zash.se>
date Fri, 07 Feb 2020 22:30:38 +0100
parents b64b08b7bf8e
children 59765d1bb6dc
comparison
equal deleted inserted replaced
3887:3d0e8e32453c 3888:04ea96a0488d
4 local st = require "util.stanza"; 4 local st = require "util.stanza";
5 local xml = require "util.xml"; 5 local xml = require "util.xml";
6 6
7 -- Reused in many XEPs so declared up here 7 -- Reused in many XEPs so declared up here
8 local dataform = { 8 local dataform = {
9 -- Generic and complete dataforms mapping
9 "func", "jabber:x:data", "x", 10 "func", "jabber:x:data", "x",
10 function (s) 11 function (s)
11 local fields = array(); 12 local fields = array();
12 local form = { 13 local form = {
13 type = s.attr.type; 14 type = s.attr.type;
87 return form; 88 return form;
88 end 89 end
89 end; 90 end;
90 }; 91 };
91 92
93 local function formdata(s,t)
94 local form = st.stanza("x", { xmlns = "jabber:x:data", type = t });
95 for k,v in pairs(s) do
96 form:tag("field", { var = k });
97 if type(v) == "string" then
98 form:text_tag("value", v);
99 elseif type(v) == "table" then
100 for _, v_ in ipairs(v) do
101 form:text_tag("value", v_);
102 end
103 end
104 end
105 return form;
106 end
107
92 local simple_types = { 108 local simple_types = {
93 -- top level stanza attributes 109 -- top level stanza attributes
94 -- needed here to mark them as known fields 110 -- needed here to mark them as known fields
95 kind = "attr", 111 kind = "attr",
96 type = "attr", 112 type = "attr",
275 elseif type(s.note) == "table" then 291 elseif type(s.note) == "table" then
276 cmd:text_tag("note", s.note.text, { type = s.note.type }); 292 cmd:text_tag("note", s.note.text, { type = s.note.type });
277 end 293 end
278 if s.form then 294 if s.form then
279 cmd:add_child(dataform[5](s.form)); 295 cmd:add_child(dataform[5](s.form));
296 elseif s.data then
297 cmd:add_child(formdata(s.data));
280 end 298 end
281 return cmd; 299 return cmd;
282 elseif type(s) == "string" then -- assume node 300 elseif type(s) == "string" then -- assume node
283 return st.stanza("command", { xmlns = "http://jabber.org/protocol/commands", node = s }); 301 return st.stanza("command", { xmlns = "http://jabber.org/protocol/commands", node = s });
284 end 302 end