changeset 3016:3f4e2340bfdc

mod_pubsub_post: Add support for publishing arbitrary JSON Payloads are wrapped in XEP-0335: JSON Containers
author Kim Alvefur <zash@zash.se>
date Sun, 20 May 2018 00:00:33 +0200
parents 338b7c808ecc
children 8e48c0b233e0
files mod_pubsub_post/mod_pubsub_post.lua
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_pubsub_post/mod_pubsub_post.lua	Sun May 20 00:04:41 2018 +0200
+++ b/mod_pubsub_post/mod_pubsub_post.lua	Sun May 20 00:00:33 2018 +0200
@@ -26,6 +26,18 @@
 	return 202;
 end
 
+local function handle_json(node, data)
+	local parsed, err = json.decode(data);
+	if not parsed then
+		return { status_code = 400; body = tostring(err); }
+	end
+	if type(parsed) ~= "table" then
+		return { status_code = 400; body = "object or array expected"; };
+	end
+	local wrapper = st.stanza("json", { xmlns="urn:xmpp:json:0" }):text(data);
+	return publish_payload(node, data.id or "current", wrapper);
+end
+
 local function publish_atom(node, feed)
 	for entry in feed:childtags("entry") do
 		local item_id = entry:get_child_text("id");
@@ -63,6 +75,8 @@
 
 	if content_type == "application/xml" or content_type:sub(-4) == "+xml" then
 		return handle_xml(path, request.body);
+	elseif content_type == "application/json" or content_type:sub(-5) == "+json" then
+		return handle_json(path, request.body);
 	end
 
 	module:log("debug", "Unsupported content-type: %q", content_type);