comparison mod_atom/mod_atom.lua @ 2294:4915b8223b07

mod_atom: Expose Microbloging PEP data over HTTP
author Kim Alvefur <zash@zash.se>
date Mon, 29 Aug 2016 23:11:05 +0200
parents
children 4b52cafd5811
comparison
equal deleted inserted replaced
2293:144b74caa5ef 2294:4915b8223b07
1 -- HTTP Access to PEP -> microblog
2 -- By Kim Alvefur <zash@zash.se>
3
4 module:depends"http";
5 module:depends"pep";
6 local nodeprep = require "util.encodings".stringprep.nodeprep;
7 local st = require "util.stanza";
8 local host, hosts = module.host, hosts;
9
10 local function handle_request(event, path)
11 local response = event.response;
12
13 local user = nodeprep(path);
14 if not user then return 400 end
15 local jid = user .. "@" .. host;
16
17 local pep_data = hosts[host].modules.pep.module.save();
18 if not pep_data.data[jid] or
19 not pep_data.data[jid]["urn:xmpp:microblog:0"] then
20 return 404;
21 end
22
23 local microblogdata = pep_data.data[jid]["urn:xmpp:microblog:0"][2]:get_child("entry", "http://www.w3.org/2005/Atom");
24 if not microblogdata then return 404; end
25 local feed = st.stanza("feed", { xmlns="http://www.w3.org/2005/Atom" } );
26 local source = microblogdata:get_child("source");
27 if source then
28 for i = 1,#source do
29 feed:add_child(source[i]):up();
30 end
31 for i = 1,#microblogdata do
32 if microblogdata[i].name == "source" then
33 table.remove(microblogdata, i);
34 break
35 end
36 end
37 end
38 feed:add_child(microblogdata);
39 response.headers.content_type = "application/atom+xml";
40 return "<?xml version='1.0' encoding='utf-8'?>" .. tostring(feed) .. "\n";
41 end
42
43 module:provides("http", {
44 route = {
45 ["GET /*"] = handle_request;
46 };
47 });