changeset 340:567e486bce24

backend (notifications): use inline callbacks in _notifyPublish + added PEP data in _prepareNotify
author Goffi <goffi@goffi.org>
date Sun, 20 Aug 2017 11:16:51 +0200
parents 516b282aa542
children b49f75a26156
files sat_pubsub/backend.py
diffstat 1 files changed, 40 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/sat_pubsub/backend.py	Sun Aug 20 10:59:47 2017 +0200
+++ b/sat_pubsub/backend.py	Sun Aug 20 11:16:51 2017 +0200
@@ -1018,54 +1018,53 @@
         # if self.backend.supportsPublishModel():       #XXX: this feature is not really described in XEP-0060, we just can see it in examples
         #     self.features.append("publish_model")     #     but it's necessary for microblogging comments (see XEP-0277)
 
+
+    def getFullItem(self, item_data):
+        """ Attach item configuration to this item
+
+        Used to give item configuration back to node's owner (and *only* to owner)
+        """
+        # TODO: a test should check that only the owner get the item configuration back
+
+        item, item_config = item_data.item, item_data.config
+        new_item = deepcopy(item)
+        if item_config:
+            new_item.addChild(item_config.toElement())
+        return new_item
+
+    @defer.inlineCallbacks
     def _notifyPublish(self, data):
         items_data = data['items_data']
         node = data['node']
         pep = data['pep']
         recipient = data['recipient']
 
-        def afterPrepare(result):
-            owners, notifications_filtered = result
-            #we notify the owners
-            #FIXME: check if this comply with XEP-0060 (option needed ?)
-            #TODO: item's access model have to be sent back to owner
-            #TODO: same thing for getItems
+        owners, notifications_filtered = yield self._prepareNotify(items_data, node, data.get('subscription'), pep, recipient)
 
-            def getFullItem(item_data):
-                """ Attach item configuration to this item
-                Used to give item configuration back to node's owner (and *only* to owner)
-                """
-                #TODO: a test should check that only the owner get the item configuration back
-
-                item, item_config = item_data.item, item_data.config
-                new_item = deepcopy(item)
-                if item_config:
-                    new_item.addChild(item_config.toElement())
-                return new_item
+        # we notify the owners
+        # FIXME: check if this comply with XEP-0060 (option needed ?)
+        # TODO: item's access model have to be sent back to owner
+        # TODO: same thing for getItems
 
-            for owner_jid in owners:
-                notifications_filtered.append(
-                    (owner_jid,
-                     set([pubsub.Subscription(node.nodeIdentifier,
-                                              owner_jid,
-                                              'subscribed')]),
-                     [getFullItem(item_data) for item_data in items_data]))
+        for owner_jid in owners:
+            notifications_filtered.append(
+                (owner_jid,
+                 set([pubsub.Subscription(node.nodeIdentifier,
+                                          owner_jid,
+                                          'subscribed')]),
+                 [self.getFullItem(item_data) for item_data in items_data]))
 
-            if pep:
-                return self.backend.privilege.notifyPublish(
-                    recipient,
-                    node.nodeIdentifier,
-                    notifications_filtered)
+        if pep:
+            defer.returnValue(self.backend.privilege.notifyPublish(
+                recipient,
+                node.nodeIdentifier,
+                notifications_filtered))
 
-            else:
-                return self.pubsubService.notifyPublish(
-                    self.serviceJID,
-                    node.nodeIdentifier,
-                    notifications_filtered)
-
-        d = self._prepareNotify(items_data, node, data.get('subscription'))
-        d.addCallback(afterPrepare)
-        return d
+        else:
+            defer.returnValue(self.pubsubService.notifyPublish(
+                self.serviceJID,
+                node.nodeIdentifier,
+                notifications_filtered))
 
     def _notifyRetract(self, data):
         items_data = data['items_data']
@@ -1097,17 +1096,17 @@
                     node.nodeIdentifier,
                     notifications_filtered)
 
-        d = self._prepareNotify(items_data, node, data.get('subscription'))
+        d = self._prepareNotify(items_data, node, data.get('subscription'), pep, recipient)
         d.addCallback(afterPrepare)
         return d
 
 
     @defer.inlineCallbacks
-    def _prepareNotify(self, items_data, node, subscription=None):
+    def _prepareNotify(self, items_data, node, subscription=None, pep=None, recipient=None):
         """Do a bunch of permissions check and filter notifications
 
         The owner is not added to these notifications,
-        it must be called by the calling method
+        it must be added by the calling method
         @param items_data(tuple): must contain:
             - item (domish.Element)
             - access_model (unicode)
@@ -1120,8 +1119,6 @@
             - node_owner_jid
             - items_data
         """
-
-
         if subscription is None:
             notifications = yield self.backend.getNotifications(node, items_data)
         else: