diff 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
line wrap: on
line diff
--- a/mod_rest/jsonmap.lib.lua	Thu May 14 17:28:38 2020 +0200
+++ b/mod_rest/jsonmap.lib.lua	Sat May 16 18:30:01 2020 +0200
@@ -425,6 +425,10 @@
 	status = "presence",
 }
 
+local implied_types = {
+	command = "set",
+}
+
 local kind_by_type = {
 	get = "iq", set = "iq", result = "iq",
 	normal = "message", chat = "message", headline = "message", groupchat = "message",
@@ -507,7 +511,15 @@
 	if type(t) ~= "table" or not str(next(t)) then
 		return nil, "invalid-json";
 	end
-	local kind = str(t.kind) or kind_by_type[str(t.type)];
+	local t_type = str(t.type);
+	if t_type == nil then
+		for k, implied in pairs(implied_types) do
+			if t[k] then
+				t_type = implied;
+			end
+		end
+	end
+	local kind = str(t.kind) or kind_by_type[t_type];
 	if not kind then
 		for k, implied in pairs(implied_kinds) do
 			if t[k] then
@@ -517,8 +529,12 @@
 		end
 	end
 
+	if t_type == "available" then
+		t_type = nil;
+	end
+
 	local s = st.stanza(kind or "message", {
-		type = t.type ~= "available" and str(t.type) or nil,
+		type = t_type;
 		to = str(t.to) and jid.prep(t.to);
 		from = str(t.to) and jid.prep(t.from);
 		id = str(t.id),