comparison mod_pubsub_post/mod_pubsub_post.lua @ 3017:8e48c0b233e0

mod_pubsub_post: Factor out the 'actor' into an argument
author Kim Alvefur <zash@zash.se>
date Sun, 20 May 2018 02:56:48 +0200
parents 3f4e2340bfdc
children 727a8beeb5c3
comparison
equal deleted inserted replaced
3016:3f4e2340bfdc 3017:8e48c0b233e0
13 ["item-not-found"] = 404; 13 ["item-not-found"] = 404;
14 ["internal-server-error"] = 500; 14 ["internal-server-error"] = 500;
15 ["conflict"] = 409; 15 ["conflict"] = 409;
16 }; 16 };
17 17
18 local function publish_payload(node, item_id, payload) 18 local function publish_payload(node, actor, item_id, payload)
19 local post_item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = item_id, }) 19 local post_item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = item_id, })
20 :add_child(payload); 20 :add_child(payload);
21 local ok, err = pubsub_service:publish(node, true, item_id, post_item); 21 local ok, err = pubsub_service:publish(node, actor, item_id, post_item);
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 27 end
28 28
29 local function handle_json(node, data) 29 local function handle_json(node, actor, data)
30 local parsed, err = json.decode(data); 30 local parsed, err = json.decode(data);
31 if not parsed then 31 if not parsed then
32 return { status_code = 400; body = tostring(err); } 32 return { status_code = 400; body = tostring(err); }
33 end 33 end
34 if type(parsed) ~= "table" then 34 if type(parsed) ~= "table" then
35 return { status_code = 400; body = "object or array expected"; }; 35 return { status_code = 400; body = "object or array expected"; };
36 end 36 end
37 local wrapper = st.stanza("json", { xmlns="urn:xmpp:json:0" }):text(data); 37 local wrapper = st.stanza("json", { xmlns="urn:xmpp:json:0" }):text(data);
38 return publish_payload(node, data.id or "current", wrapper); 38 return publish_payload(node, actor, data.id or "current", wrapper);
39 end 39 end
40 40
41 local function publish_atom(node, feed) 41 local function publish_atom(node, actor, feed)
42 for entry in feed:childtags("entry") do 42 for entry in feed:childtags("entry") do
43 local item_id = entry:get_child_text("id"); 43 local item_id = entry:get_child_text("id");
44 if not item_id then 44 if not item_id then
45 item_id = uuid_generate(); 45 item_id = uuid_generate();
46 entry:tag("id"):text(item_id):up(); 46 entry:tag("id"):text(item_id):up();
47 end 47 end
48 if not entry:get_child_text("published") then 48 if not entry:get_child_text("published") then
49 entry:tag("published"):text(timestamp_generate()):up(); 49 entry:tag("published"):text(timestamp_generate()):up();
50 end 50 end
51 local resp = publish_payload(node, item_id, entry); 51 local resp = publish_payload(node, actor, item_id, entry);
52 if resp ~= 202 then return resp; end 52 if resp ~= 202 then return resp; end
53 end 53 end
54 return 202; 54 return 202;
55 end 55 end
56 56
57 local function handle_xml(node, payload) 57 local function handle_xml(node, actor, payload)
58 local xmlpayload, err = xml.parse(payload); 58 local xmlpayload, err = xml.parse(payload);
59 if not xmlpayload then 59 if not xmlpayload then
60 module:log("debug", "XML parse error: %s\n%q", err, payload); 60 module:log("debug", "XML parse error: %s\n%q", err, payload);
61 return { status_code = 400, body = tostring(err) }; 61 return { status_code = 400, body = tostring(err) };
62 end 62 end
63 if xmlpayload.attr.xmlns == "http://www.w3.org/2005/Atom" and xmlpayload.name == "feed" then 63 if xmlpayload.attr.xmlns == "http://www.w3.org/2005/Atom" and xmlpayload.name == "feed" then
64 return publish_atom(node, xmlpayload); 64 return publish_atom(node, actor, xmlpayload);
65 else 65 else
66 return publish_payload(node, "current", xmlpayload); 66 return publish_payload(node, actor, "current", xmlpayload);
67 end 67 end
68 end 68 end
69 69
70 function handle_POST(event, path) 70 function handle_POST(event, path)
71 local request = event.request; 71 local request = event.request;
72 module:log("debug", "Handling POST: \n%s\n", tostring(request.body)); 72 module:log("debug", "Handling POST: \n%s\n", tostring(request.body));
73 73
74 local content_type = request.headers.content_type or "application/octet-stream"; 74 local content_type = request.headers.content_type or "application/octet-stream";
75 local actor = true;
75 76
76 if content_type == "application/xml" or content_type:sub(-4) == "+xml" then 77 if content_type == "application/xml" or content_type:sub(-4) == "+xml" then
77 return handle_xml(path, request.body); 78 return handle_xml(path, actor, request.body);
78 elseif content_type == "application/json" or content_type:sub(-5) == "+json" then 79 elseif content_type == "application/json" or content_type:sub(-5) == "+json" then
79 return handle_json(path, request.body); 80 return handle_json(path, actor, request.body);
80 end 81 end
81 82
82 module:log("debug", "Unsupported content-type: %q", content_type); 83 module:log("debug", "Unsupported content-type: %q", content_type);
83 return 415; 84 return 415;
84 end 85 end