diff src/plugins/plugin_misc_groupblog.py @ 465:78e67a59d51d

plugin groupblog: added getLastGroupBlogs method
author Goffi <goffi@goffi.org>
date Sat, 24 Mar 2012 17:48:12 +0100
parents d9456d94cd12
children 47af60767013
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Sat Mar 24 17:46:42 2012 +0100
+++ b/src/plugins/plugin_misc_groupblog.py	Sat Mar 24 17:48:12 2012 +0100
@@ -59,10 +59,7 @@
 "description": _("""Implementation of microblogging with roster access""")
 }
 
-class NodeCreationError(Exception):
-    pass
-
-class NodeDeletionError(Exception):
+class NoCompatiblePubSubServerFound(Exception):
     pass
 
 class GroupBlog():
@@ -79,7 +76,53 @@
                                        'param_1':'text to send',
                                        'param_2':'%(doc_profile)s'
                                      })
+       
+        host.bridge.addMethod("getLastGroupBlogs", ".plugin",
+                              in_sign='sis', out_sign='aa{ss}',
+                              method=self.getLastGroupBlogs,
+                              async = True,
+                              doc = { 'summary':'retrieve items',
+                                      'param_0':'jid: publisher of wanted microblog',
+                                      'param_1':'max_items: see XEP-0060 #6.5.7',
+                                      'param_2':'%(doc_profile)s',
+                                      'return':'list of microblog data (dict)'
+                                    })
+       
+    @defer.inlineCallbacks
+    def initialise(self, profile_key):
+        """Check that this data for this profile are initialised, and do it else
+        @param client: client of the profile
+        @profile_key: %(doc_profile)s"""
+        profile = self.host.memory.getProfileName(profile_key)
+        if not profile:
+            error(_("Unknown profile"))
+            raise Exception("Unknown profile") 
         
+        client = self.host.getClient(profile)
+        if not client:
+            error(_('No client for this profile key: %s') % profile_key)
+            raise Exception("Unknown profile") 
+        yield client.client_initialized #we want to be sure that the client is initialized
+        
+        #we first check that we have a item-access pubsub server
+        if not hasattr(client,"item_access_pubsub"):
+            debug(_('Looking for item-access power pubsub server'))
+            #we don't have any pubsub server featuring item access yet
+            test = self.host.memory.getServerServiceEntities("pubsub", "service", profile)
+            client.item_access_pubsub = None
+            for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile):
+                disco = yield client.disco.requestInfo(entity)
+                if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features):
+                    info(_("item-access powered pubsub service found: [%s]") % entity.full())
+                    client.item_access_pubsub = entity
+
+        if not client.item_access_pubsub:
+            error(_("No item-access powered pubsub server found, can't post group blog"))
+            raise NoCompatiblePubSubServerFound
+
+        defer.returnValue((profile, client))
+
+
     def _publishMblog(self, service, groups, message, client):
         """Actually publish the message on the group blog
         @param service: jid of the item-access pubsub service
@@ -98,7 +141,6 @@
         #TODO
         pass
 
-    @defer.inlineCallbacks
     def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'):
         """Publish a microblog to the node associated to the groups
         If the node doesn't exist, it is created, then the message is posted
@@ -107,37 +149,34 @@
         @profile_key: %(doc_profile)s
         """
         print "sendGroupBlog"
-        profile = self.host.memory.getProfileName(profile_key)
-        if not profile:
-            error(_("Unknown profile"))
-            return
-        
-        client = self.host.getClient(profile)
-        if not client:
-            error(_('No client for this profile key: %s') % profile_key)
-            return
-        
-        yield client.client_initialized #we want to be sure that the client is initialized
+
+        def initialised(result):
+            profile, client = result
+            _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist
+            #TODO: send an error signal if user want to post to non existant groups
+            
+            self._publishMblog(client.item_access_pubsub, _groups, message, client)
+
+        self.initialise(profile_key).addCallback(initialised)
+
+
         
-        #we first check that we have a item-access pubsub server
-        if not hasattr(client,"item_access_pubsub"):
-            debug(_('Looking for item-access power pubsub server'))
-            #we don't have any pubsub server featuring item access yet
-            test = self.host.memory.getServerServiceEntities("pubsub", "service", profile)
-            client.item_access_pubsub = None
-            for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile):
-                disco = yield client.disco.requestInfo(entity)
-                if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features):
-                    info(_("item-access powered pubsub service found: [%s]") % entity.full())
-                    client.item_access_pubsub = entity
+    def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None):
+        """Get the last published microblogs
+        @param pub_jid: jid of the publisher
+        @param max_items: how many microblogs we want to get
+        @param profile_key: profile key
+        @param callback: used for the async answer
+        @param errback: used for the async answer
+        """
+        
+        def initialised(result):
+            profile, client = result
+            d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, jid.JID(pub_jid).userhost(),
+                                                       max_items=max_items, profile_key=profile_key)
+            d.addCallbacks(lambda items: callback(map(self.host.plugins["XEP-0277"].item2mbdata, items)), errback)
 
-        if not client.item_access_pubsub:
-            error(_("No item-access powered pubsub server found, can't post group blog"))
-            return
+        assert(callback)
+        self.initialise(profile_key).addCallback(initialised)
+        #TODO: we need to use the server corresponding the the host of the jid
 
-        _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist
-        #TODO: send an error signal if user want to post to non existant groups
-        
-        self._publishMblog(client.item_access_pubsub, _groups, message, client)
-        
-