changeset 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 2fae89f30b8d
children 5f0dbf42aa9c
files src/plugins/plugin_exp_events.py src/plugins/plugin_xep_0060.py src/plugins/plugin_xep_0163.py src/plugins/plugin_xep_0277.py
diffstat 4 files changed, 41 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_exp_events.py	Tue Jun 27 17:02:10 2017 +0200
+++ b/src/plugins/plugin_exp_events.py	Tue Jun 27 19:38:20 2017 +0200
@@ -236,7 +236,7 @@
             if e.condition == u'conflict':
                 log.debug(_(u"requested node already exists"))
 
-        yield self._p.publish(service, node, items=[item_elt], profile_key=client.profile)
+        yield self._p.publish(client, service, node, items=[item_elt])
 
         defer.returnValue(node)
 
@@ -313,4 +313,4 @@
             except KeyError:
                 pass
         item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt)
-        return self._p.publish(service, node, items=[item_elt], profile_key=client.profile)
+        return self._p.publish(client, service, node, items=[item_elt])
--- 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):
--- a/src/plugins/plugin_xep_0163.py	Tue Jun 27 17:02:10 2017 +0200
+++ b/src/plugins/plugin_xep_0163.py	Tue Jun 27 19:38:20 2017 +0200
@@ -98,9 +98,9 @@
         @param data: domish.Element to use as payload
         @param profile: profile which send the data
         """
-
+        client = self.host.getClient(profile)
         item = pubsub.Item(payload=data)
-        return self.host.plugins["XEP-0060"].publish(None, node, [item], profile_key=profile)
+        return self.host.plugins["XEP-0060"].publish(client, None, node, [item])
 
     def PEPSend(self, event_type, data, profile_key=C.PROF_KEY_NONE):
         """Send personal event after checking the data is alright
--- a/src/plugins/plugin_xep_0277.py	Tue Jun 27 17:02:10 2017 +0200
+++ b/src/plugins/plugin_xep_0277.py	Tue Jun 27 19:38:20 2017 +0200
@@ -579,7 +579,7 @@
         except error.StanzaError:
             log.warning(u"Can't create comments node for item {}".format(item_id))
         item = yield self.data2entry(data, item_id, client.profile)
-        ret = yield self._p.publish(service, node, [item], profile_key=client.profile)
+        ret = yield self._p.publish(client, service, node, [item])
         defer.returnValue(ret)
 
     ## retract ##