# HG changeset patch # User souliane # Date 1390407028 -3600 # Node ID d7f9cd8a08cd28d1b9906da50931574d89e26508 # Parent d6bdf6022180d32cb4ade37b768c147017ecb21c plugin groupblog: added method getLastGroupBlogsAtom returns the Atom feed of blog posts diff -r d6bdf6022180 -r d7f9cd8a08cd src/plugins/plugin_misc_groupblog.py --- 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 = """ + + %(user)s's blogposts + + + %(id)s + %(date)s\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 + "" + + 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