diff src/plugins/plugin_misc_groupblog.py @ 621:0e16288d6816

pluging groupblog: comments handling: - comments are no more automaticly requested and sent as events - a new bridge method "getGroupBlogComments" allow to request them
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2013 12:14:43 +0200
parents 1de01a2154e2
children 69a8bfd266a5
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Thu Jun 20 12:11:54 2013 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Thu Jun 20 12:14:43 2013 +0200
@@ -101,6 +101,11 @@
                               method=self.getMassiveLastGroupBlogs,
                               async=True)
 
+        host.bridge.addMethod("getGroupBlogComments", ".plugin",
+                              in_sign='sss', out_sign='aa{ss}',
+                              method=self.getGroupBlogComments,
+                              async=True)
+
         host.bridge.addMethod("subscribeGroupBlog", ".plugin", in_sign='ss', out_sign='',
                               method=self.subscribeGroupBlog,
                               async=True)
@@ -180,17 +185,20 @@
 
         elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
             # Comment
-            self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier, profile)
+            for microblog_data in self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier):
+                publisher = None # FIXME: see below (_handleCommentsItems)
+                self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile)
             return False
         return True
 
-    def _handleCommentsItems(self, items, service, node_identifier, profile):
+    def _handleCommentsItems(self, items, service, node_identifier):
         """ Convert comments items to groupblog data, and send them as signals
         @param items: comments items
         @param service: jid of the PubSub service used
         @param node_identifier: comments node
-        @param profile: %(doc_profile)s
+        @return: list of group blog data
         """
+        ret = []
         for item in items:
             publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so
                            #        publisher is not checked and can be easily spoofed. This need to be fixed
@@ -199,8 +207,8 @@
             microblog_data["service"] = service.userhost()
             microblog_data["node"] = node_identifier
             microblog_data["verified_publisher"] = "true" if publisher else "false"
-
-            self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile)
+            ret.append(microblog_data)
+        return ret
 
     def _parseAccessData(self, microblog_data, item):
         P = self.host.plugins["XEP-0060"]
@@ -323,7 +331,7 @@
 
         self.initialise(profile_key).addCallback(initialised)
 
-    def sendGroupBlogComment(self, node_url, message, profile_key='@DEFAULT@'):
+    def sendGroupBlogComment(self, node_url, message, profile_key='@NONE@'):
         """Publish a comment in the given node
         @param node url: link to the comments node as specified in XEP-0277 and given in microblog data's comments key
         @param message: comment
@@ -344,9 +352,10 @@
     def _itemsConstruction(self, items, pub_jid, client):
         """ Transforms items to group blog data and manage comments node
         @param items: iterable of items
-        @param pub_jib: jid of the publisher
+        @param pub_jid: jid of the publisher or None to use items data
         @param client: SatXMPPClient instance
         @return: list of group blog data """
+        # TODO: use items data when pub_jid is None
         ret = []
         for item in items:
             gbdata = self.item2gbdata(item)
@@ -358,14 +367,11 @@
                     if pub_jid.userhostJID() != client.jid.userhostJID():
                         self.host.plugins["XEP-0060"].subscribe(jid.JID(gbdata["comments_service"]), gbdata["comments_node"],
                                                                 profile_key=client.profile)
-                    # we get all comments of the node, and handle them
-                    defer_getItems = self.host.plugins["XEP-0060"].getItems(jid.JID(gbdata["comments_service"]), gbdata["comments_node"], profile_key=client.profile)
-                    defer_getItems.addCallback(self._handleCommentsItems, jid.JID(gbdata["comments_service"]), gbdata["comments_node"], client.profile)
                 except KeyError:
                     warning("Missing key for comments")
         return ret
 
-    def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@DEFAULT@'):
+    def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
         """Get the last published microblogs
         @param pub_jid_s: jid of the publisher
         @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
@@ -385,7 +391,27 @@
         #TODO: we need to use the server corresponding the the host of the jid
         return self.initialise(profile_key).addCallback(initialised)
 
-    def getMassiveLastGroupBlogs(self, publishers_type, publishers, max_items=10, profile_key='@DEFAULT@'):
+    def getGroupBlogComments(self, service_s, node, profile_key='@NONE@'):
+        """Get all comments of given node
+        @param service_s: service hosting the node
+        @param node: comments node
+        @param profile_key: profile key
+        @return: list of microblog data (dict)
+        """
+        service = jid.JID(service_s)
+
+        def initialised(result):
+            profile, client = result
+            d = self.host.plugins["XEP-0060"].getItems(service, node,
+                                                       profile_key=profile_key)
+            d.addCallback(self._handleCommentsItems, service, node)
+            d.addErrback(lambda ignore: {})  # TODO: more complete error management (log !)
+            return d
+
+        #TODO: we need to use the server corresponding the the host of the jid
+        return self.initialise(profile_key).addCallback(initialised)
+
+    def getMassiveLastGroupBlogs(self, publishers_type, publishers, max_items=10, profile_key='@NONE@'):
         """Get the last published microblogs for a list of groups or jids
         @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL")
         @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids)
@@ -442,7 +468,7 @@
         return self.initialise(profile_key).addCallback(initialised)
         #TODO: we need to use the server corresponding the the host of the jid
 
-    def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT@'):
+    def subscribeGroupBlog(self, pub_jid, profile_key='@NONE@'):
         def initialised(result):
             profile, client = result
             d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),
@@ -452,7 +478,7 @@
         #TODO: we need to use the server corresponding the the host of the jid
         return self.initialise(profile_key).addCallback(initialised)
 
-    def massiveSubscribeGroupBlogs(self, publishers_type, publishers, profile_key='@DEFAULT@'):
+    def massiveSubscribeGroupBlogs(self, publishers_type, publishers, profile_key='@NONE@'):
         """Subscribe microblogs for a list of groups or jids
         @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL")
         @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids)