diff src/plugins/plugin_misc_groupblog.py @ 831:d7f9cd8a08cd

plugin groupblog: added method getLastGroupBlogsAtom returns the Atom feed of blog posts
author souliane <souliane@mailoo.org>
date Wed, 22 Jan 2014 17:10:28 +0100
parents 1fe00f0c9a91
children c4b22aedb7d7
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Wed Jan 15 23:24:22 2014 +0100
+++ b/src/plugins/plugin_misc_groupblog.py	Wed Jan 22 17:10:28 2014 +0100
@@ -21,10 +21,11 @@
 from logging import debug, info, warning, error
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid
+from twisted.words.xish.domish import Element
 from sat.core import exceptions
 from wokkel import disco, data_form, iwokkel
 from zope.interface import implements
-
+from feed import date
 import uuid
 import urllib
 
@@ -105,6 +106,11 @@
                               method=self.getLastGroupBlogs,
                               async=True)
 
+        host.bridge.addMethod("getLastGroupBlogsAtom", ".plugin",
+                              in_sign='sis', out_sign='s',
+                              method=self.getLastGroupBlogsAtom,
+                              async=True)
+
         host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
                               in_sign='sasis', out_sign='a{saa{ss}}',
                               method=self._getMassiveLastGroupBlogs,
@@ -496,6 +502,53 @@
         #TODO: we need to use the server corresponding the the host of the jid
         return self._initialise(profile_key).addCallback(initialised)
 
+    def getLastGroupBlogsAtom(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
+        """Get the atom feed of the last published microblogs
+        @param pub_jid: 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: atom XML feed (unicode)
+        """
+        pub_jid = jid.JID(pub_jid_s)
+
+        def removeAllURIs(element):
+            """Recursively remove the URIs of the element and its children.
+            Without that, the entry would still be valid but not displayed
+            by Firefox nor Thunderbird (and probably more readers)"""
+            element.uri = element.defaultUri = None
+            for child in element.children:
+                if isinstance(child, Element):
+                    removeAllURIs(child)
+
+        def items2feed(items, pub_jid, client):
+            feed = """<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+    <title>%(user)s's blogposts</title>
+    <link href="%(feed)s" rel="self" />
+    <link href="%(blog)s" />
+    <id>%(id)s</id>
+    <updated>%(date)s</updated>\n""" % {'user': pub_jid.user,
+                                        'feed': 'http://%s/blog/%s/atom.xml' % (client.jid.host, pub_jid.user),
+                                        'blog': 'http://%s/blog/%s' % (client.jid.host, pub_jid.user),
+                                        'id': self.getNodeName(pub_jid),
+                                        'date': date.rfc3339.timestamp_from_tf(date.rfc3339.tf_utc())}
+            for item in items:
+                entry = item.firstChildElement()
+                removeAllURIs(entry)
+                feed += "    " + entry.toXml().encode('utf-8') + "\n"
+            return feed + "</feed>"
+
+        def initialised(result):
+            profile, client = result
+            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(items2feed, pub_jid, client)
+            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 getGroupBlogComments(self, service_s, node, profile_key='@NONE@'):
         """Get all comments of given node
         @param service_s: service hosting the node