diff 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
line wrap: on
line diff
--- a/mod_atom/mod_atom.lua	Sun Aug 19 17:53:05 2018 +0100
+++ b/mod_atom/mod_atom.lua	Mon Aug 20 12:39:37 2018 +0200
@@ -1,47 +1,34 @@
 -- HTTP Access to PEP -> microblog
 -- By Kim Alvefur <zash@zash.se>
 
-module:depends"http";
-module:depends"pep";
+local mod_pep = 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 "<?xml version='1.0' encoding='utf-8'?>" .. tostring(feed) .. "\n";
-end
-
+module:depends("http")
 module:provides("http", {
 	route = {
-		["GET /*"] = handle_request;
-	};
+		["GET /*"] = function (event, user)
+			local actor = event.request.ip;
+
+			user = nodeprep(user);
+			if not user then return 400; end
+
+			local pubsub_service = mod_pep.get_pep_service(user);
+			local ok, items = pubsub_service:get_items("urn:xmpp:microblog:0", actor);
+			if ok then
+				event.response.headers.content_type = "application/atom+xml";
+				local feed = st.stanza("feed", { xmlns = "http://www.w3.org/2005/Atom" });
+				for i = #items, 1, -1 do
+					feed:add_direct_child(items[items[i]].tags[1]);
+				end
+				return tostring(feed);
+			elseif items == "forbidden" then
+				return 403;
+			elseif items == "item-not-found" then
+				return 404;
+			end
+		end;
+	}
 });