# HG changeset patch # User Kim Alvefur # Date 1526767233 -7200 # Node ID 3f4e2340bfdca323739e8f0dea363ba728d990d6 # Parent 338b7c808ecc03949ff44c4a6e18324a2becb6e8 mod_pubsub_post: Add support for publishing arbitrary JSON Payloads are wrapped in XEP-0335: JSON Containers diff -r 338b7c808ecc -r 3f4e2340bfdc mod_pubsub_post/mod_pubsub_post.lua --- 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);