Changeset

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
parents 4915:1d231fb827d3
children 4917:9d0ec61c70a1
files mod_rest/jsonmap.lib.lua mod_rest/res/schema-xmpp.json
diffstat 2 files changed, 39 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/jsonmap.lib.lua	Thu Apr 07 15:39:11 2022 +0200
+++ b/mod_rest/jsonmap.lib.lua	Thu Apr 07 15:39:57 2022 +0200
@@ -211,26 +211,6 @@
 		end;
 	};
 
-	-- XEP-0432: Simple JSON Messaging
-	payload = { type = "func", xmlns = "urn:xmpp:json-msg:0", tagname = "payload",
-		st2json = function (s)
-			local rawjson = s:get_child_text("json", "urn:xmpp:json:0");
-			if not rawjson then return nil, "missing-json-payload"; end
-			local parsed, err = json.decode(rawjson);
-			if not parsed then return nil, err; end
-			return {
-				datatype = s.attr.datatype;
-				data = parsed;
-			};
-		end;
-		json2st = function (s)
-			if type(s) == "table" then
-				return st.stanza("payload", { xmlns = "urn:xmpp:json-msg:0", datatype = s.datatype })
-				:tag("json", { xmlns = "urn:xmpp:json:0" }):text(json.encode(s.data));
-			end;
-		end
-	};
-
 	-- XEP-0004: Data Forms
 	dataform = {
 		-- Generic and complete dataforms mapping
@@ -450,6 +430,19 @@
 		return t;
 	end
 
+	if type(t.payload) == "table" then
+		if type(t.payload.data) == "string" then
+			local data, err = json.decode(t.payload.data);
+			if err then
+				return nil, err;
+			else
+				t.payload.data = data;
+			end
+		else
+			return nil, "invalid payload.data";
+		end
+	end
+
 	for _, tag in ipairs(s.tags) do
 		local prefix = "{" .. (tag.attr.xmlns or "jabber:client") .. "}";
 		local mapping = byxmlname[prefix .. tag.name];
@@ -536,6 +529,10 @@
 		end
 	end
 
+	if type(t.payload) == "table" then
+		t.payload.data = json.encode(t.payload.data);
+	end
+
 	local s = map.unparse(schema, { [kind or "message"] = t }).tags[1];
 
 	s.attr.type = t_type;
--- a/mod_rest/res/schema-xmpp.json	Thu Apr 07 15:39:11 2022 +0200
+++ b/mod_rest/res/schema-xmpp.json	Thu Apr 07 15:39:57 2022 +0200
@@ -149,6 +149,28 @@
             "namespace" : "http://jabber.org/protocol/nick"
          }
       },
+      "payload" : {
+         "properties" : {
+            "data" : {
+               "format" : "json",
+               "type" : "string",
+               "xml" : {
+                  "text" : true
+               }
+            },
+            "datatype" : {
+               "type" : "string",
+               "xml" : {
+                  "attribute" : true
+               }
+            }
+         },
+         "title" : "XEP-0432: Simple JSON Messaging",
+         "type" : "object",
+         "xml" : {
+            "namespace" : "urn:xmpp:json-msg:0"
+         }
+      },
       "rsm" : {
          "properties" : {
             "after" : {