X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fpubsub.lua;h=811f4a154c3ddbb25ef73183fc9020efcc9cf48e;hb=0ef23c673d1d620c9c1cc3e8aed43add63634426;hp=22a29c18a1fd74d5694c379205093016f6d3c625;hpb=fa19e92922b058d878a53260b38bfd7d3e98a991;p=prosody.git diff --git a/util/pubsub.lua b/util/pubsub.lua index 22a29c18..811f4a15 100644 --- a/util/pubsub.lua +++ b/util/pubsub.lua @@ -17,7 +17,14 @@ function service:add_subscription(node, actor, jid) end function service:remove_subscription(node, actor, jid) - self.nodes[node].subscribers[jid] = nil; + local node_obj = self.nodes[node]; + if not node_obj then + return false, "item-not-found"; + end + if not node_obj.subscribers[jid] then + return false, "not-subscribed"; + end + node_obj.subscribers[jid] = nil; return true; end @@ -28,15 +35,50 @@ function service:get_subscription(node, actor, jid) end end +function service:create(node, actor) + if not self.nodes[node] then + self.nodes[node] = { name = node, subscribers = {}, config = {}, data = {} }; + return true; + end + return false, "conflict"; +end + 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:retract(node, actor, id, retract) + local node_obj = self.nodes[node]; + if (not node_obj) or (not node_obj.data[id]) then + return false, "item-not-found"; + end + node_obj.data[id] = nil; + if retract then + self.cb.broadcaster(node, node_obj.subscribers, retract); + end + 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 + +function service:get_nodes(actor) + return true, self.nodes; +end + return _M;