diff sat_pubsub/backend.py @ 252:25a1dc7181cc

full items, with item-configuration, are returned if items are asked by the owner
author Goffi <goffi@goffi.org>
date Thu, 01 Nov 2012 19:28:43 +0100
parents 0a7d43b3dad6
children d55620ceafed
line wrap: on
line diff
--- a/sat_pubsub/backend.py	Tue Oct 23 00:09:38 2012 +0200
+++ b/sat_pubsub/backend.py	Thu Nov 01 19:28:43 2012 +0100
@@ -80,6 +80,7 @@
 from sat_pubsub import error, iidavoll, const
 from sat_pubsub.iidavoll import IBackendService, ILeafNode
 
+from copy import deepcopy
 
 def _getAffiliation(node, entity):
     d = node.getAffiliation(entity)
@@ -476,6 +477,28 @@
     def _doGetItems(self, result, requestor, maxItems, itemIdentifiers):
         node, affiliation = result
 
+        def append_item_config(items_data):
+            ret = []
+            for data in items_data:
+                item, access_model, access_list = data
+                if access_model == const.VAL_OPEN:
+                    pass
+                elif access_model == const.VAL_ROSTER: 
+                    form = data_form.Form('submit', formNamespace=const.NS_ITEM_CONFIG)
+                    access = data_form.Field(None, const.OPT_ACCESS_MODEL, value=const.VAL_ROSTER)
+                    allowed = data_form.Field(None, const.OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
+                    form.addField(access)
+                    form.addField(allowed)
+                    item.addChild(form.toElement())
+                elif access_model == const.VAL_JID:
+                    #FIXME: manage jid
+                    raise NotImplementedError
+                else:
+                    raise error.BadAccessTypeError(access_model)
+                
+                ret.append(item)
+            return ret
+
         def access_checked(access_data):
             authorized, roster = access_data
             if not authorized:
@@ -487,7 +510,11 @@
             if itemIdentifiers:
                 return node.getItemsById(authorized_groups, affiliation == 'owner', itemIdentifiers)
             else:
-                return node.getItems(authorized_groups, affiliation == 'owner', maxItems)
+                if affiliation == 'owner':
+                    d = node.getItems(authorized_groups, True, maxItems)
+                    return d.addCallback(append_item_config)
+                else:
+                    return node.getItems(authorized_groups, False, maxItems)
 
 
         if not ILeafNode.providedBy(node):
@@ -703,7 +730,6 @@
         if self.backend.supportsGroupBlog():
             self.features.append("groupblog")
 
-
     def _notify(self, data):
         items = data['items']
         node = data['node']
@@ -716,10 +742,10 @@
             #we filter items not allowed for the subscribers
             notifications_filtered = []
 
-            for subscriber, subscriptions, items in notifications:
+            for subscriber, subscriptions, _items in notifications:
                 allowed_items = [] #we keep only item which subscriber can access
 
-                for access_model, item_config, item in items:
+                for access_model, item_config, item in _items:
                     if access_model == 'open':
                         allowed_items.append(item)
                     elif access_model == 'roster':
@@ -740,11 +766,24 @@
             #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
+            
+            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
+                
+                access_model, item_config, item = item_data
+                new_item = deepcopy(item)
+                if item_config:
+                    new_item.addChild(item_config.toElement())
+                return new_item
+
             notifications_filtered.append((owner_jid,
                                            set([Subscription(node.nodeIdentifier, 
                                                             owner_jid,
                                                             'subscribed')]),
-                                           [item for access_model, item_config, item in items])) 
+                                           [getFullItem(item_data) for item_data in items])) 
 
             return self.pubsubService.notifyPublish(
                                                 self.serviceJID,