diff src/plugins/plugin_misc_groupblog.py @ 745:812dc38c0094

plugins groupblog (xep-0060, xep-0277): added blog item modification/deletion
author souliane <souliane@mailoo.org>
date Tue, 10 Dec 2013 09:02:20 +0100
parents 03744d9ebc13
children 64dd7c0f4feb
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Mon Dec 09 15:31:07 2013 +0100
+++ b/src/plugins/plugin_misc_groupblog.py	Tue Dec 10 09:02:20 2013 +0100
@@ -87,6 +87,14 @@
                               method=self.sendGroupBlog,
                               async=True)
 
+        host.bridge.addMethod("deleteGroupBlog", ".plugin", in_sign='(sss)ss', out_sign='',
+                              method=self.deleteGroupBlog,
+                              async=True)
+
+        host.bridge.addMethod("updateGroupBlog", ".plugin", in_sign='(sss)ssa{ss}s', out_sign='',
+                              method=self.updateGroupBlog,
+                              async=True)
+
         host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='ssa{ss}s', out_sign='',
                               method=self.sendGroupBlogComment,
                               async=True)
@@ -267,6 +275,7 @@
         access_model_value = ACCESS_TYPE_MAP[access_type]
 
         if extra.get('allow_comments', 'False').lower() == 'true':
+            # XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
             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'))])}
@@ -348,6 +357,75 @@
 
         return self._initialise(profile_key).addCallback(initialised)
 
+    def deleteGroupBlog(self, pub_data, comments, profile_key='@NONE@'):
+        """Delete a microblog item from a node.
+        @param pub_data: a tuple (service, comment node identifier, item identifier)
+        @param comments: comments node identifier (for main item) or empty string
+        @param profile_key: %(doc_profile_key)s
+        """
+
+        def initialised(result):
+            profile, client = result
+            service, node, item_id = pub_data
+            if comments:
+                # remove the associated comments node
+                d = self.host.plugins["XEP-0060"].deleteNode(jid.JID(service), node, profile_key=profile)
+                d.addErrback(lambda failure: error("Deletion of node %s failed: %s" % (node, failure.getErrorMessage())))
+                node = self.getNodeName(client.jid)
+            # remove the item itself
+            d = self.host.plugins["XEP-0060"].retractItems(jid.JID(service), node, [item_id], profile_key=profile)
+            d.addErrback(lambda failure: error("Deletion of item %s from %s failed: %s" % (item_id, node, failure.getErrorMessage())))
+            return d
+
+        def notify(d):
+            # TODO: this works only on the same host, and notifications for item deletion should be
+            # implemented according to http://xmpp.org/extensions/xep-0060.html#publisher-delete-success-notify
+            # instead. The notification mechanisms implemented in sat_pubsub and wokkel seem not compatible,
+            # see wokkel.pubsub.PubSubClient._onEvent_items and sat_pubsub.backend._doNotifyRetraction
+            service, node, item_id = pub_data
+            publisher = self.host.getJidNStream(profile_key)[0]
+            profile = self.host.memory.getProfileName(profile_key)
+            gbdatum = {'id': item_id, 'type': 'main_item' if comments else 'comment'}
+            self.host.bridge.personalEvent(publisher.full(), "MICROBLOG_DELETE", gbdatum, profile)
+            return d
+
+        return self._initialise(profile_key).addCallback(initialised).addCallback(notify)
+
+    def updateGroupBlog(self, pub_data, comments, message, extra, profile_key='@NONE@'):
+        """Modify a microblog node
+        @param pub_data: a tuple (service, comment node identifier, item identifier)
+        @param comments: comments node identifier (for main item) or empty string
+        @param message: new message
+        @param extra: dict which option name as key, which can be:
+            - allow_comments: True to accept an other level of comments, False else (default: False)
+            - rich: if present, contain rich text in currently selected syntax
+        @param profile_key: %(doc_profile)
+        """
+
+        def initialised(result):
+            profile, client = result
+            mblog_data = {'content': message}
+            if 'rich' in extra:
+                mblog_data['rich'] = extra['rich']
+            service, node, item_id = pub_data
+            if comments:
+                node = self.getNodeName(client.jid)
+            mblog_data['id'] = str(item_id)
+            if extra.get('allow_comments', 'False').lower() == 'true':
+                comments_node = self.host.plugins["XEP-0277"].parseCommentUrl(comments)[1]
+                # we could use comments_node directly but it's safer to rebuild it
+                # XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
+                hash_ = comments_node.split('_')[1].split('__')[0]
+                comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, hash_, node)
+                mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': jid.JID(service).userhost(),
+                                                                         'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
+            entry_d = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile)
+            entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(jid.JID(service), node, items=[mblog_item], profile_key=profile))
+            entry_d.addErrback(lambda failure: error("Modification of %s failed: %s" % (pub_data, failure.getErrorMessage())))
+            return entry_d
+
+        return self._initialise(profile_key).addCallback(initialised)
+
     def sendGroupBlogComment(self, node_url, message, extra, 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