comparison 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
comparison
equal deleted inserted replaced
251:0a7d43b3dad6 252:25a1dc7181cc
78 from wokkel.pubsub import PubSubResource, PubSubError, Subscription 78 from wokkel.pubsub import PubSubResource, PubSubError, Subscription
79 79
80 from sat_pubsub import error, iidavoll, const 80 from sat_pubsub import error, iidavoll, const
81 from sat_pubsub.iidavoll import IBackendService, ILeafNode 81 from sat_pubsub.iidavoll import IBackendService, ILeafNode
82 82
83 from copy import deepcopy
83 84
84 def _getAffiliation(node, entity): 85 def _getAffiliation(node, entity):
85 d = node.getAffiliation(entity) 86 d = node.getAffiliation(entity)
86 d.addCallback(lambda affiliation: (node, affiliation)) 87 d.addCallback(lambda affiliation: (node, affiliation))
87 return d 88 return d
474 return d 475 return d
475 476
476 def _doGetItems(self, result, requestor, maxItems, itemIdentifiers): 477 def _doGetItems(self, result, requestor, maxItems, itemIdentifiers):
477 node, affiliation = result 478 node, affiliation = result
478 479
480 def append_item_config(items_data):
481 ret = []
482 for data in items_data:
483 item, access_model, access_list = data
484 if access_model == const.VAL_OPEN:
485 pass
486 elif access_model == const.VAL_ROSTER:
487 form = data_form.Form('submit', formNamespace=const.NS_ITEM_CONFIG)
488 access = data_form.Field(None, const.OPT_ACCESS_MODEL, value=const.VAL_ROSTER)
489 allowed = data_form.Field(None, const.OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
490 form.addField(access)
491 form.addField(allowed)
492 item.addChild(form.toElement())
493 elif access_model == const.VAL_JID:
494 #FIXME: manage jid
495 raise NotImplementedError
496 else:
497 raise error.BadAccessTypeError(access_model)
498
499 ret.append(item)
500 return ret
501
479 def access_checked(access_data): 502 def access_checked(access_data):
480 authorized, roster = access_data 503 authorized, roster = access_data
481 if not authorized: 504 if not authorized:
482 raise error.NotAuthorized() 505 raise error.NotAuthorized()
483 506
485 authorized_groups = tuple(roster_item.groups) if roster_item else tuple() 508 authorized_groups = tuple(roster_item.groups) if roster_item else tuple()
486 509
487 if itemIdentifiers: 510 if itemIdentifiers:
488 return node.getItemsById(authorized_groups, affiliation == 'owner', itemIdentifiers) 511 return node.getItemsById(authorized_groups, affiliation == 'owner', itemIdentifiers)
489 else: 512 else:
490 return node.getItems(authorized_groups, affiliation == 'owner', maxItems) 513 if affiliation == 'owner':
514 d = node.getItems(authorized_groups, True, maxItems)
515 return d.addCallback(append_item_config)
516 else:
517 return node.getItems(authorized_groups, False, maxItems)
491 518
492 519
493 if not ILeafNode.providedBy(node): 520 if not ILeafNode.providedBy(node):
494 return [] 521 return []
495 522
701 self.features.append("publisher-affiliation") 728 self.features.append("publisher-affiliation")
702 729
703 if self.backend.supportsGroupBlog(): 730 if self.backend.supportsGroupBlog():
704 self.features.append("groupblog") 731 self.features.append("groupblog")
705 732
706
707 def _notify(self, data): 733 def _notify(self, data):
708 items = data['items'] 734 items = data['items']
709 node = data['node'] 735 node = data['node']
710 736
711 def _notifyAllowed(result): 737 def _notifyAllowed(result):
714 notifications, (owner_jid,roster) = result 740 notifications, (owner_jid,roster) = result
715 741
716 #we filter items not allowed for the subscribers 742 #we filter items not allowed for the subscribers
717 notifications_filtered = [] 743 notifications_filtered = []
718 744
719 for subscriber, subscriptions, items in notifications: 745 for subscriber, subscriptions, _items in notifications:
720 allowed_items = [] #we keep only item which subscriber can access 746 allowed_items = [] #we keep only item which subscriber can access
721 747
722 for access_model, item_config, item in items: 748 for access_model, item_config, item in _items:
723 if access_model == 'open': 749 if access_model == 'open':
724 allowed_items.append(item) 750 allowed_items.append(item)
725 elif access_model == 'roster': 751 elif access_model == 'roster':
726 _subscriber = subscriber.userhost() 752 _subscriber = subscriber.userhost()
727 if not _subscriber in roster: 753 if not _subscriber in roster:
738 764
739 #we notify the owner 765 #we notify the owner
740 #FIXME: check if this comply with XEP-0060 (option needed ?) 766 #FIXME: check if this comply with XEP-0060 (option needed ?)
741 #TODO: item's access model have to be sent back to owner 767 #TODO: item's access model have to be sent back to owner
742 #TODO: same thing for getItems 768 #TODO: same thing for getItems
769
770 def getFullItem(item_data):
771 """ Attach item configuration to this item
772 Used to give item configuration back to node's owner (and *only* to owner)
773 """
774 #TODO: a test should check that only the owner get the item configuration back
775
776 access_model, item_config, item = item_data
777 new_item = deepcopy(item)
778 if item_config:
779 new_item.addChild(item_config.toElement())
780 return new_item
781
743 notifications_filtered.append((owner_jid, 782 notifications_filtered.append((owner_jid,
744 set([Subscription(node.nodeIdentifier, 783 set([Subscription(node.nodeIdentifier,
745 owner_jid, 784 owner_jid,
746 'subscribed')]), 785 'subscribed')]),
747 [item for access_model, item_config, item in items])) 786 [getFullItem(item_data) for item_data in items]))
748 787
749 return self.pubsubService.notifyPublish( 788 return self.pubsubService.notifyPublish(
750 self.serviceJID, 789 self.serviceJID,
751 node.nodeIdentifier, 790 node.nodeIdentifier,
752 notifications_filtered) 791 notifications_filtered)