Changeset

3823:31b1797a78e1

mod_rest: Add support for XEP-XXXX: User-defined Data Transfer
author Kim Alvefur <zash@zash.se>
date Thu, 02 Jan 2020 09:30:47 +0100
parents 3822:f0a1d113dce4
children 3824:a0b8ec44a36b
files mod_rest/jsonmap.lib.lua
diffstat 1 files changed, 22 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/jsonmap.lib.lua	Thu Jan 02 09:28:18 2020 +0100
+++ b/mod_rest/jsonmap.lib.lua	Thu Jan 02 09:30:47 2020 +0100
@@ -1,5 +1,6 @@
 local array = require "util.array";
 local jid = require "util.jid";
+local json = require "util.json";
 local st = require "util.stanza";
 local xml = require "util.xml";
 
@@ -103,6 +104,27 @@
 			end
 		end;
 	};
+
+	-- XEP-XXXX: User-defined Data Transfer
+	payload = {"func", "urn:xmpp:udt:0", "payload",
+		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;
+		function (s)
+			if type(s) == "table" then
+				return st.stanza("payload", { xmlns = "urn:xmpp:udt:0", datatype = s.datatype })
+					:tag("json", { xmlns = "urn:xmpp:json:0" }):text(json.encode(s.data));
+			end;
+		end
+	};
+
 };
 
 local implied_kinds = {