mod_pubsub, util.pubsub: Support for fetching items

Thu, 25 Nov 2010 21:47:12 +0100

author
Florian Zeitz <florob@babelmonkeys.de>
date
Thu, 25 Nov 2010 21:47:12 +0100
changeset 3641
3603aeb325de
parent 3640
4bc88bb748d1
child 3642
ed80c4c56b9c

mod_pubsub, util.pubsub: Support for fetching items

plugins/mod_pubsub.lua file | annotate | diff | comparison | revisions
util/pubsub.lua file | annotate | diff | comparison | revisions
--- a/plugins/mod_pubsub.lua	Fri Nov 26 05:26:12 2010 +0500
+++ b/plugins/mod_pubsub.lua	Thu Nov 25 21:47:12 2010 +0100
@@ -37,6 +37,24 @@
 	return reply;
 end
 
+function handlers.get_items(origin, stanza, items)
+	local node = items.attr.node;
+	local item = items:get_child("item");
+	local id = item and item.attr.id;
+	local data = st.stanza("items", { node = node });
+	for _, entry in pairs(service:get(node, stanza.attr.from, id)) do
+		data:add_child(entry);
+	end
+	if data then
+		reply = st.reply(stanza)
+			:tag("pubsub", { xmlns = xmlns_pubsub })
+				:add_child(data);
+	else
+		reply = st.error_reply(stanza, "cancel", "item-not-found", "Item could not be found in this node");
+	end
+	return origin.send(reply);
+end
+
 function handlers.set_subscribe(origin, stanza, subscribe)
 	local node, jid = subscribe.attr.node, subscribe.attr.jid;
 	if jid_bare(jid) ~= jid_bare(stanza.attr.from) then
--- a/util/pubsub.lua	Fri Nov 26 05:26:12 2010 +0500
+++ b/util/pubsub.lua	Thu Nov 25 21:47:12 2010 +0100
@@ -31,12 +31,23 @@
 function service:publish(node, actor, id, item)
 	local node_obj = self.nodes[node];
 	if not node_obj then
-		node_obj = { name = node, subscribers = {}, config = {} };
+		node_obj = { name = node, subscribers = {}, config = {}, data = {} };
 		self.nodes[node] = node_obj;
 	end
-	node_obj.data = item;
+	node_obj.data[id] = item;
 	self.cb.broadcaster(node, node_obj.subscribers, item);
 	return true;
 end
 
+function service:get(node, actor, id)
+	local node_obj = self.nodes[node];
+	if node_obj then
+		if id then
+			return { node_obj.data[id] };
+		else
+			return node_obj.data;
+		end
+	end
+end
+
 return _M;

mercurial