diff 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
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0060.py	Tue Jun 27 17:02:10 2017 +0200
+++ b/src/plugins/plugin_xep_0060.py	Tue Jun 27 19:38:20 2017 +0200
@@ -22,7 +22,6 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.core import exceptions
-
 from sat.tools import sat_defer
 
 from twisted.words.protocols.jabber import jid, error
@@ -93,7 +92,8 @@
         host.bridge.addMethod("psNodeAffiliationsGet", ".plugin", in_sign='sss', out_sign='a{ss}', method=self._getNodeAffiliations, async=True)
         host.bridge.addMethod("psNodeAffiliationsSet", ".plugin", in_sign='ssa{ss}s', out_sign='', method=self._setNodeAffiliations, async=True)
         host.bridge.addMethod("psNodeDelete", ".plugin", in_sign='sss', out_sign='', method=self._deleteNode, async=True)
-        host.bridge.addMethod("psItemGet", ".plugin", in_sign='ssiassa{ss}s', out_sign='(asa{ss})', method=self._getItems, async=True)
+        host.bridge.addMethod("psItemsGet", ".plugin", in_sign='ssiassa{ss}s', out_sign='(asa{ss})', method=self._getItems, async=True)
+        host.bridge.addMethod("psItemSend", ".plugin", in_sign='ssssa{ss}s', out_sign='s', method=self._sendItem, async=True)
         host.bridge.addMethod("psRetractItem", ".plugin", in_sign='sssbs', out_sign='', method=self._retractItem, async=True)
         host.bridge.addMethod("psRetractItems", ".plugin", in_sign='ssasbs', out_sign='', method=self._retractItems, async=True)
         host.bridge.addMethod("psSubscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany)
@@ -258,8 +258,41 @@
     #     d.addCallback(lambda subs: [sub.getAttribute('node') for sub in subs if sub.getAttribute('subscription') == filter_])
     #     return d
 
-    def publish(self, service, nodeIdentifier, items=None, profile_key=C.PROF_KEY_NONE):
+    def _sendItem(self, service, nodeIdentifier, payload, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
         client = self.host.getClient(profile_key)
+        service = None if not service else jid.JID(service)
+        d = self.sendItem(client, service, nodeIdentifier, payload, item_id or None, extra)
+        d.addCallback(lambda ret: ret or u'')
+        return d
+
+    def _getPublishedItemId(self, iq_elt, original_id):
+        """return item of published id if found in answer
+
+        if not found original_id is returned, or empty string if it is None or empty string
+        """
+        try:
+            item_id = iq_elt.pubsub.publish.item['id']
+        except (AttributeError, KeyError):
+            item_id = None
+        return item_id or original_id
+
+    def sendItem(self, client, service, nodeIdentifier, payload, item_id=None, extra=None):
+        """high level method to send one item
+
+        @param service(jid.JID, None): service to send the item to
+            None to use PEP
+        @param NodeIdentifier(unicode): PubSub node to use
+        @param item_id(unicode, None): id to use or None to create one
+        @param payload(domish.Element, unicode): payload of the item to send
+        @param extra(dict, None): extra option, not used yet
+        @return (unicode, None): id of the created item
+        """
+        item_elt = pubsub.Item(id=item_id, payload=payload)
+        d = self.publish(client, service, nodeIdentifier, [item_elt])
+        d.addCallback(self._getPublishedItemId, item_id)
+        return d
+
+    def publish(self, client, service, nodeIdentifier, items=None):
         return client.pubsub_client.publish(service, nodeIdentifier, items, client.pubsub_client.parent.jid)
 
     def _unwrapMAMMessage(self, message_elt):