comparison mod_pubsub_post/mod_pubsub_post.lua @ 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
comparison
equal deleted inserted replaced
3015:338b7c808ecc 3016:3f4e2340bfdc
22 module:log("debug", ":publish(%q, true, %q, %s) -> %q", node, item_id, payload:top_tag(), err or ""); 22 module:log("debug", ":publish(%q, true, %q, %s) -> %q", node, item_id, payload:top_tag(), err or "");
23 if not ok then 23 if not ok then
24 return error_mapping[err] or 500; 24 return error_mapping[err] or 500;
25 end 25 end
26 return 202; 26 return 202;
27 end
28
29 local function handle_json(node, data)
30 local parsed, err = json.decode(data);
31 if not parsed then
32 return { status_code = 400; body = tostring(err); }
33 end
34 if type(parsed) ~= "table" then
35 return { status_code = 400; body = "object or array expected"; };
36 end
37 local wrapper = st.stanza("json", { xmlns="urn:xmpp:json:0" }):text(data);
38 return publish_payload(node, data.id or "current", wrapper);
27 end 39 end
28 40
29 local function publish_atom(node, feed) 41 local function publish_atom(node, feed)
30 for entry in feed:childtags("entry") do 42 for entry in feed:childtags("entry") do
31 local item_id = entry:get_child_text("id"); 43 local item_id = entry:get_child_text("id");
61 73
62 local content_type = request.headers.content_type or "application/octet-stream"; 74 local content_type = request.headers.content_type or "application/octet-stream";
63 75
64 if content_type == "application/xml" or content_type:sub(-4) == "+xml" then 76 if content_type == "application/xml" or content_type:sub(-4) == "+xml" then
65 return handle_xml(path, request.body); 77 return handle_xml(path, request.body);
78 elseif content_type == "application/json" or content_type:sub(-5) == "+json" then
79 return handle_json(path, request.body);
66 end 80 end
67 81
68 module:log("debug", "Unsupported content-type: %q", content_type); 82 module:log("debug", "Unsupported content-type: %q", content_type);
69 return 415; 83 return 415;
70 end 84 end