# HG changeset patch # User Kim Alvefur # Date 1472505065 -7200 # Node ID 4915b8223b07f73bb4ca9b58df7b4dbbe7b043d7 # Parent 144b74caa5efb557412b465e1f1d3339f533917a mod_atom: Expose Microbloging PEP data over HTTP diff -r 144b74caa5ef -r 4915b8223b07 mod_atom/mod_atom.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_atom/mod_atom.lua Mon Aug 29 23:11:05 2016 +0200 @@ -0,0 +1,47 @@ +-- HTTP Access to PEP -> microblog +-- By Kim Alvefur + +module:depends"http"; +module:depends"pep"; +local nodeprep = require "util.encodings".stringprep.nodeprep; +local st = require "util.stanza"; +local host, hosts = module.host, hosts; + +local function handle_request(event, path) + local response = event.response; + + local user = nodeprep(path); + if not user then return 400 end + local jid = user .. "@" .. host; + + local pep_data = hosts[host].modules.pep.module.save(); + if not pep_data.data[jid] or + not pep_data.data[jid]["urn:xmpp:microblog:0"] then + return 404; + end + + local microblogdata = pep_data.data[jid]["urn:xmpp:microblog:0"][2]:get_child("entry", "http://www.w3.org/2005/Atom"); + if not microblogdata then return 404; end + local feed = st.stanza("feed", { xmlns="http://www.w3.org/2005/Atom" } ); + local source = microblogdata:get_child("source"); + if source then + for i = 1,#source do + feed:add_child(source[i]):up(); + end + for i = 1,#microblogdata do + if microblogdata[i].name == "source" then + table.remove(microblogdata, i); + break + end + end + end + feed:add_child(microblogdata); + response.headers.content_type = "application/atom+xml"; + return "" .. tostring(feed) .. "\n"; +end + +module:provides("http", { + route = { + ["GET /*"] = handle_request; + }; +});