changeset 617:dafdbe28ca2f

plugin group blog: comments handling (comments are automaticaly requested when a comment node is found)
author Goffi <goffi@goffi.org>
date Mon, 17 Jun 2013 13:17:43 +0200
parents 8782f94e761e
children e2c33a16d235
files src/plugins/plugin_misc_groupblog.py
diffstat 1 files changed, 30 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Sun Jun 16 18:49:02 2013 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Mon Jun 17 13:17:43 2013 +0200
@@ -171,26 +171,37 @@
                 warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender),
                                                                                  unicode(publisher)))
                 return
-            for item in event.items:
-                microblog_data = self.item2gbdata(item)
+
+            client = self.host.getClient(profile)
 
-                self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", microblog_data, profile)
+            for gbdata in self._itemsConstruction(event.items, publisher, client):
+                self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdata, profile)
             return False
+
         elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
             # Comment
-            for item in event.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
-                               #        quickly.
-                microblog_data = self.item2gbdata(item, "comment")
-                microblog_data["service"] = event.sender.userhost()
-                microblog_data["node"] = event.nodeIdentifier
-                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)
+            self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier, profile)
             return False
         return True
 
+    def _handleCommentsItems(self, items, service, node_identifier, profile):
+        """ 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
+        """
+        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
+                           #        quickly.
+            microblog_data = self.item2gbdata(item, "comment")
+            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)
+
     def _parseAccessData(self, microblog_data, item):
         P = self.host.plugins["XEP-0060"]
         form_elts = filter(lambda elt: elt.name == "x", item.children)
@@ -333,6 +344,8 @@
     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 client: SatXMPPClient instance
         @return: list of group blog data """
         ret = []
         for item in items:
@@ -341,9 +354,12 @@
             # if there is a comments node, we subscribe to it
             if "comments_node" in gbdata and pub_jid.userhostJID() != client.jid.userhostJID():
                 try:
+                    # every comments node must be subscribed
                     self.host.plugins["XEP-0060"].subscribe(jid.JID(gbdata["comments_service"]), gbdata["comments_node"],
                                                             profile_key=client.profile)
-                    self.host.plugins["XEP-0060"].getItems(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