Mercurial > prosody-modules
comparison mod_atom/mod_atom.lua @ 3241:4b52cafd5811
mod_atom: Update to the new mod_pep
This takes advantage of the many improvements to PEP added to Prosody
recently. Such as support for multiple items and access control.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 20 Aug 2018 12:39:37 +0200 |
parents | 4915b8223b07 |
children | 119e22ccd64a |
comparison
equal
deleted
inserted
replaced
3240:c30f2cfe9f15 | 3241:4b52cafd5811 |
---|---|
1 -- HTTP Access to PEP -> microblog | 1 -- HTTP Access to PEP -> microblog |
2 -- By Kim Alvefur <zash@zash.se> | 2 -- By Kim Alvefur <zash@zash.se> |
3 | 3 |
4 module:depends"http"; | 4 local mod_pep = module:depends"pep"; |
5 module:depends"pep"; | 5 |
6 local nodeprep = require "util.encodings".stringprep.nodeprep; | 6 local nodeprep = require "util.encodings".stringprep.nodeprep; |
7 local st = require "util.stanza"; | 7 local st = require "util.stanza"; |
8 local host, hosts = module.host, hosts; | |
9 | 8 |
10 local function handle_request(event, path) | 9 module:depends("http") |
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", { | 10 module:provides("http", { |
44 route = { | 11 route = { |
45 ["GET /*"] = handle_request; | 12 ["GET /*"] = function (event, user) |
46 }; | 13 local actor = event.request.ip; |
14 | |
15 user = nodeprep(user); | |
16 if not user then return 400; end | |
17 | |
18 local pubsub_service = mod_pep.get_pep_service(user); | |
19 local ok, items = pubsub_service:get_items("urn:xmpp:microblog:0", actor); | |
20 if ok then | |
21 event.response.headers.content_type = "application/atom+xml"; | |
22 local feed = st.stanza("feed", { xmlns = "http://www.w3.org/2005/Atom" }); | |
23 for i = #items, 1, -1 do | |
24 feed:add_direct_child(items[items[i]].tags[1]); | |
25 end | |
26 return tostring(feed); | |
27 elseif items == "forbidden" then | |
28 return 403; | |
29 elseif items == "item-not-found" then | |
30 return 404; | |
31 end | |
32 end; | |
33 } | |
47 }); | 34 }); |