comparison sat/plugins/plugin_xep_0060.py @ 3867:c3e6c54660da

plugin XEP-0060: sender can now be specified in `sendItems` and `publish`: if not sended is set, `client.jid` is used.
author Goffi <goffi@goffi.org>
date Thu, 21 Jul 2022 18:02:33 +0200
parents e417c478b488
children 1e64f1ed3ebd
comparison
equal deleted inserted replaced
3866:915fb230cb28 3867:c3e6c54660da
532 except Exception as e: 532 except Exception as e:
533 raise exceptions.DataError(_("Can't parse items: {msg}").format( 533 raise exceptions.DataError(_("Can't parse items: {msg}").format(
534 msg=e)) 534 msg=e))
535 extra = data_format.deserialise(extra_ser) 535 extra = data_format.deserialise(extra_ser)
536 return defer.ensureDeferred(self.sendItems( 536 return defer.ensureDeferred(self.sendItems(
537 client, service, nodeIdentifier, items, extra 537 client, service, nodeIdentifier, items, extra=extra
538 )) 538 ))
539 539
540 async def sendItem( 540 async def sendItem(
541 self, client, service, nodeIdentifier, payload, item_id=None, extra=None 541 self, client, service, nodeIdentifier, payload, item_id=None, extra=None
542 ): 542 ):
558 published_ids = await self.sendItems( 558 published_ids = await self.sendItems(
559 client, 559 client,
560 service, 560 service,
561 nodeIdentifier, 561 nodeIdentifier,
562 [item_elt], 562 [item_elt],
563 extra 563 extra=extra
564 ) 564 )
565 try: 565 try:
566 return published_ids[0] 566 return published_ids[0]
567 except IndexError: 567 except IndexError:
568 return item_id 568 return item_id
569 569
570 async def sendItems( 570 async def sendItems(
571 self, 571 self,
572 client: SatXMPPEntity, 572 client: SatXMPPEntity,
573 service: jid.JID, 573 service: Optional[jid.JID],
574 nodeIdentifier: str, 574 nodeIdentifier: str,
575 items: List[domish.Element], 575 items: List[domish.Element],
576 sender: Optional[jid.JID] = None,
576 extra: Optional[Dict[str, Any]] = None 577 extra: Optional[Dict[str, Any]] = None
577 ) -> List[str]: 578 ) -> List[str]:
578 """High level method to send several items at once 579 """High level method to send several items at once
579 580
580 @param service: service to send the item to 581 @param service: service to send the item to
593 be used 594 be used
594 @return: ids of the created items 595 @return: ids of the created items
595 """ 596 """
596 if extra is None: 597 if extra is None:
597 extra = {} 598 extra = {}
599 if service is None:
600 service = client.jid.userhostJID()
598 parsed_items = [] 601 parsed_items = []
599 for item in items: 602 for item in items:
600 if item.name != 'item': 603 if item.name != 'item':
601 raise exceptions.DataError(_("Invalid item: {xml}").format(item.toXml())) 604 raise exceptions.DataError(_("Invalid item: {xml}").format(item.toXml()))
602 item_id = item.getAttribute("id") 605 item_id = item.getAttribute("id")
603 parsed_items.append(pubsub.Item(id=item_id, payload=item.firstChildElement())) 606 parsed_items.append(pubsub.Item(id=item_id, payload=item.firstChildElement()))
604 publish_options = extra.get(self.EXTRA_PUBLISH_OPTIONS) 607 publish_options = extra.get(self.EXTRA_PUBLISH_OPTIONS)
605 try: 608 try:
606 iq_result = await self.publish( 609 iq_result = await self.publish(
607 client, service, nodeIdentifier, parsed_items, options=publish_options) 610 client, service, nodeIdentifier, parsed_items, options=publish_options,
611 sender=sender
612 )
608 except error.StanzaError as e: 613 except error.StanzaError as e:
609 if ((e.condition == 'conflict' and e.appCondition 614 if ((e.condition == 'conflict' and e.appCondition
610 and e.appCondition.name == 'precondition-not-met' 615 and e.appCondition.name == 'precondition-not-met'
611 and publish_options is not None)): 616 and publish_options is not None)):
612 # this usually happens when publish-options can't be set 617 # this usually happens when publish-options can't be set
644 self, 649 self,
645 client: SatXMPPEntity, 650 client: SatXMPPEntity,
646 service: jid.JID, 651 service: jid.JID,
647 nodeIdentifier: str, 652 nodeIdentifier: str,
648 items: Optional[List[domish.Element]] = None, 653 items: Optional[List[domish.Element]] = None,
649 options: Optional[dict] = None 654 options: Optional[dict] = None,
655 sender: Optional[jid.JID] = None
650 ) -> domish.Element: 656 ) -> domish.Element:
651 """Publish pubsub items 657 """Publish pubsub items
652 658
659 @param sender: sender of the request,
660 client.jid will be used if nto set
653 @return: IQ result stanza 661 @return: IQ result stanza
654 """ 662 """
663 if sender is None:
664 sender = client.jid
655 iq_result_elt = await client.pubsub_client.publish( 665 iq_result_elt = await client.pubsub_client.publish(
656 service, nodeIdentifier, items, client.pubsub_client.parent.jid, 666 service, nodeIdentifier, items, sender,
657 options=options 667 options=options
658 ) 668 )
659 669
660 await self.host.trigger.asyncPoint( 670 await self.host.trigger.asyncPoint(
661 "XEP-0060_publish", client, service, nodeIdentifier, items, options, 671 "XEP-0060_publish", client, service, nodeIdentifier, items, options,
1182 return d 1192 return d
1183 1193
1184 async def subscribe( 1194 async def subscribe(
1185 self, 1195 self,
1186 client: SatXMPPEntity, 1196 client: SatXMPPEntity,
1187 service: jid.JID, 1197 service: Optional[jid.JID],
1188 nodeIdentifier: str, 1198 nodeIdentifier: str,
1189 sub_jid: Optional[jid.JID] = None, 1199 sub_jid: Optional[jid.JID] = None,
1190 options: Optional[dict] = None 1200 options: Optional[dict] = None
1191 ) -> pubsub.Subscription: 1201 ) -> pubsub.Subscription:
1192 # TODO: reimplement a subscribtion cache, checking that we have not subscription before trying to subscribe 1202 # TODO: reimplement a subscribtion cache, checking that we have not subscription before trying to subscribe
1203 if service is None:
1204 service = client.jid.userhostJID()
1193 cont, trigger_sub = await self.host.trigger.asyncReturnPoint( 1205 cont, trigger_sub = await self.host.trigger.asyncReturnPoint(
1194 "XEP-0060_subscribe", client, service, nodeIdentifier, sub_jid, options, 1206 "XEP-0060_subscribe", client, service, nodeIdentifier, sub_jid, options,
1195 ) 1207 )
1196 if not cont: 1208 if not cont:
1197 return trigger_sub 1209 return trigger_sub