comparison sat/plugins/plugin_xep_0060.py @ 3100:cea52c9ddfd9

plugin XEP-0060, jp (pubsub/set): publish-options implementation: - publishing options (XEP-0060 ยง7.1.5) implementation. - jp pubsub/set can specify publish-options using `-f` and `-F` - doc has been updated to explain that - psItemSend and psItemsSend now use serialisation for "extra" - publish-options can be specified in extra['publish-options'] with those methods
author Goffi <goffi@goffi.org>
date Sat, 28 Dec 2019 20:02:18 +0100
parents 73db9db8b9e1
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3099:0b6d56a8f7e3 3100:cea52c9ddfd9
199 async_=True, 199 async_=True,
200 ) 200 )
201 host.bridge.addMethod( 201 host.bridge.addMethod(
202 "psItemSend", 202 "psItemSend",
203 ".plugin", 203 ".plugin",
204 in_sign="ssssa{ss}s", 204 in_sign="ssssss",
205 out_sign="s", 205 out_sign="s",
206 method=self._sendItem, 206 method=self._sendItem,
207 async_=True, 207 async_=True,
208 ) 208 )
209 host.bridge.addMethod( 209 host.bridge.addMethod(
210 "psItemsSend", 210 "psItemsSend",
211 ".plugin", 211 ".plugin",
212 in_sign="ssasa{ss}s", 212 in_sign="ssasss",
213 out_sign="as", 213 out_sign="as",
214 method=self._sendItems, 214 method=self._sendItems,
215 async_=True, 215 async_=True,
216 ) 216 )
217 host.bridge.addMethod( 217 host.bridge.addMethod(
451 # """ 451 # """
452 # d = self.subscriptions(service, nodeIdentifier, profile_key=profile) 452 # d = self.subscriptions(service, nodeIdentifier, profile_key=profile)
453 # d.addCallback(lambda subs: [sub.getAttribute('node') for sub in subs if sub.getAttribute('subscription') == filter_]) 453 # d.addCallback(lambda subs: [sub.getAttribute('node') for sub in subs if sub.getAttribute('subscription') == filter_])
454 # return d 454 # return d
455 455
456 def _sendItem(self, service, nodeIdentifier, payload, item_id=None, extra=None, 456 def _sendItem(self, service, nodeIdentifier, payload, item_id=None, extra_ser="",
457 profile_key=C.PROF_KEY_NONE): 457 profile_key=C.PROF_KEY_NONE):
458 client = self.host.getClient(profile_key) 458 client = self.host.getClient(profile_key)
459 service = None if not service else jid.JID(service) 459 service = None if not service else jid.JID(service)
460 extra = data_format.deserialise(extra_ser)
460 d = self.sendItem( 461 d = self.sendItem(
461 client, service, nodeIdentifier, payload, item_id or None, extra 462 client, service, nodeIdentifier, payload, item_id or None, extra
462 ) 463 )
463 d.addCallback(lambda ret: ret or "") 464 d.addCallback(lambda ret: ret or "")
464 return d 465 return d
465 466
466 def _sendItems(self, service, nodeIdentifier, items, extra=None, 467 def _sendItems(self, service, nodeIdentifier, items, extra_ser=None,
467 profile_key=C.PROF_KEY_NONE): 468 profile_key=C.PROF_KEY_NONE):
468 client = self.host.getClient(profile_key) 469 client = self.host.getClient(profile_key)
469 service = None if not service else jid.JID(service) 470 service = None if not service else jid.JID(service)
470 try: 471 try:
471 items = [generic.parseXml(item.encode('utf-8')) for item in items] 472 items = [generic.parseXml(item.encode('utf-8')) for item in items]
472 except Exception as e: 473 except Exception as e:
473 raise exceptions.DataError(_("Can't parse items: {msg}").format( 474 raise exceptions.DataError(_("Can't parse items: {msg}").format(
474 msg=e)) 475 msg=e))
476 extra = data_format.deserialise(extra_ser)
475 d = self.sendItems( 477 d = self.sendItems(
476 client, service, nodeIdentifier, items, extra 478 client, service, nodeIdentifier, items, extra
477 ) 479 )
478 return d 480 return d
479 481
499 @param payload(domish.Element, unicode): payload of the item to send 501 @param payload(domish.Element, unicode): payload of the item to send
500 @param item_id(unicode, None): id to use or None to create one 502 @param item_id(unicode, None): id to use or None to create one
501 @param extra(dict, None): extra option, not used yet 503 @param extra(dict, None): extra option, not used yet
502 @return (unicode, None): id of the created item 504 @return (unicode, None): id of the created item
503 """ 505 """
506 if extra is None:
507 extra = {}
508 publish_options = extra.get('publish_options')
504 item_elt = pubsub.Item(id=item_id, payload=payload) 509 item_elt = pubsub.Item(id=item_id, payload=payload)
505 d = self.publish(client, service, nodeIdentifier, [item_elt]) 510 d = self.publish(
511 client, service, nodeIdentifier, [item_elt], options=publish_options)
506 d.addCallback(self._getPublishedItemId, item_id) 512 d.addCallback(self._getPublishedItemId, item_id)
507 return d 513 return d
508 514
509 def _publishCb(self, iq_result): 515 def _publishCb(self, iq_result):
510 """Parse publish result, and return ids given by pubsub service""" 516 """Parse publish result, and return ids given by pubsub service"""
521 @param service(jid.JID, None): service to send the item to 527 @param service(jid.JID, None): service to send the item to
522 None to use PEP 528 None to use PEP
523 @param NodeIdentifier(unicode): PubSub node to use 529 @param NodeIdentifier(unicode): PubSub node to use
524 @param items(list[domish.Element]): whole item elements to send, 530 @param items(list[domish.Element]): whole item elements to send,
525 "id" will be used if set 531 "id" will be used if set
526 @param extra(dict, None): extra option, not used yet 532 @param extra(dict, None): extra options. Key can be:
533 - publish_options: dict of publish-options
527 @return (list[unicode]): ids of the created items 534 @return (list[unicode]): ids of the created items
528 """ 535 """
536 if extra is None:
537 extra = {}
529 parsed_items = [] 538 parsed_items = []
530 for item in items: 539 for item in items:
531 if item.name != 'item': 540 if item.name != 'item':
532 raise exceptions.DataError(_("Invalid item: {xml}").format(item.toXml())) 541 raise exceptions.DataError(_("Invalid item: {xml}").format(item.toXml()))
533 item_id = item.getAttribute("id") 542 item_id = item.getAttribute("id")
534 parsed_items.append(pubsub.Item(id=item_id, payload=item.firstChildElement())) 543 parsed_items.append(pubsub.Item(id=item_id, payload=item.firstChildElement()))
535 d = self.publish(client, service, nodeIdentifier, parsed_items) 544 publish_options = extra.get('publish_options')
545 d = self.publish(
546 client, service, nodeIdentifier, parsed_items, options=publish_options)
536 d.addCallback(self._publishCb) 547 d.addCallback(self._publishCb)
537 return d 548 return d
538 549
539 def publish(self, client, service, nodeIdentifier, items=None): 550 def publish(self, client, service, nodeIdentifier, items=None, options=None):
540 return client.pubsub_client.publish( 551 return client.pubsub_client.publish(
541 service, nodeIdentifier, items, client.pubsub_client.parent.jid 552 service, nodeIdentifier, items, client.pubsub_client.parent.jid,
553 options=options
542 ) 554 )
543 555
544 def _unwrapMAMMessage(self, message_elt): 556 def _unwrapMAMMessage(self, message_elt):
545 try: 557 try:
546 item_elt = reduce( 558 item_elt = reduce(