diff src/plugins/plugin_xep_0277.py @ 1450:7797dda847ae

plugins xep-0277, groupblog: added subscriteToMany to replace massiveSubscribeGroupBlogs + added SatRosterProtocol.getJidsSet
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2015 22:20:43 +0200
parents e8c8e467964b
children 9b88b19b1ca8
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Sat Aug 15 22:13:27 2015 +0200
+++ b/src/plugins/plugin_xep_0277.py	Sat Aug 15 22:20:43 2015 +0200
@@ -73,8 +73,9 @@
                                    'return': 'list of microblog data (dict)'})
         host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='',
                               method=self.setMicroblogAccess,
-                              async=True,
-                              doc={})
+                              async=True)
+        host.bridge.addMethod("mBSubscribeToMany", ".plugin", in_sign='sass', out_sign='s',
+                              method=self._mBSubscribeToMany)
 
     ## plugin management methods ##
 
@@ -401,3 +402,69 @@
             return self.host.plugins["XEP-0060"].setOptions(_jid.userhostJID(), NS_MICROBLOG, _jid.userhostJID(), _options, profile_key=profile_key)
 
         create_node().addCallback(cb).addErrback(err_cb)
+
+    ## methods to manage several stanzas/jids at once ##
+
+    # common
+
+    def _getClientAndNodeData(self, publishers_type, publishers, profile_key):
+        """Helper method to construct node_data from publishers_type/publishers
+
+        @param publishers_type: type of the list of publishers, one of:
+            C.ALL: get all jids from roster, publishers is not used
+            C.GROUP: get jids from groups
+            C.JID: use publishers directly as list of jids
+        @param publishers: list of publishers, according to "publishers_type" (None, list of groups or list of jids)
+        @param profile_key: %(doc_profile_key)s
+        """
+        client = self.host.getClient(profile_key)
+        if publishers_type == C.JID:
+            jids_set = set(publishers)
+        else:
+            jids_set = client.roster.getJidsSet(publishers_type, publishers)
+
+        node_data = []
+        for jid_ in jids_set:
+            node_data.append((jid_, NS_MICROBLOG))
+        return client, node_data
+
+    def _checkPublishers(self, publishers_type, publishers):
+        """Helper method to deserialise publishers coming from bridge
+
+        publishers_type(unicode): type of the list of publishers, one of:
+        publishers: list of publishers according to type
+        @return: deserialised (publishers_type, publishers) tuple
+        """
+        if publishers_type == C.ALL:
+            if publishers:
+                raise failure.Failure(ValueError("Can't use publishers with {} type".format(publishers_type)))
+            else:
+                publishers = None
+        elif publishers_type == C.JID:
+            publishers[:] = [jid.JID(publisher) for publisher in publishers]
+        return publishers_type, publishers
+
+    # subscribe #
+
+    def _mBSubscribeToMany(self, publishers_type, publishers, profile_key):
+        """
+
+        @return (str): session id: Use pubsub.getSubscribeRTResult to get the results
+        """
+        publishers_type, publishers = self._checkPublishers(publishers_type, publishers)
+        return self.mBSubscribeToMany(publishers_type, publishers, profile_key)
+
+    def mBSubscribeToMany(self, publishers_type, publishers, profile_key):
+        """Subscribe microblogs for a list of groups or jids
+
+        @param publishers_type: type of the list of publishers, one of:
+            C.ALL: get all jids from roster, publishers is not used
+            C.GROUP: get jids from groups
+            C.JID: use publishers directly as list of jids
+        @param publishers: list of publishers, according to "publishers_type" (None, list of groups or list of jids)
+        @param profile: %(doc_profile)s
+        @return (str): session id
+        """
+        client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
+        return self.host.plugins["XEP-0060"].subscribeToMany(node_data, client.jid.userhostJID(), profile_key=profile_key)
+