comparison src/plugins/plugin_xep_0060.py @ 2203:427391c706eb

plugin XEP-0060: added affiliation handling: New methods getAffiliations (for checking affiliation by any entity) and getNodeAffiliations (for checking the affiliations of a node, for the owner), and theirs corresponding bridge methods.
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2017 21:01:51 +0100
parents 40dd9eb2692c
children 59d3de85a0cb
comparison
equal deleted inserted replaced
2202:7f91b894bfdf 2203:427391c706eb
86 self._mam = host.plugins.get('XEP-0313') 86 self._mam = host.plugins.get('XEP-0313')
87 self._node_cb = {} # dictionnary of callbacks for node (key: node, value: list of callbacks) 87 self._node_cb = {} # dictionnary of callbacks for node (key: node, value: list of callbacks)
88 self.rt_sessions = sat_defer.RTDeferredSessions() 88 self.rt_sessions = sat_defer.RTDeferredSessions()
89 host.bridge.addMethod("psNodeConfigurationGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getNodeConfiguration, async=True) 89 host.bridge.addMethod("psNodeConfigurationGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getNodeConfiguration, async=True)
90 host.bridge.addMethod("psNodeConfigurationSet", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._setNodeConfiguration, async=True) 90 host.bridge.addMethod("psNodeConfigurationSet", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._setNodeConfiguration, async=True)
91 host.bridge.addMethod("psAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getAffiliations, async=True)
92 host.bridge.addMethod("psNodeAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getNodeAffiliations, async=True)
91 host.bridge.addMethod("psDeleteNode", ".plugin", in_sign='sss', out_sign='', method=self._deleteNode, async=True) 93 host.bridge.addMethod("psDeleteNode", ".plugin", in_sign='sss', out_sign='', method=self._deleteNode, async=True)
92 host.bridge.addMethod("psItemGet", ".plugin", in_sign='ssiassa{ss}s', out_sign='(asa{ss})', method=self._getItems, async=True) 94 host.bridge.addMethod("psItemGet", ".plugin", in_sign='ssiassa{ss}s', out_sign='(asa{ss})', method=self._getItems, async=True)
93 host.bridge.addMethod("psRetractItem", ".plugin", in_sign='sssbs', out_sign='', method=self._retractItem, async=True) 95 host.bridge.addMethod("psRetractItem", ".plugin", in_sign='sssbs', out_sign='', method=self._retractItem, async=True)
94 host.bridge.addMethod("psRetractItems", ".plugin", in_sign='ssasbs', out_sign='', method=self._retractItems, async=True) 96 host.bridge.addMethod("psRetractItems", ".plugin", in_sign='ssasbs', out_sign='', method=self._retractItems, async=True)
95 host.bridge.addMethod("psSubscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany) 97 host.bridge.addMethod("psSubscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany)
434 request.options = form 436 request.options = form
435 437
436 d = request.send(client.xmlstream) 438 d = request.send(client.xmlstream)
437 return d 439 return d
438 440
441 def _getAffiliations(self, service_s, nodeIdentifier, profile_key):
442 client = self.host.getClient(profile_key)
443 d = self.getAffiliations(client, jid.JID(service_s) if service_s else None, nodeIdentifier or None)
444 return d
445
446 def getAffiliations(self, client, service, nodeIdentifier=None):
447 """Retrieve affiliations of an entity
448
449 @param nodeIdentifier(unicode, None): node to get affiliation from
450 None to get all nodes affiliations for this service
451 """
452 request = pubsub.PubSubRequest('affiliations')
453 request.recipient = service
454 request.nodeIdentifier = nodeIdentifier
455
456 def cb(iq_elt):
457 try:
458 affiliations_elt = next(iq_elt.pubsub.elements((pubsub.NS_PUBSUB, 'affiliations')))
459 except StopIteration:
460 raise ValueError(_(u"Invalid result: missing <affiliations> element: {}").format(iq_elt.toXml))
461 try:
462 return {e['node']: e['affiliation'] for e in affiliations_elt.elements((pubsub.NS_PUBSUB, 'affiliation'))}
463 except KeyError:
464 raise ValueError(_(u"Invalid result: bad <affiliation> element: {}").format(iq_elt.toXml))
465
466 d = request.send(client.xmlstream)
467 d.addCallback(cb)
468 return d
469
470 def _getNodeAffiliations(self, service_s, nodeIdentifier, profile_key):
471 client = self.host.getClient(profile_key)
472 d = self.getNodeAffiliations(client, jid.JID(service_s) if service_s else None, nodeIdentifier)
473 return d
474
475 def getNodeAffiliations(self, client, service, nodeIdentifier):
476 """Retrieve affiliations of a node owned by profile"""
477 request = pubsub.PubSubRequest('affiliationsGet')
478 request.recipient = service
479 request.nodeIdentifier = nodeIdentifier
480
481 def cb(iq_elt):
482 try:
483 affiliations_elt = next(iq_elt.pubsub.elements((pubsub.NS_PUBSUB_OWNER, 'affiliations')))
484 except StopIteration:
485 raise ValueError(_(u"Invalid result: missing <affiliations> element: {}").format(iq_elt.toXml))
486 try:
487 return {e['jid']: e['affiliation'] for e in affiliations_elt.elements((pubsub.NS_PUBSUB_OWNER, 'affiliation'))}
488 except KeyError:
489 raise ValueError(_(u"Invalid result: bad <affiliation> element: {}").format(iq_elt.toXml))
490
491 d = request.send(client.xmlstream)
492 d.addCallback(cb)
493 return d
494
439 def _deleteNode(self, service_s, nodeIdentifier, profile_key): 495 def _deleteNode(self, service_s, nodeIdentifier, profile_key):
440 return self.deleteNode(jid.JID(service_s) if service_s else None, nodeIdentifier, profile_key) 496 return self.deleteNode(jid.JID(service_s) if service_s else None, nodeIdentifier, profile_key)
441 497
442 def deleteNode(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE): 498 def deleteNode(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
443 client = self.host.getClient(profile_key) 499 client = self.host.getClient(profile_key)
621 extra = self.parseExtra(extra_dict) 677 extra = self.parseExtra(extra_dict)
622 return self.getFromMany([(jid.JID(service), unicode(node)) for service, node in node_data], max_item, extra.rsm_request, extra.extra, profile_key) 678 return self.getFromMany([(jid.JID(service), unicode(node)) for service, node in node_data], max_item, extra.rsm_request, extra.extra, profile_key)
623 679
624 def getFromMany(self, node_data, max_item=None, rsm_request=None, extra=None, profile_key=C.PROF_KEY_NONE): 680 def getFromMany(self, node_data, max_item=None, rsm_request=None, extra=None, profile_key=C.PROF_KEY_NONE):
625 """Get items from many nodes at once 681 """Get items from many nodes at once
682
626 @param node_data (iterable[tuple]): iterable of tuple (service, node) where: 683 @param node_data (iterable[tuple]): iterable of tuple (service, node) where:
627 - service (jid.JID) is the pubsub service 684 - service (jid.JID) is the pubsub service
628 - node (unicode) is the node to get items from 685 - node (unicode) is the node to get items from
629 @param max_items (int): optional limit on the number of retrieved items. 686 @param max_items (int): optional limit on the number of retrieved items.
630 @param rsm_request (RSMRequest): RSM request data 687 @param rsm_request (RSMRequest): RSM request data