comparison sat_pubsub/backend.py @ 341:b49f75a26156

backend, pgsql: implemented subscriptionsGet and subscriptionsSet
author Goffi <goffi@goffi.org>
date Sun, 20 Aug 2017 11:56:04 +0200
parents 567e486bce24
children ff8aff4c9b79
comparison
equal deleted inserted replaced
340:567e486bce24 341:b49f75a26156
609 d = node.setAffiliations(affiliations) 609 d = node.setAffiliations(affiliations)
610 610
611 return d 611 return d
612 612
613 613
614 def getSubscriptionsOwner(self, nodeIdentifier, requestor, pep, recipient):
615 d = self.storage.getNode(nodeIdentifier, pep, recipient)
616 d.addCallback(_getAffiliation, requestor)
617 d.addCallback(self._doGetSubscriptionsOwner)
618 return d
619
620
621 def _doGetSubscriptionsOwner(self, result):
622 node, affiliation = result
623
624 if affiliation != 'owner':
625 raise error.Forbidden()
626 return node.getSubscriptions()
627
628
629 def setSubscriptionsOwner(self, nodeIdentifier, requestor, subscriptions, pep, recipient):
630 d = self.storage.getNode(nodeIdentifier, pep, recipient)
631 d.addCallback(_getAffiliation, requestor)
632 d.addCallback(self._doSetSubscriptionsOwner, requestor, subscriptions)
633 return d
634
635 def unwrapFirstError(self, failure):
636 failure.trap(defer.FirstError)
637 return failure.value.subFailure
638
639 def _doSetSubscriptionsOwner(self, result, requestor, subscriptions):
640 # Check that requestor is allowed to set subscriptions, and delete entities
641 # with "none" subscription
642
643 # TODO: return error with failed subscriptions in case of failure
644 node, requestor_affiliation = result
645
646 if requestor_affiliation != 'owner':
647 raise error.Forbidden()
648
649 d_list = []
650
651 for subscription in subscriptions.copy():
652 if subscription.state == 'none':
653 subscriptions.remove(subscription)
654 d_list.append(node.removeSubscription(subscription.subscriber))
655
656 if subscriptions:
657 d_list.append(node.setSubscriptions(subscriptions))
658
659 d = defer.gatherResults(d_list, consumeErrors=True)
660 d.addCallback(lambda _: None)
661 d.addErrback(self.unwrapFirstError)
662 return d
663
664
614 def getItems(self, nodeIdentifier, requestor, recipient, maxItems=None, 665 def getItems(self, nodeIdentifier, requestor, recipient, maxItems=None,
615 itemIdentifiers=None, ext_data=None): 666 itemIdentifiers=None, ext_data=None):
616 d = self.getItemsData(nodeIdentifier, requestor, recipient, maxItems, itemIdentifiers, ext_data) 667 d = self.getItemsData(nodeIdentifier, requestor, recipient, maxItems, itemIdentifiers, ext_data)
617 d.addCallback(lambda items_data: [item_data.item for item_data in items_data]) 668 d.addCallback(lambda items_data: [item_data.item for item_data in items_data])
618 return d 669 return d
1349 self._isPep(request), 1400 self._isPep(request),
1350 request.recipient) 1401 request.recipient)
1351 return d.addErrback(self._mapErrors) 1402 return d.addErrback(self._mapErrors)
1352 1403
1353 1404
1405 def subscriptionsGet(self, request):
1406 """Retrieve subscriptions for owner (cf. XEP-0060 ยง8.8.1)
1407
1408 retrieve all affiliations for a node
1409 """
1410 d = self.backend.getSubscriptionsOwner(request.nodeIdentifier,
1411 request.sender,
1412 self._isPep(request),
1413 request.recipient)
1414 return d.addErrback(self._mapErrors)
1415
1416
1417 def subscriptionsSet(self, request):
1418 d = self.backend.setSubscriptionsOwner(request.nodeIdentifier,
1419 request.sender,
1420 request.subscriptions,
1421 self._isPep(request),
1422 request.recipient)
1423 return d.addErrback(self._mapErrors)
1424
1425
1354 def items(self, request): 1426 def items(self, request):
1355 ext_data = {} 1427 ext_data = {}
1356 if const.FLAG_ENABLE_RSM and request.rsm is not None: 1428 if const.FLAG_ENABLE_RSM and request.rsm is not None:
1357 ext_data['rsm'] = request.rsm 1429 ext_data['rsm'] = request.rsm
1358 try: 1430 try: