changeset 1760:30efe084471c

plugin XEP-0060: None is no more used in addManagedNode, but all nodes prefixed with a registered one are now triggered
author Goffi <goffi@goffi.org>
date Fri, 18 Dec 2015 15:06:58 +0100
parents 81923b3f8b14
children ad20cedbf25e
files src/plugins/plugin_xep_0060.py src/plugins/plugin_xep_0277.py
diffstat 2 files changed, 26 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0060.py	Thu Dec 17 22:38:00 2015 +0100
+++ b/src/plugins/plugin_xep_0060.py	Fri Dec 18 15:06:58 2015 +0100
@@ -142,12 +142,14 @@
     def addManagedNode(self, node, **kwargs):
         """Add a handler for a node
 
-        @param node(unicode): node to monitor, or None to monitor all
+        @param node(unicode): node to monitor
+            all node *prefixed* with this one will be triggered
         @param **kwargs: method(s) to call when the node is found
             the methode must be named after PubSub constants in lower case
             and suffixed with "_cb"
             e.g.: "publish_cb" for C.PS_PUBLISH, "delete_cb" for C.PS_DELETE
         """
+        assert node is not None
         assert kwargs
         callbacks = self._node_cb.setdefault(node, {})
         for event, cb in kwargs.iteritems():
@@ -247,7 +249,7 @@
         def subscribeEb(failure, service, node):
             failure.trap(error.StanzaError)
             log.warning("Could not subscribe to node {} on service {}: {}".format(node, unicode(service), unicode(failure.value)))
-            
+
         def doSubscribe(items):
             self.subscribe(service, node, profile_key=profile_key).addErrback(subscribeEb, service, node)
             return items
@@ -495,27 +497,32 @@
     def connectionInitialized(self):
         rsm.PubSubClient.connectionInitialized(self)
 
+    def _getNodeCallbacks(self, node, event):
+        """Generate callbacks from given node and event
+
+        @param node(unicode): node used for the item
+            any registered node which prefix the node will match
+        @param event(unicode): one of C.PS_ITEMS, C.PS_RETRACT, C.PS_DELETE
+        @return (iterator[callable]): callbacks for this node/event
+        """
+        for registered_node, callbacks_dict in self.parent_plugin._node_cb.iteritems():
+            if not node.startswith(registered_node):
+                continue
+            try:
+                for callback in callbacks_dict[event]:
+                    yield callback
+            except KeyError:
+                continue
+
     def itemsReceived(self, event):
         log.debug(u"Pubsub items received")
-        for node in (event.nodeIdentifier, None):
-            try:
-                callbacks = self.parent_plugin._node_cb[node][C.PS_ITEMS]
-            except KeyError:
-                pass
-            else:
-                for callback in callbacks:
-                    callback(event, self.parent.profile)
+        for callback in self._getNodeCallbacks(event.nodeIdentifier, C.PS_ITEMS):
+            callback(event, self.parent.profile)
 
     def deleteReceived(self, event):
         log.debug((u"Publish node deleted"))
-        for node in (event.nodeIdentifier, None):
-            try:
-                callbacks = self.parent_plugin._node_cb[node][C.PS_DELETE]
-            except KeyError:
-                pass
-            else:
-                for callback in callbacks:
-                    callback(event, self.parent.profile)
+        for callback in self._getNodeCallbacks(event.nodeIdentifier, C.PS_DELETE):
+            callback(event, self.parent.profile)
 
     def subscriptions(self, service, nodeIdentifier, sender=None):
         """Return the list of subscriptions to the given service and node.
--- a/src/plugins/plugin_xep_0277.py	Thu Dec 17 22:38:00 2015 +0100
+++ b/src/plugins/plugin_xep_0277.py	Fri Dec 18 15:06:58 2015 +0100
@@ -72,7 +72,7 @@
         self.host = host
         self._p = self.host.plugins["XEP-0060"] # this facilitate the access to pubsub plugin
         self.rt_sessions = sat_defer.RTDeferredSessions()
-        self.host.plugins["XEP-0060"].addManagedNode(None, items_cb=self._itemsReceived)
+        self.host.plugins["XEP-0060"].addManagedNode(NS_MICROBLOG, items_cb=self._itemsReceived)
 
         host.bridge.addMethod("mbSend", ".plugin",
                               in_sign='ssa{ss}s', out_sign='',
@@ -120,8 +120,6 @@
 
     def _itemsReceived(self, itemsEvent, profile):
         """Callback which manage items notifications (publish + retract)"""
-        if not itemsEvent.nodeIdentifier.startswith(NS_MICROBLOG):
-            return
         def manageItem(data, event):
             self.host.bridge.psEvent(C.PS_MICROBLOG, itemsEvent.sender.full(), itemsEvent.nodeIdentifier, event, data, profile)