diff src/plugins/plugin_misc_groupblog.py @ 471:6cd04adddaea

core: exceptions moved to core plugin group blog: group blog now manage public microblogs
author Goffi <goffi@goffi.org>
date Wed, 04 Apr 2012 00:06:44 +0200
parents 5c916b99d0f6
children b9fd32b46306
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Sun Apr 01 19:48:31 2012 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Wed Apr 04 00:06:44 2012 +0200
@@ -62,6 +62,12 @@
 class NoCompatiblePubSubServerFound(Exception):
     pass
 
+class BadAccessTypeError(Exception):
+    pass
+
+class BadAccessListError(Exception):
+    pass
+
 class GroupBlog():
     """This class use a SàT PubSub Service to manage access on microblog"""
 
@@ -69,24 +75,13 @@
         info(_("Group blog plugin initialization"))
         self.host = host
 
-        host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='asss', out_sign='',
-                               method=self.sendGroupBlog,
-                               doc = { 'summary':"Send a microblog to a list of groups",
-                                       'param_0':'list of groups which can read the microblog',
-                                       'param_1':'text to send',
-                                       'param_2':'%(doc_profile)s'
-                                     })
+        host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='',
+                               method=self.sendGroupBlog)
        
         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)'
-                                    })
+                              async = True)
 
         host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
                               in_sign='sasis', out_sign='a{saa{ss}}',
@@ -129,40 +124,61 @@
         defer.returnValue((profile, client))
 
 
-    def _publishMblog(self, service, groups, message, client):
+    def _publishMblog(self, service, client, access_type, access_list, message):
         """Actually publish the message on the group blog
         @param service: jid of the item-access pubsub service
-        @param groups: set of groups allowed to see the item
+        @param client: SatXMPPClient of the published
+        @param access_type: one of "PUBLIC", "GROUP", "JID"
+        @param access_list: set of entities (empty list for all, groups or jids) allowed to see the item
         @param message: message to publish
-        @param client: SatXMPPClient of the published"""
+        """
         mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile)
         form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
-        field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=groups)
-        form.addField(field)
-        mblog_item.addChild(form.toElement())
+        if access_type == "PUBLIC":
+            if access_list:
+                raise BadAccessListError("access_list must be empty for PUBLIC access")
+        elif access_type == "GROUP":
+            field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
+            form.addField(field)
+            mblog_item.addChild(form.toElement())
+        elif access_type == "JID":
+            raise NotImplementedError
+        else:
+            error(_("Unknown access_type"))
+            raise BadAccessTypeError
         defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile)
         defer_blog.addErrback(self._mblogPublicationFailed)
 
     def _mblogPublicationFailed(self, failure):
         #TODO
-        pass
+        return failure
 
-    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
-        @param groups: list of groups allowed to retrieve the microblog
+    def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
+        """Publish a microblog with given item access
+        @param access_type: one of "PUBLIC", "GROUP", "JID"
+        @param access_list: list of authorized entity (empty list for PUBLIC ACCESS,
+                            list of groups or list of jids) for this item
         @param message: microblog
         @profile_key: %(doc_profile)s
         """
         print "sendGroupBlog"
-
         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
+            if access_type == "PUBLIC":
+                if access_list:
+                    raise Exception("Publishers list must be empty when getting microblogs for all contacts")
+                self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message)
+            elif access_type == "GROUP":
+                _groups = set(access_list).intersection(client.roster.getGroups()) #We only keep group which actually exist
+                if not _groups:
+                    raise BadAccessListError("No valid group")
+                self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message)
+            elif access_type == "JID":
+                raise NotImplementedError
+            else:
+                error(_("Unknown access type"))
+                raise BadAccessTypeError 
             
-            self._publishMblog(client.item_access_pubsub, _groups, message, client)
-
         self.initialise(profile_key).addCallback(initialised)
 
 
@@ -170,10 +186,9 @@
     def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
         """Get the last published microblogs
         @param pub_jid: jid of the publisher
-        @param max_items: how many microblogs we want to get
+        @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
         @param profile_key: profile key
-        @param callback: used for the async answer
-        @param errback: used for the async answer
+        @return: list of microblog data (dict)
         """
         
         def initialised(result):