diff src/plugins/plugin_xep_0277.py @ 1452:5116d70ddd1c

plugin xep-0060,xep-0277: added getFromMany using new RTDeferredSession mechanism
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2015 22:22:34 +0200
parents 9b88b19b1ca8
children d5e72362ee91
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Sat Aug 15 22:20:56 2015 +0200
+++ b/src/plugins/plugin_xep_0277.py	Sat Aug 15 22:22:34 2015 +0200
@@ -28,6 +28,7 @@
 from sat.tools.xml_tools import ElementParser
 
 from wokkel import pubsub
+from wokkel import rsm
 from feed import atom, date
 import uuid
 from time import time
@@ -77,6 +78,10 @@
                               async=True)
         host.bridge.addMethod("mBSubscribeToMany", ".plugin", in_sign='sass', out_sign='s',
                               method=self._mBSubscribeToMany)
+        host.bridge.addMethod("mBGetFromManyRTResult", ".plugin", in_sign='ss', out_sign='(ua(sssaa{ss}a{ss}))',
+                              method=self._mBGetFromManyRTResult, async=True)
+        host.bridge.addMethod("mBGetFromMany", ".plugin", in_sign='sasia{ss}s', out_sign='s',
+                              method=self._mBGetFromMany)
 
     ## plugin management methods ##
 
@@ -329,15 +334,9 @@
 
         @return: a deferred couple with the list of items and metadatas.
         """
-        items, metadata = yield self.host.plugins["XEP-0060"].getItems(pub_jid, NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
-        dlist_result = yield defer.DeferredList(map(self.item2mbdata, items), consumeErrors=True)
-        items_data = []
-        for success, value in dlist_result:
-            if success:
-                items_data.append(value)
-            else:
-                log.warning(u"Error while parsing microblog data: {}".format(value.value))
-        defer.returnValue((items_data, metadata))
+        items_data = yield self._p.getItems(pub_jid, NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
+        serialised = yield self._p.serItemsDataD(items_data, self.item2mbdata)
+        defer.returnValue(serialised)
 
     def parseCommentUrl(self, node_url):
         """Parse a XMPP URI
@@ -468,3 +467,58 @@
         client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
         return self._p.subscribeToMany(node_data, client.jid.userhostJID(), profile_key=profile_key)
 
+    # get #
+
+    def _mBGetFromManyRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT):
+        """Get real-time results for mBGetFromMany session
+
+        @param session_id: id of the real-time deferred session
+        @param return (tuple): (remaining, results) where:
+            - remaining is the number of still expected results
+            - results is a list of tuple with
+                - service (unicode): pubsub service
+                - node (unicode): pubsub node
+                - failure (unicode): empty string in case of success, error message else
+                - items_data(tuple): data tuple as returned by [getLastMicroblogs]
+        @param profile_key: %(doc_profile_key)s
+        """
+        def onSuccess(items_data):
+            """convert items elements to list of microblog data in items_data"""
+            d = self._p.serItemsDataD(items_data, self.item2mbdata)
+            d.addCallback(lambda serialised:('', serialised))
+            return d
+
+        profile = self.host.getClient(profile_key).profile
+        d = self._p.getRTResults(session_id,
+                                 on_success = onSuccess,
+                                 on_error = lambda failure: (unicode(failure.value), ([],{})),
+                                 profile = profile)
+        d.addCallback(lambda ret: (ret[0],
+                                   [(service.full(), node, failure, items, metadata)
+                                    for (service, node), (success, (failure, (items, metadata))) in ret[1].iteritems()]))
+        return d
+
+    def _mBGetFromMany(self, publishers_type, publishers, max_item=10, rsm_dict=None, profile_key=C.PROF_KEY_NONE):
+        """
+        @param max_item(int): maximum number of item to get, C.NO_LIMIT for no limit
+        """
+        max_item = None if max_item == C.NO_LIMIT else max_item
+        publishers_type, publishers = self._checkPublishers(publishers_type, publishers)
+        return self.mBGetFromMany(publishers_type, publishers, max_item, rsm.RSMRequest(**rsm_dict) if rsm_dict else None, profile_key)
+
+    def mBGetFromMany(self, publishers_type, publishers, max_item=None, rsm_data=None, profile_key=C.PROF_KEY_NONE):
+        """Get the published microblogs for a list of groups or jids
+
+        @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
+        @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
+        @param max_items (int): optional limit on the number of retrieved items.
+        @param rsm_data (rsm.RSMRequest): RSM request data, common to all publishers
+        @param profile_key: profile key
+        @return: a deferred dict with:
+            - key: publisher (unicode)
+            - value: couple (list[dict], dict) with:
+                - the microblogs data
+                - RSM response data
+        """
+        client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
+        return self._p.getFromMany(node_data, max_item, rsm_data, profile_key=profile_key)