Comparison

mod_rest/jsonmap.lib.lua @ 3878:9a3dfe0bf9fd

mod_rest: Add JSON mapping for dataform (XEP-0004)
author Kim Alvefur <zash@zash.se>
date Tue, 04 Feb 2020 22:06:19 +0100
parent 3877:562b34050561
child 3880:44c2d36c40a4
comparison
equal deleted inserted replaced
3877:562b34050561 3878:9a3dfe0bf9fd
1 local array = require "util.array"; 1 local array = require "util.array";
2 local jid = require "util.jid"; 2 local jid = require "util.jid";
3 local json = require "util.json"; 3 local json = require "util.json";
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
7 -- Reused in many XEPs so declared up here
8 local dataform = {
9 "func", "jabber:x:data", "x",
10 function (s)
11 local fields = array();
12 local form = {
13 type = s.attr.type;
14 title = s:get_child_text("title");
15 instructions = s:get_child_text("instructions");
16 fields = fields;
17 };
18 for field in s:childtags("field") do
19 local i = {
20 var = field.attr.var;
21 type = field.attr.type;
22 label = field.attr.label;
23 desc = field:get_child_text("desc");
24 required = field:get_child("required") and true or nil;
25 value = field:get_child_text("value");
26 };
27 if field.attr.type == "jid-multi" or field.attr.type == "list-multi" or field.attr.type == "text-multi" then
28 local value = array();
29 for v in field:childtags("value") do
30 value:push(v:get_text());
31 end
32 if field.attr.type == "text-multi" then
33 i.value = value:concat("\n");
34 else
35 i.value = value;
36 end
37 end
38 if field.attr.type == "list-single" or field.attr.type == "list-multi" then
39 local options = array();
40 for o in field:childtags("option") do
41 options:push({ label = o.attr.label, value = o:get_child_text("value") });
42 end
43 i.options = options;
44 end
45 fields:push(i);
46 end
47 return form;
48 end;
49 function (x)
50 -- TODO
51 end;
52 };
6 53
7 local simple_types = { 54 local simple_types = {
8 -- basic message 55 -- basic message
9 body = "text_tag", 56 body = "text_tag",
10 subject = "text_tag", 57 subject = "text_tag",
135 sessionid = s.attr.sessionid, 182 sessionid = s.attr.sessionid,
136 status = s.attr.status, 183 status = s.attr.status,
137 }; 184 };
138 local actions = s:get_child("actions"); 185 local actions = s:get_child("actions");
139 local note = s:get_child("note"); 186 local note = s:get_child("note");
140 -- TODO -- local form = s:get_child("x", "jabber:x:data"); 187 local form = s:get_child("x", "jabber:x:data");
141 if actions then 188 if actions then
142 cmd.actions = { 189 cmd.actions = {
143 execute = actions.attr.execute, 190 execute = actions.attr.execute,
144 }; 191 };
145 for action in actions:childtags() do 192 for action in actions:childtags() do
148 elseif note then 195 elseif note then
149 cmd.note = { 196 cmd.note = {
150 type = note.attr.type; 197 type = note.attr.type;
151 text = note:get_text(); 198 text = note:get_text();
152 }; 199 };
200 end
201 if form then
202 cmd.form = dataform[4](form);
153 end 203 end
154 return cmd; 204 return cmd;
155 end; 205 end;
156 function (s) 206 function (s)
157 if type(s) == "table" and s ~= json.null then 207 if type(s) == "table" and s ~= json.null then