comparison src/plugins/plugin_xep_0060.py @ 2272:b5befe7722d3

plugin XEP-0060: added sendItem and psItemSend bridge method as a way to send directly raw XML for an item + use client instead of profile_key in publish + renamed psItemGet to psItemsGet
author Goffi <goffi@goffi.org>
date Tue, 27 Jun 2017 19:38:20 +0200
parents 6856b7225c5b
children 972e33507609
comparison
equal deleted inserted replaced
2271:2fae89f30b8d 2272:b5befe7722d3
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 log = getLogger(__name__) 23 log = getLogger(__name__)
24 from sat.core import exceptions 24 from sat.core import exceptions
25
26 from sat.tools import sat_defer 25 from sat.tools import sat_defer
27 26
28 from twisted.words.protocols.jabber import jid, error 27 from twisted.words.protocols.jabber import jid, error
29 from twisted.internet import defer 28 from twisted.internet import defer
30 from wokkel import disco 29 from wokkel import disco
91 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)
92 host.bridge.addMethod("psAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getAffiliations, async=True) 91 host.bridge.addMethod("psAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getAffiliations, async=True)
93 host.bridge.addMethod("psNodeAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getNodeAffiliations, async=True) 92 host.bridge.addMethod("psNodeAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getNodeAffiliations, async=True)
94 host.bridge.addMethod("psNodeAffiliationsSet", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._setNodeAffiliations, async=True) 93 host.bridge.addMethod("psNodeAffiliationsSet", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._setNodeAffiliations, async=True)
95 host.bridge.addMethod("psNodeDelete", ".plugin", in_sign='sss', out_sign='', method=self._deleteNode, async=True) 94 host.bridge.addMethod("psNodeDelete", ".plugin", in_sign='sss', out_sign='', method=self._deleteNode, async=True)
96 host.bridge.addMethod("psItemGet", ".plugin", in_sign='ssiassa{ss}s', out_sign='(asa{ss})', method=self._getItems, async=True) 95 host.bridge.addMethod("psItemsGet", ".plugin", in_sign='ssiassa{ss}s', out_sign='(asa{ss})', method=self._getItems, async=True)
96 host.bridge.addMethod("psItemSend", ".plugin", in_sign='ssssa{ss}s', out_sign='s', method=self._sendItem, async=True)
97 host.bridge.addMethod("psRetractItem", ".plugin", in_sign='sssbs', out_sign='', method=self._retractItem, async=True) 97 host.bridge.addMethod("psRetractItem", ".plugin", in_sign='sssbs', out_sign='', method=self._retractItem, async=True)
98 host.bridge.addMethod("psRetractItems", ".plugin", in_sign='ssasbs', out_sign='', method=self._retractItems, async=True) 98 host.bridge.addMethod("psRetractItems", ".plugin", in_sign='ssasbs', out_sign='', method=self._retractItems, async=True)
99 host.bridge.addMethod("psSubscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany) 99 host.bridge.addMethod("psSubscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany)
100 host.bridge.addMethod("psGetSubscribeRTResult", ".plugin", in_sign='ss', out_sign='(ua(sss))', method=self._manySubscribeRTResult, async=True) 100 host.bridge.addMethod("psGetSubscribeRTResult", ".plugin", in_sign='ss', out_sign='(ua(sss))', method=self._manySubscribeRTResult, async=True)
101 host.bridge.addMethod("psGetFromMany", ".plugin", in_sign='a(ss)ia{ss}s', out_sign='s', method=self._getFromMany) 101 host.bridge.addMethod("psGetFromMany", ".plugin", in_sign='a(ss)ia{ss}s', out_sign='s', method=self._getFromMany)
256 # """ 256 # """
257 # d = self.subscriptions(service, nodeIdentifier, profile_key=profile) 257 # d = self.subscriptions(service, nodeIdentifier, profile_key=profile)
258 # d.addCallback(lambda subs: [sub.getAttribute('node') for sub in subs if sub.getAttribute('subscription') == filter_]) 258 # d.addCallback(lambda subs: [sub.getAttribute('node') for sub in subs if sub.getAttribute('subscription') == filter_])
259 # return d 259 # return d
260 260
261 def publish(self, service, nodeIdentifier, items=None, profile_key=C.PROF_KEY_NONE): 261 def _sendItem(self, service, nodeIdentifier, payload, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
262 client = self.host.getClient(profile_key) 262 client = self.host.getClient(profile_key)
263 service = None if not service else jid.JID(service)
264 d = self.sendItem(client, service, nodeIdentifier, payload, item_id or None, extra)
265 d.addCallback(lambda ret: ret or u'')
266 return d
267
268 def _getPublishedItemId(self, iq_elt, original_id):
269 """return item of published id if found in answer
270
271 if not found original_id is returned, or empty string if it is None or empty string
272 """
273 try:
274 item_id = iq_elt.pubsub.publish.item['id']
275 except (AttributeError, KeyError):
276 item_id = None
277 return item_id or original_id
278
279 def sendItem(self, client, service, nodeIdentifier, payload, item_id=None, extra=None):
280 """high level method to send one item
281
282 @param service(jid.JID, None): service to send the item to
283 None to use PEP
284 @param NodeIdentifier(unicode): PubSub node to use
285 @param item_id(unicode, None): id to use or None to create one
286 @param payload(domish.Element, unicode): payload of the item to send
287 @param extra(dict, None): extra option, not used yet
288 @return (unicode, None): id of the created item
289 """
290 item_elt = pubsub.Item(id=item_id, payload=payload)
291 d = self.publish(client, service, nodeIdentifier, [item_elt])
292 d.addCallback(self._getPublishedItemId, item_id)
293 return d
294
295 def publish(self, client, service, nodeIdentifier, items=None):
263 return client.pubsub_client.publish(service, nodeIdentifier, items, client.pubsub_client.parent.jid) 296 return client.pubsub_client.publish(service, nodeIdentifier, items, client.pubsub_client.parent.jid)
264 297
265 def _unwrapMAMMessage(self, message_elt): 298 def _unwrapMAMMessage(self, message_elt):
266 try: 299 try:
267 item_elt = (message_elt.elements(mam.NS_MAM, 'result').next() 300 item_elt = (message_elt.elements(mam.NS_MAM, 'result').next()