changeset 465:78e67a59d51d

plugin groupblog: added getLastGroupBlogs method
author Goffi <goffi@goffi.org>
date Sat, 24 Mar 2012 17:48:12 +0100 (2012-03-24)
parents 794f60e2da27
children 448ce3c9e2ac
files src/plugins/plugin_misc_groupblog.py src/plugins/plugin_xep_0277.py
diffstat 2 files changed, 79 insertions(+), 40 deletions(-) [+]
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)
-        
-
--- a/src/plugins/plugin_xep_0277.py	Sat Mar 24 17:46:42 2012 +0100
+++ b/src/plugins/plugin_xep_0277.py	Sat Mar 24 17:48:12 2012 +0100
@@ -75,7 +75,7 @@
                                doc = {
                                      })
 
-    def _item2mbdata(self, item):
+    def item2mbdata(self, item):
         """Convert an XML Item to microblog data used in bridge API
         @param item: domish.Element of microblog item
         @return: microblog data (dictionary)"""
@@ -110,7 +110,7 @@
 
     def microblogCB(self, itemsEvent, profile):
         for item in itemsEvent.items:
-            microblog_data = self._item2mbdata(item)
+            microblog_data = self.item2mbdata(item)
             self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
 
     def data2entry(self, data, profile):
@@ -149,14 +149,14 @@
     def getLastMicroblogs(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 wants to get
+        @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
         """
         assert(callback)
         d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
-        d.addCallbacks(lambda items: callback(map(self._item2mbdata, items)), errback)
+        d.addCallbacks(lambda items: callback(map(self.item2mbdata, items)), errback)
         
     def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@', callback=None, errback=None):
         """Create a microblog node on PEP with given access