comparison mod_rest/jsonmap.lib.lua @ 4021:1925d63eec6b

mod_rest/jsonmap: Derive stanza @type from certain payloads Because I forget type=set every single time with ad-hoc commands.
author Kim Alvefur <zash@zash.se>
date Sat, 16 May 2020 18:30:01 +0200
parents 2c6d5734ae04
children 270cd50852be
comparison
equal deleted inserted replaced
4020:4b47c8eeca22 4021:1925d63eec6b
423 priority = "presence", 423 priority = "presence",
424 show = "presence", 424 show = "presence",
425 status = "presence", 425 status = "presence",
426 } 426 }
427 427
428 local implied_types = {
429 command = "set",
430 }
431
428 local kind_by_type = { 432 local kind_by_type = {
429 get = "iq", set = "iq", result = "iq", 433 get = "iq", set = "iq", result = "iq",
430 normal = "message", chat = "message", headline = "message", groupchat = "message", 434 normal = "message", chat = "message", headline = "message", groupchat = "message",
431 available = "presence", unavailable = "presence", 435 available = "presence", unavailable = "presence",
432 subscribe = "presence", unsubscribe = "presence", 436 subscribe = "presence", unsubscribe = "presence",
505 509
506 local function json2st(t) 510 local function json2st(t)
507 if type(t) ~= "table" or not str(next(t)) then 511 if type(t) ~= "table" or not str(next(t)) then
508 return nil, "invalid-json"; 512 return nil, "invalid-json";
509 end 513 end
510 local kind = str(t.kind) or kind_by_type[str(t.type)]; 514 local t_type = str(t.type);
515 if t_type == nil then
516 for k, implied in pairs(implied_types) do
517 if t[k] then
518 t_type = implied;
519 end
520 end
521 end
522 local kind = str(t.kind) or kind_by_type[t_type];
511 if not kind then 523 if not kind then
512 for k, implied in pairs(implied_kinds) do 524 for k, implied in pairs(implied_kinds) do
513 if t[k] then 525 if t[k] then
514 kind = implied; 526 kind = implied;
515 break 527 break
516 end 528 end
517 end 529 end
518 end 530 end
519 531
532 if t_type == "available" then
533 t_type = nil;
534 end
535
520 local s = st.stanza(kind or "message", { 536 local s = st.stanza(kind or "message", {
521 type = t.type ~= "available" and str(t.type) or nil, 537 type = t_type;
522 to = str(t.to) and jid.prep(t.to); 538 to = str(t.to) and jid.prep(t.to);
523 from = str(t.to) and jid.prep(t.from); 539 from = str(t.to) and jid.prep(t.from);
524 id = str(t.id), 540 id = str(t.id),
525 }); 541 });
526 542