Software / code / prosody-modules
Comparison
mod_rest/jsonmap.lib.lua @ 4916:3dc8e329d233
mod_rest: Move most of XEP-0432 handling into JSON mapping schema
The pre- and post-processing is still needed to encode/decode the JSON
since util.datamapper can't (currently) do this.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 07 Apr 2022 15:39:57 +0200 |
| parent | 4915:1d231fb827d3 |
| child | 4927:77b7e1322281 |
comparison
equal
deleted
inserted
replaced
| 4915:1d231fb827d3 | 4916:3dc8e329d233 |
|---|---|
| 207 json2st = function (s) | 207 json2st = function (s) |
| 208 if type(s) == "string" then | 208 if type(s) == "string" then |
| 209 return st.stanza("x", { xmlns = "jabber:x:oob" }):text_tag("url", s); | 209 return st.stanza("x", { xmlns = "jabber:x:oob" }):text_tag("url", s); |
| 210 end | 210 end |
| 211 end; | 211 end; |
| 212 }; | |
| 213 | |
| 214 -- XEP-0432: Simple JSON Messaging | |
| 215 payload = { type = "func", xmlns = "urn:xmpp:json-msg:0", tagname = "payload", | |
| 216 st2json = function (s) | |
| 217 local rawjson = s:get_child_text("json", "urn:xmpp:json:0"); | |
| 218 if not rawjson then return nil, "missing-json-payload"; end | |
| 219 local parsed, err = json.decode(rawjson); | |
| 220 if not parsed then return nil, err; end | |
| 221 return { | |
| 222 datatype = s.attr.datatype; | |
| 223 data = parsed; | |
| 224 }; | |
| 225 end; | |
| 226 json2st = function (s) | |
| 227 if type(s) == "table" then | |
| 228 return st.stanza("payload", { xmlns = "urn:xmpp:json-msg:0", datatype = s.datatype }) | |
| 229 :tag("json", { xmlns = "urn:xmpp:json:0" }):text(json.encode(s.data)); | |
| 230 end; | |
| 231 end | |
| 232 }; | 212 }; |
| 233 | 213 |
| 234 -- XEP-0004: Data Forms | 214 -- XEP-0004: Data Forms |
| 235 dataform = { | 215 dataform = { |
| 236 -- Generic and complete dataforms mapping | 216 -- Generic and complete dataforms mapping |
| 448 by = error and error.attr.by or nil, | 428 by = error and error.attr.by or nil, |
| 449 }; | 429 }; |
| 450 return t; | 430 return t; |
| 451 end | 431 end |
| 452 | 432 |
| 433 if type(t.payload) == "table" then | |
| 434 if type(t.payload.data) == "string" then | |
| 435 local data, err = json.decode(t.payload.data); | |
| 436 if err then | |
| 437 return nil, err; | |
| 438 else | |
| 439 t.payload.data = data; | |
| 440 end | |
| 441 else | |
| 442 return nil, "invalid payload.data"; | |
| 443 end | |
| 444 end | |
| 445 | |
| 453 for _, tag in ipairs(s.tags) do | 446 for _, tag in ipairs(s.tags) do |
| 454 local prefix = "{" .. (tag.attr.xmlns or "jabber:client") .. "}"; | 447 local prefix = "{" .. (tag.attr.xmlns or "jabber:client") .. "}"; |
| 455 local mapping = byxmlname[prefix .. tag.name]; | 448 local mapping = byxmlname[prefix .. tag.name]; |
| 456 if not mapping then | 449 if not mapping then |
| 457 mapping = byxmlname[prefix]; | 450 mapping = byxmlname[prefix]; |
| 534 archive["before"] = nil; | 527 archive["before"] = nil; |
| 535 archive["max"] = nil; | 528 archive["max"] = nil; |
| 536 end | 529 end |
| 537 end | 530 end |
| 538 | 531 |
| 532 if type(t.payload) == "table" then | |
| 533 t.payload.data = json.encode(t.payload.data); | |
| 534 end | |
| 535 | |
| 539 local s = map.unparse(schema, { [kind or "message"] = t }).tags[1]; | 536 local s = map.unparse(schema, { [kind or "message"] = t }).tags[1]; |
| 540 | 537 |
| 541 s.attr.type = t_type; | 538 s.attr.type = t_type; |
| 542 s.attr.to = str(t.to) and jid.prep(t.to); | 539 s.attr.to = str(t.to) and jid.prep(t.to); |
| 543 s.attr.from = str(t.to) and jid.prep(t.from); | 540 s.attr.from = str(t.to) and jid.prep(t.from); |