changeset 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 312a2842b2b8
children 539f278bc265
files src/plugins/plugin_misc_groupblog.py src/plugins/plugin_xep_0060.py src/plugins/plugin_xep_0277.py
diffstat 3 files changed, 105 insertions(+), 5 deletions(-) [+]
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
--- a/src/plugins/plugin_xep_0060.py	Mon Dec 09 15:31:07 2013 +0100
+++ b/src/plugins/plugin_xep_0060.py	Tue Dec 10 09:02:20 2013 +0100
@@ -18,9 +18,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from logging import debug, info, error
-
+from wokkel.pubsub import PubSubRequest
 from wokkel import disco, pubsub
-
 from zope.interface import implements
 
 PLUGIN_INFO = {
@@ -119,6 +118,10 @@
         profile, client = self.__getClientNProfile(profile_key, 'delete node')
         return client.deleteNode(service, nodeIdentifier)
 
+    def retractItems(self, service, nodeIdentifier, itemIdentifiers, profile_key='@DEFAULT@'):
+        profile, client = self.__getClientNProfile(profile_key, 'retract items')
+        return client.retractItems(service, nodeIdentifier, itemIdentifiers)
+
     def subscribe(self, service, nodeIdentifier, sub_jid=None, options=None, profile_key='@DEFAULT@'):
         profile, client = self.__getClientNProfile(profile_key, 'subscribe node')
         return client.subscribe(service, nodeIdentifier, sub_jid or client.parent.jid.userhostJID(), options=options)
@@ -135,6 +138,25 @@
     def connectionInitialized(self):
         pubsub.PubSubClient.connectionInitialized(self)
 
+    # XXX: this should be done in wokkel
+    def retractItems(self, service, nodeIdentifier, itemIdentifiers, sender=None):
+        """
+        Retract items from a publish subscribe node.
+
+        @param service: The publish subscribe service to delete the node from.
+        @type service: L{JID<twisted.words.protocols.jabber.jid.JID>}
+        @param nodeIdentifier: The identifier of the node.
+        @type nodeIdentifier: C{unicode}
+        @param itemIdentifiers: Identifiers of the items to be retracted.
+        @type itemIdentifiers: C{set}
+        """
+        request = PubSubRequest('retract')
+        request.recipient = service
+        request.nodeIdentifier = nodeIdentifier
+        request.itemIdentifiers = itemIdentifiers
+        request.sender = sender
+        return request.send(self.xmlstream)
+
     def itemsReceived(self, event):
         if not self.host.trigger.point("PubSubItemsReceived", event, self.parent.profile):
             return
--- a/src/plugins/plugin_xep_0277.py	Mon Dec 09 15:31:07 2013 +0100
+++ b/src/plugins/plugin_xep_0277.py	Tue Dec 10 09:02:20 2013 +0100
@@ -173,7 +173,8 @@
         _entry.author = atom.Author()
         _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8')
         _entry.updated = float(data.get('timestamp', time()))
-        _entry.id = str(_uuid)
+        entry_id = data.get('id', str(_uuid))
+        _entry.id = entry_id
         if 'comments' in data:
             link = atom.Link()
             link.attrs['href'] = data['comments']
@@ -181,8 +182,7 @@
             link.attrs['title'] = 'comments'
             _entry.links.append(link)
         _entry_elt = ElementParser()(str(_entry).decode('utf-8'))
-        item = pubsub.Item(payload=_entry_elt)
-        item['id'] = _uuid
+        item = pubsub.Item(id=entry_id, payload=_entry_elt)
         defer.returnValue(item)
 
     @defer.inlineCallbacks