diff src/plugins/plugin_misc_groupblog.py @ 615:6f4c31192c7c

plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet): - PubSub options constants are moved to XEP-0060 - comments url are generated/parsed according to XEP-0277 - microblog data can now have the following keys: - "comments", with the url as given in the <link> tag - "comments_service", with the jid of the PubSub service hosting the comments - "comments_node", with the parsed node - comments nodes use different access_model according to parent microblog item access - publisher is not verified yet, see FIXME warning - so far, comments node are automatically subscribed - some bug fixes
author Goffi <goffi@goffi.org>
date Mon, 20 May 2013 23:21:29 +0200
parents 84a6e83157c2
children 8782f94e761e
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Sun Apr 07 23:27:07 2013 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Mon May 20 23:21:29 2013 +0200
@@ -25,6 +25,9 @@
 
 from zope.interface import implements
 
+import uuid
+import urllib
+
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
 except ImportError:
@@ -33,20 +36,18 @@
 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
 NS_GROUPBLOG = 'http://goffi.org/protocol/groupblog'
 NS_NODE_PREFIX = 'urn:xmpp:groupblog:'
+NS_COMMENT_PREFIX = 'urn:xmpp:comments:'
 #NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features
 NS_PUBSUB_EXP = NS_PUBSUB  # XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
 NS_PUBSUB_ITEM_ACCESS = NS_PUBSUB_EXP + "#item-access"
 NS_PUBSUB_CREATOR_JID_CHECK = NS_PUBSUB_EXP + "#creator-jid-check"
 NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config"
 NS_PUBSUB_AUTO_CREATE = NS_PUBSUB + "#auto-create"
-OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed'
-OPT_ACCESS_MODEL = 'pubsub#access_model'
-OPT_PERSIST_ITEMS = 'pubsub#persist_items'
-OPT_MAX_ITEMS = 'pubsub#max_items'
-OPT_NODE_TYPE = 'pubsub#node_type'
-OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type'
-OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth'
 TYPE_COLLECTION = 'collection'
+ACCESS_TYPE_MAP = { 'PUBLIC': 'open',
+                    'GROUP': 'roster',
+                    'JID': None, #JID is not yet managed
+                  }
 
 PLUGIN_INFO = {
     "name": "Group blogging throught collections",
@@ -83,9 +84,13 @@
         info(_("Group blog plugin initialization"))
         self.host = host
 
-        host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='',
+        host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sassa{ss}s', out_sign='',
                               method=self.sendGroupBlog)
 
+        host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='sss', out_sign='',
+                              method=self.sendGroupBlogComment,
+                              async=True)
+
         host.bridge.addMethod("getLastGroupBlogs", ".plugin",
                               in_sign='sis', out_sign='aa{ss}',
                               method=self.getLastGroupBlogs,
@@ -139,7 +144,7 @@
                     client.item_access_pubsub = entity
             client._item_access_pubsub_pending.callback(None)
 
-        if hasattr(client, "_item_access_pubsub_pending"):
+        if "_item_access_pubsub_pending" in client:
             #XXX: we need to wait for item access pubsub service check
             yield client._item_access_pubsub_pending
             del client._item_access_pubsub_pending
@@ -152,7 +157,9 @@
 
     def pubSubItemsReceivedTrigger(self, event, profile):
         """"Trigger which catch groupblogs events"""
+
         if event.nodeIdentifier.startswith(NS_NODE_PREFIX):
+            # Microblog
             publisher = jid.JID(event.nodeIdentifier[len(NS_NODE_PREFIX):])
             origin_host = publisher.host.split('.')
             event_host = event.sender.host.split('.')
@@ -169,18 +176,32 @@
 
                 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", microblog_data, 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)
+                microblog_data["comments_service"] = event.sender.userhost()
+                microblog_data["comments_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_COMMENT", microblog_data, profile)
+            return False
         return True
 
     def _parseAccessData(self, microblog_data, item):
+        P = self.host.plugins["XEP-0060"]
         form_elts = filter(lambda elt: elt.name == "x", item.children)
         for form_elt in form_elts:
             form = data_form.Form.fromElement(form_elt)
 
             if (form.formNamespace == NS_PUBSUB_ITEM_CONFIG):
-                access_model = form.get(OPT_ACCESS_MODEL, 'open')
+                access_model = form.get(P.OPT_ACCESS_MODEL, 'open')
                 if access_model == "roster":
                     try:
-                        microblog_data["groups"] = '\n'.join(form.fields[OPT_ROSTER_GROUPS_ALLOWED].values)
+                        microblog_data["groups"] = '\n'.join(form.fields[P.OPT_ROSTER_GROUPS_ALLOWED].values)
                     except KeyError:
                         warning("No group found for roster access-model")
                         microblog_data["groups"] = ''
@@ -199,24 +220,51 @@
         @return: node's name (string)"""
         return NS_NODE_PREFIX + publisher.userhost()
 
-    def _publishMblog(self, service, client, access_type, access_list, message):
+    def _publishMblog(self, service, client, access_type, access_list, message, options):
         """Actually publish the message on the group blog
         @param service: jid of the item-access pubsub service
         @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 options: dict which option name as key, which can be:
+            - allow_comments: True to accept comments, False else (default: False)
         """
-        mblog_item = self.host.plugins["XEP-0277"].data2entry({'content': message}, client.profile)
+        node_name = self.getNodeName(client.jid)
+        mblog_data = {'content': message}
+        P = self.host.plugins["XEP-0060"]
+        access_model_value = ACCESS_TYPE_MAP[access_type]
+
+        if options.get('allow_comments', 'False').lower() == 'true':
+            comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, str(uuid.uuid4()), node_name)
+            mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service.userhost(),
+                                                                     'query': urllib.urlencode([('node',comments_node.encode('utf-8'))])}
+            _options = {P.OPT_ACCESS_MODEL: access_model_value,
+                        P.OPT_PERSIST_ITEMS: 1,
+                        P.OPT_MAX_ITEMS: -1,
+                        P.OPT_DELIVER_PAYLOADS: 1,
+                        P.OPT_SEND_ITEM_SUBSCRIBE: 1,
+                        P.OPT_PUBLISH_MODEL: "subscribers", #TODO: should be open if *both* node and item access_model are open (public node and item)
+                       }
+            if access_model_value == 'roster':
+                _options[P.OPT_ROSTER_GROUPS_ALLOWED] = list(access_list)
+
+            # FIXME: check comments node creation success, at the moment this is a potential security risk (if the node
+            #        already exists, the creation will silently fail, but the comments link will stay the same, linking to a
+            #        node owned by somebody else)
+            defer_blog = self.host.plugins["XEP-0060"].createNode(service, comments_node, _options, profile_key=client.profile)
+
+        mblog_item = self.host.plugins["XEP-0277"].data2entry(mblog_data, client.profile)
         form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
+
         if access_type == "PUBLIC":
             if access_list:
                 raise BadAccessListError("access_list must be empty for PUBLIC access")
-            access = data_form.Field(None, OPT_ACCESS_MODEL, value="open")
+            access = data_form.Field(None, P.OPT_ACCESS_MODEL, value=access_model_value)
             form.addField(access)
         elif access_type == "GROUP":
-            access = data_form.Field(None, OPT_ACCESS_MODEL, value="roster")
-            allowed = data_form.Field(None, OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
+            access = data_form.Field(None, P.OPT_ACCESS_MODEL, value=access_model_value)
+            allowed = data_form.Field(None, P.OPT_ROSTER_GROUPS_ALLOWED, values=access_list)
             form.addField(access)
             form.addField(allowed)
             mblog_item.addChild(form.toElement())
@@ -225,34 +273,36 @@
         else:
             error(_("Unknown access_type"))
             raise BadAccessTypeError
-        defer_blog = self.host.plugins["XEP-0060"].publish(service, self.getNodeName(client.jid), items=[mblog_item], profile_key=client.profile)
+
+        defer_blog = self.host.plugins["XEP-0060"].publish(service, node_name, items=[mblog_item], profile_key=client.profile)
         defer_blog.addErrback(self._mblogPublicationFailed)
 
     def _mblogPublicationFailed(self, failure):
         #TODO
         return failure
 
-    def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'):
+    def sendGroupBlog(self, access_type, access_list, message, options, profile_key='@NONE@'):
         """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
+        @param options: dict which option name as key, which can be:
+            - allow_comments: True to accept comments, False else (default: False)
         @profile_key: %(doc_profile)s
         """
-        print "sendGroupBlog"
 
         def initialised(result):
             profile, client = result
             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)
+                self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message, options)
             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)
+                self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message, options)
             elif access_type == "JID":
                 raise NotImplementedError
             else:
@@ -261,19 +311,56 @@
 
         self.initialise(profile_key).addCallback(initialised)
 
-    def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
+    def sendGroupBlogComment(self, node_url, message, profile_key='@DEFAULT@'):
+        """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
+        @profile_key: %(doc_profile)s
+        """
+        profile = self.host.memory.getProfileName(profile_key)
+        if not profile:
+            error(_("Unknown profile"))
+            raise Exception("Unknown profile")
+
+        service, node = self.host.plugins["XEP-0277"].parseCommentUrl(node_url)
+
+        mblog_data = {'content': message}
+        mblog_item = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile)
+
+        return self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile)
+
+    def _itemsConstruction(self, items, pub_jid, client):
+        """ Transforms items to group blog data and manage comments node
+        @param items: iterable of items
+        @return: list of group blog data """
+        ret = []
+        for item in items:
+            gbdata = self.item2gbdata(item)
+            ret.append(gbdata)
+            # if there is a comments node, we subscribe to it
+            if "comments_node" in gbdata and pub_jid.userhostJID() != client.jid.userhostJID():
+                try:
+                    self.host.plugins["XEP-0060"].subscribe(gbdata["comments_service"], gbdata["comments_node"],
+                                                            profile_key=client.profile)
+                    self.host.plugins["XEP-0060"].getItems(gbdata["comments_service"], gbdata["comments_node"], profile_key=client.profile)
+                except KeyError:
+                    warning("Missing key for comments")
+        return ret
+
+    def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@DEFAULT@'):
         """Get the last published microblogs
-        @param pub_jid: jid of the publisher
+        @param pub_jid_s: jid of the publisher
         @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
         @param profile_key: profile key
         @return: list of microblog data (dict)
         """
+        pub_jid = jid.JID(pub_jid_s)
 
         def initialised(result):
             profile, client = result
-            d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),
+            d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(pub_jid),
                                                        max_items=max_items, profile_key=profile_key)
-            d.addCallback(lambda items: map(self.item2gbdata, items))
+            d.addCallback(self._itemsConstruction, pub_jid, client)
             d.addErrback(lambda ignore: {})  # TODO: more complete error management (log !)
             return d
 
@@ -317,10 +404,12 @@
 
             mblogs = []
 
-            for _jid in jids:
-                d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(_jid)),
+            for jid_s in jids:
+                _jid = jid.JID(jid_s)
+                d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid),
                                                            max_items=max_items, profile_key=profile_key)
-                d.addCallback(lambda items, source_jid: (source_jid, map(self.item2gbdata, items)), _jid)
+                d.addCallback(lambda items, source_jid: (source_jid, self._itemsConstruction(items, _jid, client)), jid_s)
+
                 mblogs.append(d)
             dlist = defer.DeferredList(mblogs)
             dlist.addCallback(sendResult)
@@ -335,7 +424,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='@DEFAULT@'):
         def initialised(result):
             profile, client = result
             d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),